Nebula
Loading...
Searching...
No Matches
posixthreadbarrier_cond.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
10#include "core/types.h"
12#include <pthread.h>
13#include <time.h>
14
15//------------------------------------------------------------------------------
16namespace Posix
17{
19{
20public:
28 bool IsValid() const;
30 bool Arrive();
32 void Wait();
35
36private:
38 long numThreads;
39 volatile long outstandingThreads;
40 pthread_cond_t cond;
41 pthread_mutex_t mutex;
42 bool isValid;
43 timespec ts;
44};
45
46//------------------------------------------------------------------------------
49inline
51 numThreads(0),
52 outstandingThreads(0),
53 isValid(false)
54{
55 pthread_cond_init(&cond,NULL);
56 pthread_mutex_init(&mutex,NULL);
57}
58
59//------------------------------------------------------------------------------
62inline
64{
65 pthread_cond_destroy(&cond);
66 pthread_mutex_destroy(&mutex);
67}
68
69//------------------------------------------------------------------------------
72inline void
74{
75 n_assert(!this->isValid);
76 this->numThreads = num;
77 this->outstandingThreads = num;
78 this->isValid = true;
79}
80
81//------------------------------------------------------------------------------
84inline bool
86{
87 return this->isValid;
88}
89
90//------------------------------------------------------------------------------
99inline bool
101{
102 this->critSect.Enter();
103 n_assert(this->outstandingThreads > 0);
104 this->outstandingThreads--;
105 return (0 == this->outstandingThreads);
106}
107
108//------------------------------------------------------------------------------
118inline void
120{
121 pthread_mutex_lock(&mutex);
122 this->critSect.Leave();
123 clock_gettime(CLOCK_REALTIME,&ts);
124 ts.tv_sec += 2;
125 int reason = pthread_cond_timedwait(&cond,&mutex,&ts);
126 if ( ETIMEDOUT == reason )
127 {
128 n_printf("PosixThreadBarrier::Wait() timed out!\n");
129 }
130}
131
132//------------------------------------------------------------------------------
138inline void
140{
141 this->outstandingThreads = this->numThreads;
142 pthread_cond_broadcast(&cond);
143 this->critSect.Leave();
144}
145
146} // namespace Posix
147//------------------------------------------------------------------------------
148
Block until all thread have arrived at the barrier.
Definition posixthreadbarrier_cond.h:19
pthread_mutex_t mutex
Definition posixthreadbarrier_cond.h:41
volatile long outstandingThreads
Definition posixthreadbarrier.h:38
long numThreads
Definition posixthreadbarrier.h:37
void Setup(SizeT numThreads)
setup the object with the number of threads
timespec ts
Definition posixthreadbarrier_cond.h:43
pthread_cond_t cond
Definition posixthreadbarrier_cond.h:40
bool Arrive()
enter thread barrier, return false if not all threads have arrived yet
void Wait()
call after Arrive() returns false to wait for other threads
~PosixThreadBarrier()
destructor
bool IsValid() const
return true if the object has been setup
Threading::CriticalSection critSect
Definition posixthreadbarrier.h:36
PosixThreadBarrier()
constructor
bool isValid
Definition posixthreadbarrier.h:40
void SignalContinue()
call after Arrive() returns true to resume all threads
Critical section objects are used to protect a portion of code from parallel execution.
void __cdecl n_printf(const char *msg,...)
Nebula's printf replacement.
Definition debug.cc:209
#define n_assert(exp)
Definition debug.h:50
Posix implemention of a read-many write-few lock.
Definition posixsysfunc.cc:21
Typedefs for the Timing subsystem.
int SizeT
Definition types.h:49