summaryrefslogtreecommitdiff
path: root/include/mbgl/actor/mailbox.hpp
blob: 23c579917a248ace848512f6153b56316242d338 (plain)
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
#pragma once

#include <mbgl/util/optional.hpp>

#include <memory>
#include <mutex>
#include <queue>

namespace mbgl {

class Scheduler;
class Message;

class Mailbox : public std::enable_shared_from_this<Mailbox> {
public:
   
    // Create a "holding" mailbox, messages to which will remain queued,
    // unconsumed, until the mailbox is associated with a Scheduler using
    // start(). This allows a Mailbox object to be created on one thread and
    // later transferred to a different target thread that may not yet exist.
    Mailbox();
    
    Mailbox(Scheduler&);

    // Attach the given scheduler to this mailbox and begin processing messages
    // sent to it. The mailbox must be a "holding" mailbox, as created by the
    // default constructor Mailbox().
    void open(Scheduler& scheduler_);
    void close();

    bool isOpen() const;

    void push(std::unique_ptr<Message>);
    void receive();

    static void maybeReceive(std::weak_ptr<Mailbox>);

private:
    optional<Scheduler*> scheduler;

    std::recursive_mutex receivingMutex;
    std::mutex pushingMutex;

    bool closed { false };

    std::mutex queueMutex;
    std::queue<std::unique_ptr<Message>> queue;
};

} // namespace mbgl