blob: 92bfd11fedc734ec422bf6957c00cbf313c9a204 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert, R. Belmont
//============================================================
//
// osdsync.h - Core synchronization functions
//
//============================================================
#ifndef __OSDSYNC__
#define __OSDSYNC__
// C++ headers
#include <mutex>
#include <atomic>
#include <condition_variable>
#include "osdcomm.h"
/***************************************************************************
SYNCHRONIZATION INTERFACES - Events
***************************************************************************/
#define OSD_EVENT_WAIT_INFINITE (~(osd_ticks_t)0)
/* osd_event is an opaque type which represents a setable/resetable event */
class osd_event
{
public:
/*-----------------------------------------------------------------------------
constructor: allocate a new event
Parameters:
manualreset - boolean. If true, the event will be automatically set
to non-signalled after a thread successfully waited for
it.
initialstate - boolean. If true, the event is signalled initially.
Return value:
A pointer to the allocated event.
-----------------------------------------------------------------------------*/
osd_event(int manualreset, int initialstate)
{
m_signalled = initialstate;
m_autoreset = !manualreset;
}
~osd_event()
{
}
/*-----------------------------------------------------------------------------
wait: wait for an event to be signalled
If the event is in signalled state, the
function returns immediately. If not it will wait for the event
to become signalled.
Parameters:
timeout - timeout in osd_ticks
Return value:
true: The event was signalled
false: A timeout occurred
-----------------------------------------------------------------------------*/
bool wait(osd_ticks_t timeout)
{
if (timeout == OSD_EVENT_WAIT_INFINITE)
timeout = osd_ticks_per_second() * (osd_ticks_t)10000;
std::unique_lock<std::mutex> lock(m_mutex);
if (!timeout)
{
if (!m_signalled)
{
return false;
}
}
else
{
if (!m_signalled)
{
UINT64 msec = timeout * 1000 / osd_ticks_per_second();
do {
if (m_cond.wait_for(lock, std::chrono::milliseconds(msec)) == std::cv_status::timeout)
{
if (!m_signalled)
{
return false;
}
else
break;
} else
break;
} while (true);
}
}
if (m_autoreset)
m_signalled = 0;
return true;
}
/*-----------------------------------------------------------------------------
osd_event_reset: reset an event to non-signalled state
Parameters:
None
Return value:
None
-----------------------------------------------------------------------------*/
void reset()
{
m_mutex.lock();
m_signalled = FALSE;
m_mutex.unlock();
}
/*-----------------------------------------------------------------------------
osd_event_set: set an event to signalled state
Parameters:
None
Return value:
None
Notes:
All threads waiting for the event will be signalled.
-----------------------------------------------------------------------------*/
void set()
{
m_mutex.lock();
if (m_signalled == FALSE)
{
m_signalled = TRUE;
if (m_autoreset)
m_cond.notify_one();
else
m_cond.notify_all();
}
m_mutex.unlock();
}
private:
std::mutex m_mutex;
std::condition_variable m_cond;
std::atomic<INT32> m_autoreset;
std::atomic<INT32> m_signalled;
};
#endif /* __OSDSYNC__ */
|