1 | #pragma once |
2 | |
3 | #include <mbgl/util/optional.hpp> |
4 | |
5 | #include <memory> |
6 | #include <mutex> |
7 | #include <queue> |
8 | |
9 | namespace mbgl { |
10 | |
11 | class Scheduler; |
12 | class Message; |
13 | |
14 | class Mailbox : public std::enable_shared_from_this<Mailbox> { |
15 | public: |
16 | |
17 | // Create a "holding" mailbox, messages to which will remain queued, |
18 | // unconsumed, until the mailbox is associated with a Scheduler using |
19 | // start(). This allows a Mailbox object to be created on one thread and |
20 | // later transferred to a different target thread that may not yet exist. |
21 | Mailbox(); |
22 | |
23 | Mailbox(Scheduler&); |
24 | |
25 | // Attach the given scheduler to this mailbox and begin processing messages |
26 | // sent to it. The mailbox must be a "holding" mailbox, as created by the |
27 | // default constructor Mailbox(). |
28 | void open(Scheduler& scheduler_); |
29 | void close(); |
30 | |
31 | bool isOpen() const; |
32 | |
33 | void push(std::unique_ptr<Message>); |
34 | void receive(); |
35 | |
36 | static void maybeReceive(std::weak_ptr<Mailbox>); |
37 | |
38 | private: |
39 | optional<Scheduler*> scheduler; |
40 | |
41 | std::recursive_mutex receivingMutex; |
42 | std::mutex pushingMutex; |
43 | |
44 | bool closed { false }; |
45 | |
46 | std::mutex queueMutex; |
47 | std::queue<std::unique_ptr<Message>> queue; |
48 | }; |
49 | |
50 | } // namespace mbgl |
51 | |