Nebula
Loading...
Searching...
No Matches
posixthreadbarrier.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
10#include "core/types.h"
12#include <semaphore.h>
13
14//------------------------------------------------------------------------------
15namespace Posix
16{
17class PosixThreadBarrier
18{
19public:
27 bool IsValid() const;
29 bool Arrive();
31 void Wait();
33 void SignalContinue();
34
35private:
38 volatile long outstandingThreads;
39 sem_t* semaphore;
40 bool isValid;
41};
42
43//------------------------------------------------------------------------------
47 numThreads(0),
48 outstandingThreads(0),
49 isValid(false)
50{
51 this->semaphore = new sem_t;
52 sem_init(this->semaphore, 0, 0);
53}
54
55//------------------------------------------------------------------------------
59{
60 sem_destroy(this->semaphore);
61 delete this->semaphore;
62 this->semaphore = 0;
63}
64
65//------------------------------------------------------------------------------
68void
70{
71 n_assert(!this->IsValid());
72 this->numThreads = numThreads;
74 this->isValid = true;
75}
76
77//------------------------------------------------------------------------------
80bool
82{
83 return this->isValid;
84}
85
86//------------------------------------------------------------------------------
89bool
91{
92 this->critSect.Enter();
94 this->outstandingThreads--;
95 return (0 == this->outstandingThreads);
96}
97
98//------------------------------------------------------------------------------
101void
103{
104 this->critSect.Leave();
105 int timeout = 2000;
106 while (timeout > 0)
107 {
108 if (0 == sem_trywait(this->semaphore))
109 {
110 return;
111 }
112 usleep(1000);
113 timeout -= 1;
114 }
115}
116
117//------------------------------------------------------------------------------
120void
122{
123 this->outstandingThreads = this->numThreads;
124 sem_post(this->semaphore);
125 this->critSect.Leave();
126}
127
128
129} // namespace PosiX
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
Definition posixthreadbarrier.h:69
bool Arrive()
enter thread barrier, return false if not all threads have arrived yet
Definition posixthreadbarrier.h:90
void Wait()
call after Arrive() returns false to wait for other threads
Definition posixthreadbarrier.h:102
~PosixThreadBarrier()
destructor
Definition posixthreadbarrier.h:58
bool IsValid() const
return true if the object has been setup
Definition posixthreadbarrier.h:81
Threading::CriticalSection critSect
Definition posixthreadbarrier.h:36
sem_t * semaphore
Definition posixthreadbarrier.h:39
PosixThreadBarrier()
constructor
Definition posixthreadbarrier.h:46
bool isValid
Definition posixthreadbarrier.h:40
void SignalContinue()
call after Arrive() returns true to resume all threads
Definition posixthreadbarrier.h:121
Critical section objects are used to protect a portion of code from parallel execution.
#define n_assert(exp)
Definition debug.h:50
Posix implemention of a read-many write-few lock.
Definition posixsysfunc.cc:21
A semaphore is an inter-GPU queue synchronization primitive.
int SizeT
Definition types.h:49