Nebula
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//------------------------------------------------------------------------------
15namespace Linux
16{
18{
19public:
21 LinuxEvent(bool manualReset=false);
27 void Signal();
29 void Wait() const;
31 bool WaitTimeout(int ms) const;
33 bool Peek() const;
35 void Reset();
37 bool IsManual() const;
38
39private:
40 // emulate windows event behaviour (*sigh*)
42 {
46 };
47
48 mutable pthread_mutex_t mutex;
49 mutable pthread_cond_t cond;
50
52 volatile mutable EventStatus status;
53};
54
55//------------------------------------------------------------------------------
58inline
60 : manualReset(manual)
61 , status(SIGNAL_NONE)
62{
63 // setup the mutex
64 int res = pthread_mutex_init(&this->mutex, nullptr);
65 n_assert(0 == res);
66
67 // setup the condition variable
68 res = pthread_cond_init(&this->cond, nullptr);
69 n_assert(0 == res);
70}
71
72//------------------------------------------------------------------------------
75inline
77{
78 int res = pthread_cond_destroy(&this->cond);
79 n_assert(0 == res);
80 res = pthread_mutex_destroy(&this->mutex);
81 n_assert(0 == res);
82
83 this->mutex = ev.mutex;
84 this->cond = ev.cond;
85 this->manualReset = ev.manualReset;
86 this->status = ev.status;
87 ev.mutex = pthread_mutex_t{};
88 ev.cond = pthread_cond_t{};
89 ev.status = SIGNAL_NONE;
90}
91
92//------------------------------------------------------------------------------
95inline
97{
98 int res = pthread_cond_destroy(&this->cond);
99 n_assert(0 == res);
100 res = pthread_mutex_destroy(&this->mutex);
101 n_assert(0 == res);
102}
103
104//------------------------------------------------------------------------------
107inline void
109{
110 pthread_mutex_lock(&this->mutex);
111 if(this->manualReset)
112 {
113 this->status = SIGNAL_ALL;
114 pthread_cond_broadcast(&this->cond);
115 }
116 else
117 {
118 this->status = SIGNAL_ONE;
119 pthread_cond_signal(&this->cond);
120 }
121 pthread_mutex_unlock(&this->mutex);
122}
123
124//------------------------------------------------------------------------------
127inline void
129{
130 pthread_mutex_lock(&this->mutex);
131
132 bool locked = false;
133 do
134 {
135 // dont wait for cond if already triggered
136 if (this->status == SIGNAL_ONE)
137 {
138 this->status = SIGNAL_NONE;
139 locked = true;
140 }
141 else if( this->status == SIGNAL_ALL)
142 {
143 // we are in a manual reset event, do nothing
144 locked = true;
145 }
146 else
147 {
148 // we actually have to wait
149 int res = pthread_cond_wait(&this->cond, &this->mutex);
150 n_assert(res == 0);
151 }
152 } while (!locked);
153 pthread_mutex_unlock(&this->mutex);
154}
155
156//------------------------------------------------------------------------------
159inline bool
161{
162 bool timeOutOccured = false;
163 pthread_mutex_lock(&this->mutex);
164
165 bool locked = false;
166 do
167 {
168 // dont wait for cond if already triggered
169 if (this->status == SIGNAL_ONE)
170 {
171 this->status = SIGNAL_NONE;
172 locked = true;
173 }
174 else if( this->status == SIGNAL_ALL)
175 {
176 // we are in a manual reset event, do nothing
177 locked = true;
178 }
179 else
180 {
181 timespec timeSpec;
182 timeSpec.tv_sec = ms / 1000;
183 timeSpec.tv_nsec = (ms - timeSpec.tv_sec * 1000) * 1000000;
184
185 int res = pthread_cond_timedwait(&this->cond, &this->mutex, &timeSpec);
186
187 if (ETIMEDOUT == res)
188 {
189 timeOutOccured = true;
190 }
191 }
192 } while(!locked && !timeOutOccured);
193 pthread_mutex_unlock(&this->mutex);
194 return !timeOutOccured;
195}
196
197//------------------------------------------------------------------------------
200inline bool
202{
203 return this->status != SIGNAL_NONE;
204}
205
206//------------------------------------------------------------------------------
209inline void
211{
212 pthread_mutex_lock(&this->mutex);
213 this->status = SIGNAL_NONE;
214 pthread_mutex_unlock(&this->mutex);
215}
216
217//------------------------------------------------------------------------------
220inline bool
222{
223 return this->manualReset;
224}
225
226} // namespace Linux
227//------------------------------------------------------------------------------
228
Linux implementation of Event.
Definition linuxevent.h:18
void Reset()
manually reset the event
Definition linuxevent.h:210
bool WaitTimeout(int ms) const
wait for the event with timeout in millisecs, resets the event
Definition linuxevent.h:160
bool manualReset
Definition linuxevent.h:51
EventStatus
Definition linuxevent.h:42
@ SIGNAL_ALL
Definition linuxevent.h:45
@ SIGNAL_ONE
Definition linuxevent.h:44
@ SIGNAL_NONE
Definition linuxevent.h:43
pthread_mutex_t mutex
Definition linuxevent.h:48
bool Peek() const
check if event is signaled
Definition linuxevent.h:201
void Signal()
signal the event
Definition linuxevent.h:108
void Wait() const
wait for the event to become signalled, resets the event
Definition linuxevent.h:128
pthread_cond_t cond
Definition linuxevent.h:49
bool IsManual() const
Returns true if event is manually reset.
Definition linuxevent.h:221
~LinuxEvent()
destructor
Definition linuxevent.h:96
volatile EventStatus status
Definition linuxevent.h:52
LinuxEvent(bool manualReset=false)
constructor
Definition linuxevent.h:59
#define n_assert(exp)
Definition debug.h:50
Definition linuxcompletioncounter.h:15