Nebula
Loading...
Searching...
No Matches
posixevent.h
Go to the documentation of this file.
1#pragma once
2#ifndef POSIX_POSIXEVENT_H
3#define POSIX_POSIXEVENT_H
4//------------------------------------------------------------------------------
13#include "core/types.h"
14#include <semaphore.h>
15
16//------------------------------------------------------------------------------
17namespace Posix
18{
20{
21public:
23 PosixEvent(bool manualReset=false);
29 void Signal();
31 void Reset();
33 void Wait() const;
35 bool WaitTimeout(int ms) const;
37 bool Peek() const;
39 bool IsManual() const;
40
41private:
42 bool manual;
43 sem_t* semaphore;
44};
45
46//------------------------------------------------------------------------------
50inline
51PosixEvent::PosixEvent(bool manualReset)
52 : semaphore(0)
53 , manual(manualReset)
54{
55 this->semaphore = new sem_t;
56 sem_init(this->semaphore, 0, 0);
57}
58
59//------------------------------------------------------------------------------
62inline
64{
65 this->semaphore = ev.semaphore;
66 this->manual = ev.semaphore;
67 ev.semaphore = nullptr;
68}
69
70//------------------------------------------------------------------------------
73inline
75{
76 sem_destroy(this->semaphore);
77 delete this->semaphore;
78 this->semaphore = 0;
79}
80
81//------------------------------------------------------------------------------
84inline void
86{
87 sem_post(this->semaphore);
88}
89
90//------------------------------------------------------------------------------
93inline void
95{
96 // do nothing, not required for POSIX events
97}
98
99//------------------------------------------------------------------------------
102inline void
104{
105 sem_wait(this->semaphore);
106}
107
108//------------------------------------------------------------------------------
115inline bool
116PosixEvent::WaitTimeout(int timeoutInMilliSec) const
117{
118 while (timeoutInMilliSec > 0)
119 {
120 if (0 == sem_trywait(this->semaphore))
121 {
122 return true;
123 }
124 usleep(1000);
125 timeoutInMilliSec -= 1;
126 }
127 return false;
128}
129
130//------------------------------------------------------------------------------
134inline bool
136{
137 return 0 == sem_trywait(this->semaphore);
138}
139
140//------------------------------------------------------------------------------
143inline bool
145{
146 return this->manual;
147}
148
149}; // namespace Posix
150//------------------------------------------------------------------------------
151#endif
152
Posix implmentation of an event synchronization object.
Definition posixevent.h:20
void Signal()
signal the event
Definition posixevent.h:85
void Wait() const
wait for the event to become signalled
Definition posixevent.h:103
bool WaitTimeout(int ms) const
wait for the event with timeout in millisecs
Definition posixevent.h:116
bool IsManual() const
Returns true if event is manually reset.
Definition posixevent.h:144
bool manual
Definition posixevent.h:42
bool Peek() const
check if event is signalled
Definition posixevent.h:135
PosixEvent(bool manualReset=false)
constructor
Definition posixevent.h:51
sem_t * semaphore
Definition posixevent.h:43
void Reset()
reset the event (only if manual reset)
Definition posixevent.h:94
~PosixEvent()
destructor
Definition posixevent.h:74
Posix implemention of a read-many write-few lock.
Definition posixsysfunc.cc:21
A semaphore is an inter-GPU queue synchronization primitive.