Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
linuxcompletioncounter.h
Go to the documentation of this file.
1
#pragma once
2
//----------------------------------------------------------------------------------------
11
#include "
core/types.h
"
12
13
//----------------------------------------------------------------------------------------
14
namespace
Linux
15
{
16
class
LinuxCompletionCounter
17
{
18
public
:
20
LinuxCompletionCounter
();
22
~LinuxCompletionCounter
();
23
25
void
Reset
(
int
count);
27
bool
Decrement
(
int
num);
29
void
Wait
();
31
bool
Peek
();
32
33
private
:
34
volatile
int
curCount
;
35
pthread_cond_t
completionEvent
;
36
};
37
38
//----------------------------------------------------------------------------------------
41
inline
42
LinuxCompletionCounter::LinuxCompletionCounter
() :
43
curCount
(0)
44
{
45
// create a manual-reset event, and initially in signalled state
46
pthread_cond_init(&this->
completionEvent
, 0);
47
}
48
49
//----------------------------------------------------------------------------------------
52
inline
53
LinuxCompletionCounter::~LinuxCompletionCounter
()
54
{
55
pthread_cond_destroy(&this->
completionEvent
);
56
}
57
58
//----------------------------------------------------------------------------------------
64
inline
void
65
LinuxCompletionCounter::Reset
(
int
count)
66
{
67
n_assert
(count > 0);
68
n_assert
(0 == this->
curCount
);
69
this->
curCount
= count;
70
}
71
72
//----------------------------------------------------------------------------------------
78
inline
bool
79
LinuxCompletionCounter::Decrement
(
int
num)
80
{
81
if
(LinuxInterlocked::Add(this->
curCount
, -num) == num)
82
{
83
pthread_cond_signal(&this->
completionEvent
);
84
return
true
;
85
}
86
else
87
{
88
return
false
;
89
}
90
}
91
92
//----------------------------------------------------------------------------------------
98
inline
void
99
LinuxCompletionCounter::Wait
()
100
{
101
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
102
pthread_mutex_lock(&m);
103
while
(this->
curCount
> 0)
104
{
105
pthread_cond_wait(&this->
completionEvent
, &m);
106
}
107
pthread_mutex_unlock(&m);
108
pthread_mutex_destroy(&m);
109
}
110
111
//----------------------------------------------------------------------------------------
115
inline
bool
116
LinuxCompletionCounter::Peek
()
117
{
118
return
(0 == this->
curCount
);
119
}
120
121
}
// namespace Linux
122
//----------------------------------------------------------------------------------------
Linux::LinuxCompletionCounter::Reset
void Reset(int count)
reset the counter, call from main thread
Definition
linuxcompletioncounter.h:65
Linux::LinuxCompletionCounter::Wait
void Wait()
wait until counter has reached 0, call from main thread or worker threads
Definition
linuxcompletioncounter.h:99
Linux::LinuxCompletionCounter::Peek
bool Peek()
check if the counter has reached 0
Definition
linuxcompletioncounter.h:116
Linux::LinuxCompletionCounter::curCount
volatile int curCount
Definition
linuxcompletioncounter.h:34
Linux::LinuxCompletionCounter::~LinuxCompletionCounter
~LinuxCompletionCounter()
destructor
Definition
linuxcompletioncounter.h:53
Linux::LinuxCompletionCounter::Decrement
bool Decrement(int num)
decrement the counter, call from worker threads, return true if count has reached zero
Definition
linuxcompletioncounter.h:79
Linux::LinuxCompletionCounter::LinuxCompletionCounter
LinuxCompletionCounter()
constructor
Definition
linuxcompletioncounter.h:42
Linux::LinuxCompletionCounter::completionEvent
pthread_cond_t completionEvent
Definition
linuxcompletioncounter.h:35
n_assert
#define n_assert(exp)
Definition
debug.h:50
Linux
Definition
linuxcompletioncounter.h:15
types.h
code
foundation
threading
linux
linuxcompletioncounter.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.