Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
linuxevent.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
11
#include "
core/types.h
"
12
#include <bits/pthreadtypes.h>
13
14
//------------------------------------------------------------------------------
15
namespace
Linux
16
{
17
class
LinuxEvent
18
{
19
public
:
21
LinuxEvent
(
bool
manualReset
=
false
);
23
LinuxEvent
(
LinuxEvent
&& ev);
25
LinuxEvent
(
const
LinuxEvent
& ev) =
default
;
27
~LinuxEvent
();
29
void
Signal
();
31
void
Wait
()
const
;
33
bool
WaitTimeout
(
int
ms)
const
;
35
bool
Peek
()
const
;
37
void
Reset
();
39
bool
IsManual
()
const
;
40
42
LinuxEvent
&
operator=
(
const
LinuxEvent
& ev) =
default
;
43
44
private
:
45
// emulate windows event behaviour (*sigh*)
46
enum
EventStatus
47
{
48
SIGNAL_NONE
= 0,
49
SIGNAL_ONE
= 1,
50
SIGNAL_ALL
= 2,
51
};
52
53
mutable
pthread_mutex_t
mutex
;
54
mutable
pthread_cond_t
cond
;
55
56
bool
manualReset
;
57
volatile
mutable
EventStatus
status
;
58
};
59
60
//------------------------------------------------------------------------------
63
inline
64
LinuxEvent::LinuxEvent
(
bool
manual)
65
:
manualReset
(manual)
66
,
status
(
SIGNAL_NONE
)
67
{
68
// setup the mutex
69
int
res = pthread_mutex_init(&this->
mutex
,
nullptr
);
70
n_assert
(0 == res);
71
72
// setup the condition variable
73
res = pthread_cond_init(&this->
cond
,
nullptr
);
74
n_assert
(0 == res);
75
}
76
77
//------------------------------------------------------------------------------
80
inline
81
LinuxEvent::LinuxEvent
(
LinuxEvent
&& ev)
82
{
83
int
res = pthread_cond_destroy(&this->
cond
);
84
n_assert
(0 == res);
85
res = pthread_mutex_destroy(&this->
mutex
);
86
n_assert
(0 == res);
87
88
this->
mutex
= ev.mutex;
89
this->
cond
= ev.cond;
90
this->
manualReset
= ev.manualReset;
91
this->
status
= ev.status;
92
ev.mutex = pthread_mutex_t{};
93
ev.cond = pthread_cond_t{};
94
ev.status =
SIGNAL_NONE
;
95
}
96
97
//------------------------------------------------------------------------------
100
inline
101
LinuxEvent::~LinuxEvent
()
102
{
103
int
res = pthread_cond_destroy(&this->
cond
);
104
n_assert
(0 == res);
105
res = pthread_mutex_destroy(&this->
mutex
);
106
n_assert
(0 == res);
107
}
108
109
//------------------------------------------------------------------------------
112
inline
void
113
LinuxEvent::Signal
()
114
{
115
pthread_mutex_lock(&this->
mutex
);
116
if
(this->
manualReset
)
117
{
118
this->
status
=
SIGNAL_ALL
;
119
pthread_cond_broadcast(&this->
cond
);
120
}
121
else
122
{
123
this->
status
=
SIGNAL_ONE
;
124
pthread_cond_signal(&this->
cond
);
125
}
126
pthread_mutex_unlock(&this->
mutex
);
127
}
128
129
//------------------------------------------------------------------------------
132
inline
void
133
LinuxEvent::Wait
()
const
134
{
135
pthread_mutex_lock(&this->
mutex
);
136
137
bool
locked =
false
;
138
do
139
{
140
// dont wait for cond if already triggered
141
if
(this->
status
==
SIGNAL_ONE
)
142
{
143
this->
status
=
SIGNAL_NONE
;
144
locked =
true
;
145
}
146
else
if
( this->
status
==
SIGNAL_ALL
)
147
{
148
// we are in a manual reset event, do nothing
149
locked =
true
;
150
}
151
else
152
{
153
// we actually have to wait
154
int
res = pthread_cond_wait(&this->
cond
, &this->
mutex
);
155
n_assert
(res == 0);
156
}
157
}
while
(!locked);
158
pthread_mutex_unlock(&this->
mutex
);
159
}
160
161
//------------------------------------------------------------------------------
164
inline
bool
165
LinuxEvent::WaitTimeout
(
int
ms)
const
166
{
167
bool
timeOutOccured =
false
;
168
169
// compute the absolute deadline once, so spurious wakeups don't extend the timeout,
170
// and keep tv_nsec below one second, otherwise pthread_cond_timedwait() fails with
171
// EINVAL and we would busy-spin until the clock produces a valid deadline
172
timespec target;
173
clock_gettime(CLOCK_REALTIME, &target);
174
target.tv_sec += ms / 1000;
175
target.tv_nsec += (ms % 1000) * 1000000L;
176
if
(target.tv_nsec >= 1000000000L)
177
{
178
target.tv_sec += 1;
179
target.tv_nsec -= 1000000000L;
180
}
181
182
pthread_mutex_lock(&this->
mutex
);
183
184
bool
locked =
false
;
185
do
186
{
187
// dont wait for cond if already triggered
188
if
(this->
status
==
SIGNAL_ONE
)
189
{
190
this->
status
=
SIGNAL_NONE
;
191
locked =
true
;
192
}
193
else
if
( this->
status
==
SIGNAL_ALL
)
194
{
195
// we are in a manual reset event, do nothing
196
locked =
true
;
197
}
198
else
199
{
200
int
res = pthread_cond_timedwait(&this->
cond
, &this->
mutex
, &target);
201
if
(ETIMEDOUT == res)
202
{
203
timeOutOccured =
true
;
204
}
205
else
206
{
207
n_assert
(res == 0);
208
}
209
}
210
}
while
(!locked && !timeOutOccured);
211
pthread_mutex_unlock(&this->
mutex
);
212
return
!timeOutOccured;
213
}
214
215
//------------------------------------------------------------------------------
218
inline
bool
219
LinuxEvent::Peek
()
const
220
{
221
return
this->
status
!=
SIGNAL_NONE
;
222
}
223
224
//------------------------------------------------------------------------------
227
inline
void
228
LinuxEvent::Reset
()
229
{
230
pthread_mutex_lock(&this->
mutex
);
231
this->
status
=
SIGNAL_NONE
;
232
pthread_mutex_unlock(&this->
mutex
);
233
}
234
235
//------------------------------------------------------------------------------
238
inline
bool
239
LinuxEvent::IsManual
()
const
240
{
241
return
this->
manualReset
;
242
}
243
244
}
// namespace Linux
245
//------------------------------------------------------------------------------
246
Linux::LinuxEvent::Reset
void Reset()
manually reset the event
Definition
linuxevent.h:228
Linux::LinuxEvent::LinuxEvent
LinuxEvent(const LinuxEvent &ev)=default
copy constructor
Linux::LinuxEvent::operator=
LinuxEvent & operator=(const LinuxEvent &ev)=default
copy assignment operator
Linux::LinuxEvent::WaitTimeout
bool WaitTimeout(int ms) const
wait for the event with timeout in millisecs, resets the event
Definition
linuxevent.h:165
Linux::LinuxEvent::manualReset
bool manualReset
Definition
linuxevent.h:56
Linux::LinuxEvent::EventStatus
EventStatus
Definition
linuxevent.h:47
Linux::LinuxEvent::SIGNAL_ALL
@ SIGNAL_ALL
Definition
linuxevent.h:50
Linux::LinuxEvent::SIGNAL_ONE
@ SIGNAL_ONE
Definition
linuxevent.h:49
Linux::LinuxEvent::SIGNAL_NONE
@ SIGNAL_NONE
Definition
linuxevent.h:48
Linux::LinuxEvent::mutex
pthread_mutex_t mutex
Definition
linuxevent.h:53
Linux::LinuxEvent::Peek
bool Peek() const
check if event is signaled
Definition
linuxevent.h:219
Linux::LinuxEvent::Signal
void Signal()
signal the event
Definition
linuxevent.h:113
Linux::LinuxEvent::Wait
void Wait() const
wait for the event to become signalled, resets the event
Definition
linuxevent.h:133
Linux::LinuxEvent::cond
pthread_cond_t cond
Definition
linuxevent.h:54
Linux::LinuxEvent::IsManual
bool IsManual() const
Returns true if event is manually reset.
Definition
linuxevent.h:239
Linux::LinuxEvent::~LinuxEvent
~LinuxEvent()
destructor
Definition
linuxevent.h:101
Linux::LinuxEvent::status
volatile EventStatus status
Definition
linuxevent.h:57
Linux::LinuxEvent::LinuxEvent
LinuxEvent(bool manualReset=false)
constructor
Definition
linuxevent.h:64
n_assert
#define n_assert(exp)
Definition
debug.h:58
Linux
Definition
linuxcompletioncounter.h:15
types.h
code
foundation
threading
linux
linuxevent.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.