Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
win32threadbarrier.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
12
#include "
core/types.h
"
13
#include "
threading/criticalsection.h
"
14
15
//------------------------------------------------------------------------------
16
namespace
Win32
17
{
18
class
Win32ThreadBarrier
19
{
20
public
:
22
Win32ThreadBarrier
();
24
~Win32ThreadBarrier
();
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
HANDLE
event
;
41
bool
isValid
;
42
};
43
44
//------------------------------------------------------------------------------
47
inline
48
Win32ThreadBarrier::Win32ThreadBarrier
() :
49
numThreads
(0),
50
outstandingThreads
(0),
51
isValid
(false)
52
{
53
// create a manual-reset event
54
this->
event
=
CreateEvent
(NULL, TRUE, FALSE, NULL);
55
}
56
57
//------------------------------------------------------------------------------
60
inline
61
Win32ThreadBarrier::~Win32ThreadBarrier
()
62
{
63
CloseHandle(this->
event
);
64
this->
event
= 0;
65
}
66
67
//------------------------------------------------------------------------------
70
inline
void
71
Win32ThreadBarrier::Setup
(
SizeT
num)
72
{
73
n_assert
(!this->
isValid
);
74
this->
numThreads
= num;
75
this->
outstandingThreads
= num;
76
this->
isValid
=
true
;
77
}
78
79
//------------------------------------------------------------------------------
82
inline
bool
83
Win32ThreadBarrier::IsValid
()
const
84
{
85
return
this->
isValid
;
86
}
87
88
//------------------------------------------------------------------------------
97
inline
bool
98
Win32ThreadBarrier::Arrive
()
99
{
100
this->
critSect
.Enter();
101
n_assert
(this->
outstandingThreads
> 0);
102
this->
outstandingThreads
--;
103
return
(0 == this->
outstandingThreads
);
104
}
105
106
//------------------------------------------------------------------------------
116
inline
void
117
Win32ThreadBarrier::Wait
()
118
{
119
ResetEvent(this->
event
);
120
this->
critSect
.Leave();
121
DWORD reason = WaitForSingleObject(this->
event
, 2000);
122
if
(WAIT_TIMEOUT == reason)
123
{
124
n_printf
(
"Win32ThreadBarrier::Wait() timed out!\n"
);
125
}
126
}
127
128
//------------------------------------------------------------------------------
134
inline
void
135
Win32ThreadBarrier::SignalContinue
()
136
{
137
this->
outstandingThreads
= this->
numThreads
;
138
SetEvent(this->
event
);
139
this->
critSect
.Leave();
140
}
141
142
}
// namespace Win32
143
//------------------------------------------------------------------------------
144
Win32::Win32ThreadBarrier::~Win32ThreadBarrier
~Win32ThreadBarrier()
destructor
Definition
win32threadbarrier.h:61
Win32::Win32ThreadBarrier::Wait
void Wait()
call after Arrive() returns false to wait for other threads
Definition
win32threadbarrier.h:117
Win32::Win32ThreadBarrier::Win32ThreadBarrier
Win32ThreadBarrier()
constructor
Definition
win32threadbarrier.h:48
Win32::Win32ThreadBarrier::numThreads
long numThreads
Definition
win32threadbarrier.h:38
Win32::Win32ThreadBarrier::SignalContinue
void SignalContinue()
call after Arrive() returns true to resume all threads
Definition
win32threadbarrier.h:135
Win32::Win32ThreadBarrier::Setup
void Setup(SizeT numThreads)
setup the object with the number of threads
Definition
win32threadbarrier.h:71
Win32::Win32ThreadBarrier::event
HANDLE event
Definition
win32threadbarrier.h:40
Win32::Win32ThreadBarrier::isValid
bool isValid
Definition
win32threadbarrier.h:41
Win32::Win32ThreadBarrier::critSect
Threading::CriticalSection critSect
Definition
win32threadbarrier.h:37
Win32::Win32ThreadBarrier::Arrive
bool Arrive()
enter thread barrier, return false if not all threads have arrived yet
Definition
win32threadbarrier.h:98
Win32::Win32ThreadBarrier::IsValid
bool IsValid() const
return true if the object has been setup
Definition
win32threadbarrier.h:83
Win32::Win32ThreadBarrier::outstandingThreads
volatile long outstandingThreads
Definition
win32threadbarrier.h:39
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
CoreGraphics::CreateEvent
EventId CreateEvent(const EventCreateInfo &info)
create new event
Definition
vkevent.cc:42
Win32
[TODO: Describe Win32 subsystem]
types.h
SizeT
int SizeT
Definition
types.h:42
code
foundation
threading
win32
win32threadbarrier.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.