-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathMessageQueue.h
More file actions
482 lines (392 loc) · 13.7 KB
/
MessageQueue.h
File metadata and controls
482 lines (392 loc) · 13.7 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* Tencent is pleased to support the open source community by making ScriptX available.
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <cstring>
#include <deque>
#include <functional>
#include <limits>
#include <mutex>
#include <stdexcept>
#include <vector>
#include "../foundation.h"
#include "MemoryPool.hpp"
namespace script::utils {
struct alignas(std::max_align_t) ArbitraryData {
/** arbitrary data */
int64_t data0 = 0;
int64_t data1 = 0;
int64_t data2 = 0;
int64_t data3 = 0;
/** arbitrary pointer */
void* ptr0 = nullptr;
void* ptr1 = nullptr;
void* ptr2 = nullptr;
void* ptr3 = nullptr;
ArbitraryData() = default;
ArbitraryData(const ArbitraryData& copy) { *this = copy; }
ArbitraryData& operator=(const ArbitraryData&);
SCRIPTX_DISALLOW_MOVE(ArbitraryData);
~ArbitraryData() = default;
};
inline ArbitraryData& ArbitraryData::operator=(const ArbitraryData& copy) {
// copy ArbitraryData byte-by-byte
// since is may be used as a chunk of memory to support placement new.
std::memcpy(static_cast<void*>(this), ©, sizeof(copy)); // NOLINT
return *this;
}
// because ArbitraryData is used as memory chunk,
// its better be standard layout
static_assert(std::is_standard_layout_v<ArbitraryData>);
class InplaceMessage;
/**
* plain message used to post, contains only int or pointer types.
*/
class Message : public ArbitraryData {
public:
// We don't want the MessageProc to have capture(s),
// which may cause memory allocation.
// We just want caller store all context data inside message itself.
// So a std::function is not applied here for now.
// using MessageProc = std::function<void(const Message&)>;
/**
* the function type for handleMessage and cleanupMessage.
*/
using MessageProc = void(Message&);
protected:
std::chrono::nanoseconds dueTime = std::chrono::nanoseconds(0);
int32_t messageId = 0;
MessageProc* handlerProc;
MessageProc* cleanupProc;
public:
/**
* Message priority: Messages are ordered according to due-time in the queue.
* However, of the same due-time messages, higher priority messages are
* ordered in the front (be executed first).
*
* default priority is 0, smaller number has higher priority.
*/
int32_t priority = 0;
/** arbitrary message type */
int32_t what = 0;
void* tag = nullptr;
/** name of this message, used for debug purpose */
const char* name = nullptr;
private:
Message();
public:
/**
* @param handlerProc routine to handle the message
* @param cleanupProc routine to clean resources related in message.
* please don't do cleanup directly in handlerProc,
* because shutdownNow() would require messages to be cleared without being handles.
*
* inplaceObject function can't be called with this constructor.
*
* note: you can do nearly everything inside handlerProc (except awaitTermination),
* however in the cleanupProc, be careful, not to call post or removeMessage.
*/
Message(MessageProc* handlerProc, MessageProc* cleanupProc);
MessageProc* getHandlerProc() const;
MessageProc* getCleanupProc() const;
private:
void performCleanup();
void handle();
bool due() const;
bool due(std::chrono::nanoseconds now) const;
friend class MessageQueue;
friend class MemoryPool<Message>;
friend class InplaceMessage;
};
class InplaceMessage : public Message {
public:
using HandlerPorc = void(InplaceMessage&);
~InplaceMessage() = default;
SCRIPTX_DISALLOW_COPY_AND_MOVE(InplaceMessage);
/**
* create a class T in the memory of this message.
* @return reference of T
*/
template <typename T, typename... Args>
T& inplaceObject(Args&&... args) {
static_assert(sizeof(T) <= sizeof(ArbitraryData));
static_assert(std::is_destructible_v<T>);
if (cleanupProc) {
throw std::runtime_error("inplaceObject can only be called once.");
}
auto ptr = new (alignedStorage()) T(std::forward<Args>(args)...);
cleanupProc = [](Message& self) {
reinterpret_cast<InplaceMessage&>(self).getObject<T>().~T();
};
return *ptr;
}
template <typename T>
T& getObject() {
return *static_cast<T*>(alignedStorage());
}
private:
InplaceMessage() = default;
void* alignedStorage() { return static_cast<void*>(static_cast<ArbitraryData*>(this)); }
friend class MessageQueue;
friend class MemoryPool<InplaceMessage>;
};
// make sure we can cast InplaceMessage to Message and vice-versa.
static_assert(static_cast<Message*>(static_cast<InplaceMessage*>(nullptr)) == nullptr);
static_assert(static_cast<ArbitraryData*>(static_cast<InplaceMessage*>(nullptr)) ==
static_cast<ArbitraryData*>(static_cast<Message*>(nullptr)));
static_assert(sizeof(InplaceMessage) == sizeof(Message));
/**
* A MessageQueue support
* 0. schedule task
* 1. schedule task with delay
* 2. thread safe
* 3. support producer-consumer mode
* 4. support N:M produce-consumer
*/
class MessageQueue {
public:
class Supervisor {
public:
virtual ~Supervisor() = default;
protected:
virtual void beforeMessage(Message& message) = 0;
virtual void afterMessage(Message& message) = 0;
private:
friend MessageQueue;
};
private:
enum class ShutdownType { kNone, kNow, kAwaitQueue };
std::size_t maxMessageInQueue_;
MemoryPool<Message> messagePool_;
ShutdownType shutdown_;
bool interrupt_;
mutable std::mutex queueMutex_;
std::condition_variable queueNotEmptyCondition_;
std::condition_variable queueNotFullCondition_;
std::deque<Message*> queue_;
std::atomic_int32_t messageIdCounter_;
std::uint32_t workerCount_; // guard by queueMutex_
std::condition_variable workerQuitCondition_;
std::shared_ptr<Supervisor> supervisor_;
static constexpr std::size_t kDefaultPoolSize = 64;
friend class Message;
// used in the implementation
friend class LoopQueueGuard;
private:
static std::chrono::nanoseconds timestamp();
bool hasDueMessageLocked() const;
std::deque<Message*>::const_iterator findInsertPositionLocked(std::chrono::nanoseconds dueTime,
int32_t priority) const;
bool isQueueFull() const;
void awaitNotFullLocked(std::unique_lock<std::mutex>& lock);
void processMessage(Message* message);
void releaseMessage(Message* message);
void beforeMessage(Message& message);
void afterMessage(Message& message);
size_t dueMessageCount() const;
/**
* post a message to queue
* @param message
* @param delayNanos time in nano seconds for the message to delay before executing, default is 0
* @return messageId used to removeMessage, return 0 for failure (already shutdown)
*/
int32_t postMessage(Message* message, int64_t delayNanos = 0);
public:
static constexpr std::size_t kDefaultMaxMessageInQueue =
// workaround windows.h "max()" marco
(std::numeric_limits<std::size_t>::max)();
/**
* @param maxMessageInQueue if call postXXX when queue is full, will block.
*/
explicit MessageQueue(std::size_t maxMessageInQueue = kDefaultMaxMessageInQueue);
~MessageQueue();
SCRIPTX_DISALLOW_COPY_AND_MOVE(MessageQueue);
/**
* shut down the queue, after shut down, caller CAN NOT post message (would fail).
* Immediately quit the queue, the remain message in queue are CLEARED.
*
* \code
*
* if (awaitTermination == true) {
* when this method returns the loopQueue() call is guaranteed to be
* returned first,
* } else {
* this method will return immediately (issue a signal to quit the queue)
* and wait for all message to be executed (the loopQueue() call to return).
* }
*
* \endcode
*
* @param awaitTermination wait the loopQueue() call to return.
* Caller can wait for all message to be executed (the loopQueue() call to return).
*/
void shutdownNow(bool awaitTermination = false);
/**
* shut down the queue, after shut down, caller CAN still post message.
*
* @param awaitTermination If true, wait the loopQueue() call to return
* (wait for all message to be executed),
* otherwise this method will return immediately (issue a signal to quit the queue).
*
*/
void shutdown(bool awaitTermination = false);
/**
* await the loopQueue to quit (after shutdown).
*/
void awaitTermination();
/**
* @return if the MessageQueue is shut down or shutting down.
*/
bool isShutdown() const;
/**
* causing current loopQueue() call return immediately.
* and postMessage can stilled be called.
*/
void interrupt();
/**
* @param supervisor a message supervisor oversees message handling
* (both before handling and after handing)
*/
void setSupervisor(const std::shared_ptr<Supervisor>& supervisor);
/**
* @param delay a std::chrono::duration type like milliseconds nanoseconds
* @return messageId used to removeMessage, return 0 for failure (already shutdown)
* @see postMessage(const Message&, int64_t)
*
* example:
*
* \code{.cc}
* using namespace std::chrono;
* postMessage(msg, std::chrono::milliseconds(10);
* postMessage(msg, 10ms);
* postMessage(msg, 20ns);
* \endcode
*
*/
template <class Rep = int64_t, class Period = std::milli>
int32_t postMessage(const Message& message,
std::chrono::duration<Rep, Period> delay = std::chrono::milliseconds(0)) {
using std::chrono::duration_cast;
using std::chrono::nanoseconds;
auto m = messagePool_.obtain();
*m = message;
return postMessage(m, duration_cast<nanoseconds>(delay).count());
}
/**
* @return messageId used to removeMessage, return 0 for failure (already shutdown)
*/
template <class Rep = int, class Period = std::milli>
int32_t postMessage(std::unique_ptr<InplaceMessage>& message,
std::chrono::duration<Rep, Period> delay = std::chrono::milliseconds(0)) {
if (!message->cleanupProc) {
throw std::runtime_error("InplaceMessage haven't placed anything");
}
return postMessage(message.release(),
std::chrono::duration_cast<std::chrono::nanoseconds>(delay).count());
}
/**
* obtain a InplaceMessage for placement new type in message.
*/
std::unique_ptr<InplaceMessage> obtainInplaceMessage(InplaceMessage::HandlerPorc* handlerProc);
public:
/**
* how long do you want to loop
*/
enum class LoopType {
/**
* Loop and wait for up coming messages.
* only quite when interrupt() or shutdown() is called.
*/
kLoopAndWait,
/**
* run current due message and return.
*/
kLoopOnce
};
/**
* explain why the loopQueue() call is returned.
*/
enum class LoopReturnType {
/** called loopQueue() with loopType = LoopType::kLoopOnce */
kRunOnce,
/** interrupt() is called */
kInterrupt,
/** shutdown() is called */
kShutDown,
};
LoopReturnType loopQueue(LoopType loopType = LoopType::kLoopAndWait);
private:
bool checkQuitLoopNowLocked(MessageQueue::LoopType loopType, size_t onceMessageCount,
MessageQueue::LoopReturnType& returnType);
bool checkQuitLoopWhenNoDueMessageLocked(MessageQueue::LoopType loopType,
MessageQueue::LoopReturnType& returnType);
Message* awaitDueMessage(MessageQueue::LoopType loopType, size_t onceMessageCount,
MessageQueue::LoopReturnType& returnType);
public:
// removeMessage family
enum class RemoveMessagePredReturnType {
/**
* dont remove this message, and continue search for message to remove;
*/
kDontRemove,
/**
* remove this message, and don't search for any message.
*/
kRemove,
/**
* remove this message, and continue search for message to remove;
*/
kRemoveAndContinue,
};
bool removeMessageIf(const std::function<RemoveMessagePredReturnType(Message&)>& pred);
bool removeMessage(int32_t messageId) {
return removeMessageIf([messageId](const Message& msg) {
return msg.messageId == messageId ? RemoveMessagePredReturnType::kRemove
: RemoveMessagePredReturnType::kDontRemove;
});
}
/**
* @param what Message::what
* @return removed or not
*/
bool removeMessageByWhat(int32_t what) {
return removeMessageIf([what](const Message& msg) {
return msg.what == what ? RemoveMessagePredReturnType::kRemoveAndContinue
: RemoveMessagePredReturnType::kDontRemove;
});
}
bool removeMessageByTag(void* tag) {
return removeMessageIf([tag](const Message& msg) {
return msg.tag == tag ? RemoveMessagePredReturnType::kRemoveAndContinue
: RemoveMessagePredReturnType::kDontRemove;
});
}
/**
*
* @param proc
* @return removed or not
*/
bool removeMessageByHandlerProc(Message::MessageProc* proc) {
return removeMessageIf([proc](const Message& msg) {
return msg.handlerProc == proc ? RemoveMessagePredReturnType::kRemoveAndContinue
: RemoveMessagePredReturnType::kDontRemove;
});
}
};
} // namespace script::utils