-
Notifications
You must be signed in to change notification settings - Fork 17
/
DispatchQueue.h
245 lines (209 loc) · 7.98 KB
/
DispatchQueue.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include "DispatchThunk.h"
#include <list>
#include <queue>
#include MUTEX_HEADER
#include RVALUE_HEADER
#include MEMORY_HEADER
class DispatchQueue;
/// <summary>
/// Thrown when a dispatch operation was aborted
/// </summary>
class dispatch_aborted_exception:
public std::exception
{
public:
dispatch_aborted_exception(void);
virtual ~dispatch_aborted_exception(void);
};
/// <summary>
/// This is an asynchronous queue of zero-argument functions
/// </summary>
/// <remarks>
/// A DispatchQueue is a type of event receiver which allows for the reception of deferred events.
/// </remarks>
class DispatchQueue {
public:
DispatchQueue(void);
/// <summary>
/// Runs down the dispatch queue without calling anything
/// </summary>
/// <remarks>
/// Nothing in the destructor is synchronized. This is done under the assumption that multi-
/// access during teardown is impossible.
/// </remarks>
virtual ~DispatchQueue(void);
protected:
// The maximum allowed number of pended dispatches before pended calls start getting dropped
size_t m_dispatchCap;
// The dispatch queue proper:
std::list<DispatchThunkBase*> m_dispatchQueue;
// Priority queue of non-ready events:
std::priority_queue<DispatchThunkDelayed> m_delayedQueue;
// A lock held when the dispatch queue must be updated:
std::mutex m_dispatchLock;
// Notice when the dispatch queue has been updated:
std::condition_variable m_queueUpdated;
bool m_aborted;
/// <summary>
/// Recommends a point in time to wake up to check for events
/// </summary>
std::chrono::steady_clock::time_point SuggestSoonestWakeupTimeUnsafe(std::chrono::steady_clock::time_point latestTime) const;
/// <summary>
/// Moves all ready events from the delayed queue into the dispatch queue
/// </summary>
void PromoteReadyEventsUnsafe(void);
/// <summary>
/// Similar to DispatchEvent, except assumes that the dispatch lock is currently held
/// </summary>
/// <param name="lk">A lock on m_dispatchLock</param>
/// <remarks>
/// This method assumes that the dispatch lock is held and that m_aborted is false. It
/// is an error to call this method without those preconditions met.
/// </remarks>
void DispatchEventUnsafe(std::unique_lock<std::mutex>& lk);
/// <summary>
/// Utility virtual, called whenever a new event is deferred
/// </summary>
/// <remarks>
/// The recipient of this call will be running in an arbitrary thread context while holding the dispatch
/// lock. The queue is guaranteed to contain at least one element, and may potentially contain more. The
/// caller MUST NOT attempt to pend any more events during this call, or a deadlock could occur.
/// </remarks>
virtual void OnPended(std::unique_lock<std::mutex>&& lk) {}
/// <summary>
/// Attaches an element to the end of the dispatch queue without any checks.
/// </summary>
template<class _Fx>
void Pend(_Fx&& fx) {
std::unique_lock<std::mutex> lk(m_dispatchLock);
m_dispatchQueue.push_back(new DispatchThunk<_Fx>(fx));
m_queueUpdated.notify_all();
OnPended(std::move(lk));
}
public:
/// <returns>
/// True if there are curerntly any dispatchers ready for execution--IE, DispatchEvent would return true
/// </returns>
bool AreAnyDispatchersReady(void) const { return !m_dispatchQueue.empty(); }
/// <returns>
/// The total number of all ready and delayed events
/// </returns>
size_t GetDispatchQueueLength(void) const {return m_dispatchQueue.size() + m_delayedQueue.size();}
/// <summary>
/// Causes the current dispatch queue to be dumped if it's non-empty
/// </summary>
/// <remarks>
/// This method should only be called if a non-graceful termination is desired. In this case, the dispatch
/// queue will be immediately cleared and any subsequent calls to WaitForEvent or DispatchEvent will throw
/// a dispatch_aborted_exception.
///
/// Callers who are willing to allow the dispatch queue to be fully processed should call Rundown instead.
///
/// This method is idempotent
/// </remarks>
void Abort(void);
protected:
/// <summary>
/// Updates the upper bound on the number of allowed pending dispatchers
/// </summary>
void SetDispatcherCap(size_t dispatchCap) { m_dispatchCap = dispatchCap; }
/// <summary>
/// Similar to WaitForEvent, but does not block
/// </summary>
/// <returns>True if an event was dispatched, false if the queue was empty when checked</returns>
bool DispatchEvent(void);
/// <summary>
/// Similar to DispatchEvent, but will attempt to dispatch all events currently queued
/// </summary>
/// <returns>The total number of events dispatched</returns>
int DispatchAllEvents(void) {
int retVal = 0;
while(DispatchEvent())
retVal++;
return retVal;
}
public:
/// <summary>
/// Explicit overload for already-constructed dispatch thunk types
/// </summary>
void AddExisting(DispatchThunkBase* pBase) {
std::unique_lock<std::mutex> lk(m_dispatchLock);
if(m_dispatchQueue.size() >= m_dispatchCap)
return;
m_dispatchQueue.push_back(pBase);
m_queueUpdated.notify_all();
OnPended(std::move(lk));
}
class DispatchThunkDelayedExpression {
public:
DispatchThunkDelayedExpression(DispatchQueue* pParent, std::chrono::steady_clock::time_point wakeup) :
m_pParent(pParent),
m_wakeup(wakeup)
{}
private:
DispatchQueue* m_pParent;
std::chrono::steady_clock::time_point m_wakeup;
public:
template<class _Fx>
void operator,(_Fx&& fx) {
// Let the parent handle this one directly after composing a delayed dispatch thunk r-value
*m_pParent += DispatchThunkDelayed(
m_wakeup,
new DispatchThunk<_Fx>(std::forward<_Fx>(fx))
);
}
};
/// <summary>
/// Overload for the introduction of a delayed dispatch thunk
/// </summary>
template<class Rep, class Period>
DispatchThunkDelayedExpression operator+=(std::chrono::duration<Rep, Period> rhs) {
// Verify that the duration is at least microseconds. If you're getting an assertion here, try
// using std::duration_cast<std::chrono::microseconds>(duration)
static_assert(
Period::num / Period::den <= 1000000,
"Dispatch queues cannot be used to describe intervals less than one microseconds in duration"
);
std::chrono::steady_clock::time_point timepoint = std::chrono::steady_clock::now() + rhs;
return *this += timepoint;
}
/// <summary>
/// Overload for absolute-time based delayed dispatch thunk
/// </summary>
DispatchThunkDelayedExpression operator+=(std::chrono::steady_clock::time_point rhs) {
return DispatchThunkDelayedExpression(this, rhs);
}
/// <summary>
/// Directly pends a delayed dispatch thunk
/// </summary>
/// <remarks>
/// This overload will always succeed and does not consult the dispatch cap
/// </remarks>
void operator+=(DispatchThunkDelayed&& rhs) {
std::lock_guard<std::mutex> lk(m_dispatchLock);
m_delayedQueue.push(std::forward<DispatchThunkDelayed>(rhs));
if(
m_delayedQueue.top().GetReadyTime() == rhs.GetReadyTime() &&
m_dispatchQueue.empty()
)
// We're becoming the new next-to-execute entity, dispatch queue currently empty, trigger wakeup
// so our newly pended delay thunk is eventually processed.
m_queueUpdated.notify_all();
}
/// <summary>
/// Generic overload which will pend an arbitrary dispatch type
/// </summary>
template<class _Fx>
void operator+=(_Fx&& fx) {
static_assert(!std::is_base_of<DispatchThunkBase, _Fx>::value, "Overload resolution malfunction, must not doubly wrap a dispatch thunk");
static_assert(!std::is_pointer<_Fx>::value, "Cannot pend a pointer to a function, we must have direct ownership");
std::unique_lock<std::mutex> lk(m_dispatchLock);
if(m_dispatchQueue.size() >= m_dispatchCap)
return;
m_dispatchQueue.push_back(new DispatchThunk<_Fx>(std::forward<_Fx>(fx)));
m_queueUpdated.notify_all();
OnPended(std::move(lk));
}
};