Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
posixthreadbarrier_cond.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
10
#include "
core/types.h
"
11
#include "
threading/criticalsection.h
"
12
#include <pthread.h>
13
#include <
time.h
>
14
15
//------------------------------------------------------------------------------
16
namespace
Posix
17
{
18
class
PosixThreadBarrier
19
{
20
public
:
22
PosixThreadBarrier
();
24
~PosixThreadBarrier
();
26
void
Setup
(
SizeT
numThreads
);
28
bool
IsValid
()
const
;
30
bool
Arrive
();
32
void
Wait
();
34
void
SignalContinue
();
35
36
private
:
37
Threading::CriticalSection
critSect
;
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
//------------------------------------------------------------------------------
49
inline
50
PosixThreadBarrier::PosixThreadBarrier
() :
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
//------------------------------------------------------------------------------
62
inline
63
PosixThreadBarrier::~PosixThreadBarrier
()
64
{
65
pthread_cond_destroy(&
cond
);
66
pthread_mutex_destroy(&
mutex
);
67
}
68
69
//------------------------------------------------------------------------------
72
inline
void
73
PosixThreadBarrier::Setup
(
SizeT
num)
74
{
75
n_assert
(!this->
isValid
);
76
this->
numThreads
= num;
77
this->
outstandingThreads
= num;
78
this->
isValid
=
true
;
79
}
80
81
//------------------------------------------------------------------------------
84
inline
bool
85
PosixThreadBarrier::IsValid
()
const
86
{
87
return
this->
isValid
;
88
}
89
90
//------------------------------------------------------------------------------
99
inline
bool
100
PosixThreadBarrier::Arrive
()
101
{
102
this->
critSect
.Enter();
103
n_assert
(this->
outstandingThreads
> 0);
104
this->
outstandingThreads
--;
105
return
(0 == this->
outstandingThreads
);
106
}
107
108
//------------------------------------------------------------------------------
118
inline
void
119
PosixThreadBarrier::Wait
()
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
//------------------------------------------------------------------------------
138
inline
void
139
PosixThreadBarrier::SignalContinue
()
140
{
141
this->
outstandingThreads
= this->
numThreads
;
142
pthread_cond_broadcast(&
cond
);
143
this->
critSect
.Leave();
144
}
145
146
}
// namespace Posix
147
//------------------------------------------------------------------------------
148
Posix::PosixThreadBarrier
Block until all thread have arrived at the barrier.
Definition
posixthreadbarrier.h:18
Posix::PosixThreadBarrier::mutex
pthread_mutex_t mutex
Definition
posixthreadbarrier_cond.h:41
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
Posix::PosixThreadBarrier::ts
timespec ts
Definition
posixthreadbarrier_cond.h:43
Posix::PosixThreadBarrier::cond
pthread_cond_t cond
Definition
posixthreadbarrier_cond.h:40
Posix::PosixThreadBarrier::Arrive
bool Arrive()
enter thread barrier, return false if not all threads have arrived yet
Posix::PosixThreadBarrier::Wait
void Wait()
call after Arrive() returns false to wait for other threads
Posix::PosixThreadBarrier::~PosixThreadBarrier
~PosixThreadBarrier()
destructor
Posix::PosixThreadBarrier::IsValid
bool IsValid() const
return true if the object has been setup
Posix::PosixThreadBarrier::critSect
Threading::CriticalSection critSect
Definition
posixthreadbarrier.h:36
Posix::PosixThreadBarrier::PosixThreadBarrier
PosixThreadBarrier()
constructor
Posix::PosixThreadBarrier::isValid
bool isValid
Definition
posixthreadbarrier.h:40
Posix::PosixThreadBarrier::SignalContinue
void SignalContinue()
call after Arrive() returns true to resume all threads
criticalsection.h
n_printf
void __cdecl n_printf(const char *msg,...)
Nebula's printf replacement.
Definition
debug.cc:209
n_assert
#define n_assert(exp)
Definition
debug.h:50
Posix
Posix implemention of a read-many write-few lock.
Definition
posixsysfunc.cc:21
time.h
Typedefs for the Timing subsystem.
types.h
SizeT
int SizeT
Definition
types.h:42
code
foundation
threading
posix
posixthreadbarrier_cond.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.