forked from khuttun/PolyM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.hpp
More file actions
66 lines (53 loc) · 1.55 KB
/
Queue.hpp
File metadata and controls
66 lines (53 loc) · 1.55 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
#ifndef POLYM_QUEUE_HPP
#define POLYM_QUEUE_HPP
#include "Msg.hpp"
#include <memory>
namespace PolyM {
/** Msg ID for timeout message */
const unsigned long long MSG_TIMEOUT = 1000;
/**
* Queue is a thread-safe message queue.
* It supports one-way messaging and request-response pattern.
*/
class Queue
{
public:
Queue();
~Queue();
/**
* Put Msg to the end of the queue.
*
* @param msg Msg to put to the queue.
*/
void put(Msg&& msg);
/**
* Get message from the head of the queue.
* Blocks until at least one message is available in the queue, or until timeout happens.
* If get() returns due to timeout, the returned Msg will have Msg ID MSG_TIMEOUT.
*
* @param timeoutMillis How many ms to wait for message until timeout happens.
* 0 = wait indefinitely.
*/
std::unique_ptr<Msg> get(int timeoutMillis = 0);
/**
* Make a request.
* Call will block until response is given with respondTo().
*
* @param msg Request message. Is put to the queue so it can be retrieved from it with get().
*/
std::unique_ptr<Msg> request(Msg&& msg);
/**
* Respond to a request previously made with request().
*
* @param reqUid Msg UID of the request message.
* @param responseMsg Response message. The requester will receive it as the return value of
* request().
*/
void respondTo(MsgUID reqUid, Msg&& responseMsg);
int size();
private:
class Impl;
std::unique_ptr<Impl> impl_;
};
}
#endif