Nebula
Loading...
Searching...
No Matches
win32threadbarrier.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/types.h"
14
15//------------------------------------------------------------------------------
16namespace Win32
17{
19{
20public:
28 bool IsValid() const;
30 bool Arrive();
32 void Wait();
34 void SignalContinue();
35
36private:
39 volatile long outstandingThreads;
40 HANDLE event;
41 bool isValid;
42};
43
44//------------------------------------------------------------------------------
47inline
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//------------------------------------------------------------------------------
60inline
62{
63 CloseHandle(this->event);
64 this->event = 0;
65}
66
67//------------------------------------------------------------------------------
70inline void
72{
73 n_assert(!this->isValid);
74 this->numThreads = num;
75 this->outstandingThreads = num;
76 this->isValid = true;
77}
78
79//------------------------------------------------------------------------------
82inline bool
84{
85 return this->isValid;
86}
87
88//------------------------------------------------------------------------------
97inline bool
99{
100 this->critSect.Enter();
101 n_assert(this->outstandingThreads > 0);
102 this->outstandingThreads--;
103 return (0 == this->outstandingThreads);
104}
105
106//------------------------------------------------------------------------------
116inline void
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//------------------------------------------------------------------------------
134inline void
136{
137 this->outstandingThreads = this->numThreads;
138 SetEvent(this->event);
139 this->critSect.Leave();
140}
141
142} // namespace Win32
143//------------------------------------------------------------------------------
144
Critical section objects are used to protect a portion of code from parallel execution.
Block until all thread have arrived at the barrier.
Definition win32threadbarrier.h:19
~Win32ThreadBarrier()
destructor
Definition win32threadbarrier.h:61
void Wait()
call after Arrive() returns false to wait for other threads
Definition win32threadbarrier.h:117
Win32ThreadBarrier()
constructor
Definition win32threadbarrier.h:48
long numThreads
Definition win32threadbarrier.h:38
void SignalContinue()
call after Arrive() returns true to resume all threads
Definition win32threadbarrier.h:135
void Setup(SizeT numThreads)
setup the object with the number of threads
Definition win32threadbarrier.h:71
HANDLE event
Definition win32threadbarrier.h:40
bool isValid
Definition win32threadbarrier.h:41
Threading::CriticalSection critSect
Definition win32threadbarrier.h:37
bool Arrive()
enter thread barrier, return false if not all threads have arrived yet
Definition win32threadbarrier.h:98
bool IsValid() const
return true if the object has been setup
Definition win32threadbarrier.h:83
volatile long outstandingThreads
Definition win32threadbarrier.h:39
void __cdecl n_printf(const char *msg,...)
Nebula's printf replacement.
Definition debug.cc:209
#define n_assert(exp)
Definition debug.h:50
EventId CreateEvent(const EventCreateInfo &info)
create new event
Definition vkevent.cc:42
[TODO: Describe Win32 subsystem]
int SizeT
Definition types.h:49