forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PubSub.h
110 lines (92 loc) · 3.21 KB
/
PubSub.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
/* Copyright 2016-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#pragma once
#include "watchman_system.h"
#include "watchman_string.h"
#include <folly/Synchronized.h>
#include "thirdparty/jansson/jansson.h"
#include <deque>
#include <functional>
#include <vector>
namespace watchman {
class Publisher : public std::enable_shared_from_this<Publisher> {
public:
struct Item {
// copy of nextSerial_ at the time this was created.
// The item can be released when all subscribers have
// observed this serial number.
uint64_t serial;
json_ref payload;
};
// Generic callback that subscribers can register to arrange
// to be woken up when something is published
using Notifier = std::function<void()>;
// Each subscriber is represented by one of these
class Subscriber : public std::enable_shared_from_this<Subscriber> {
// The serial of the last Item to be consumed by
// this subscriber.
uint64_t serial_;
// Subscriber keeps the publisher alive so that no Items are lost
// if the Publisher is released before all of the subscribers.
std::shared_ptr<Publisher> publisher_;
// Advising the subscriber that there may be more items available
Notifier notify_;
// Information for debugging purposes
const json_ref info_;
public:
~Subscriber();
Subscriber(
std::shared_ptr<Publisher> pub,
Notifier notify,
const json_ref& info);
Subscriber(const Subscriber&) = delete;
// Returns all as yet unseen published items for this subscriber.
void getPending(std::vector<std::shared_ptr<const Item>>& pending);
inline uint64_t getSerial() const {
return serial_;
}
inline Notifier& getNotify() {
return notify_;
}
inline const json_ref& getInfo() {
return info_;
}
};
// Register a new subscriber.
// When the Subscriber object is released, the registration is
// automatically removed.
std::shared_ptr<Subscriber> subscribe(
Notifier notify,
const json_ref& info = nullptr);
// Returns true if there are any subscribers.
// This is racy and intended to be used to gate building a payload
// if there are no current subscribers.
bool hasSubscribers() const;
// Enqueue a new item, but only if there are subscribers.
// Returns true if the item was queued.
bool enqueue(json_ref&& payload);
// Return debugging info useful for state inspection.
json_ref getDebugInfo() const;
private:
struct state {
state() = default;
state(const state&) = delete;
// Serial number to use for the next Item
uint64_t nextSerial{1};
// The stream of Items
std::deque<std::shared_ptr<const Item>> items;
// The subscribers
std::vector<std::weak_ptr<Subscriber>> subscribers;
void collectGarbage();
void enqueue(json_ref&& payload);
};
folly::Synchronized<state> state_;
friend class Subscriber;
};
// Equivalent to calling getPending on up to two Subscriber and
// joining the resultant vectors together.
void getPending(
std::vector<std::shared_ptr<const Publisher::Item>>& pending,
const std::shared_ptr<Publisher::Subscriber>& sub1,
const std::shared_ptr<Publisher::Subscriber>& sub2);
} // namespace watchman