Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
posixthreadbarrier.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
10
#include "
core/types.h
"
11
#include "
threading/criticalsection.h
"
12
#include <
semaphore.h
>
13
14
//------------------------------------------------------------------------------
15
namespace
Posix
16
{
17
class
PosixThreadBarrier
18
{
19
public
:
21
PosixThreadBarrier
();
23
~PosixThreadBarrier
();
25
void
Setup
(
SizeT
numThreads
);
27
bool
IsValid
()
const
;
29
bool
Arrive
();
31
void
Wait
();
33
void
SignalContinue
();
34
35
private
:
36
Threading::CriticalSection
critSect
;
37
long
numThreads
;
38
volatile
long
outstandingThreads
;
39
sem_t*
semaphore
;
40
bool
isValid
;
41
};
42
43
//------------------------------------------------------------------------------
46
PosixThreadBarrier::PosixThreadBarrier
() :
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
//------------------------------------------------------------------------------
58
PosixThreadBarrier::~PosixThreadBarrier
()
59
{
60
sem_destroy(this->
semaphore
);
61
delete
this->
semaphore
;
62
this->
semaphore
= 0;
63
}
64
65
//------------------------------------------------------------------------------
68
void
69
PosixThreadBarrier::Setup
(
SizeT
numThreads
)
70
{
71
n_assert
(!this->
IsValid
());
72
this->numThreads =
numThreads
;
73
this->
outstandingThreads
=
numThreads
;
74
this->
isValid
=
true
;
75
}
76
77
//------------------------------------------------------------------------------
80
bool
81
PosixThreadBarrier::IsValid
()
const
82
{
83
return
this->
isValid
;
84
}
85
86
//------------------------------------------------------------------------------
89
bool
90
PosixThreadBarrier::Arrive
()
91
{
92
this->
critSect
.Enter();
93
n_assert
(this->
outstandingThreads
> 0);
94
this->
outstandingThreads
--;
95
return
(0 == this->
outstandingThreads
);
96
}
97
98
//------------------------------------------------------------------------------
101
void
102
PosixThreadBarrier::Wait
()
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
//------------------------------------------------------------------------------
120
void
121
PosixThreadBarrier::SignalContinue
()
122
{
123
this->
outstandingThreads
= this->
numThreads
;
124
sem_post(this->
semaphore
);
125
this->
critSect
.Leave();
126
}
127
128
129
}
// namespace PosiX
Posix::PosixThreadBarrier::outstandingThreads
volatile long outstandingThreads
Definition
posixthreadbarrier.h:38
Posix::PosixThreadBarrier::numThreads
long numThreads
Definition
posixthreadbarrier.h:37
Posix::PosixThreadBarrier::Setup
void Setup(SizeT numThreads)
setup the object with the number of threads
Definition
posixthreadbarrier.h:69
Posix::PosixThreadBarrier::Arrive
bool Arrive()
enter thread barrier, return false if not all threads have arrived yet
Definition
posixthreadbarrier.h:90
Posix::PosixThreadBarrier::Wait
void Wait()
call after Arrive() returns false to wait for other threads
Definition
posixthreadbarrier.h:102
Posix::PosixThreadBarrier::~PosixThreadBarrier
~PosixThreadBarrier()
destructor
Definition
posixthreadbarrier.h:58
Posix::PosixThreadBarrier::IsValid
bool IsValid() const
return true if the object has been setup
Definition
posixthreadbarrier.h:81
Posix::PosixThreadBarrier::critSect
Threading::CriticalSection critSect
Definition
posixthreadbarrier.h:36
Posix::PosixThreadBarrier::semaphore
sem_t * semaphore
Definition
posixthreadbarrier.h:39
Posix::PosixThreadBarrier::PosixThreadBarrier
PosixThreadBarrier()
constructor
Definition
posixthreadbarrier.h:46
Posix::PosixThreadBarrier::isValid
bool isValid
Definition
posixthreadbarrier.h:40
Posix::PosixThreadBarrier::SignalContinue
void SignalContinue()
call after Arrive() returns true to resume all threads
Definition
posixthreadbarrier.h:121
criticalsection.h
n_assert
#define n_assert(exp)
Definition
debug.h:50
Posix
Posix implemention of a read-many write-few lock.
Definition
posixsysfunc.cc:21
semaphore.h
A semaphore is an inter-GPU queue synchronization primitive.
types.h
SizeT
int SizeT
Definition
types.h:42
code
foundation
threading
posix
posixthreadbarrier.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.