forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunLoopObserver.h
More file actions
129 lines (111 loc) · 3.77 KB
/
RunLoopObserver.h
File metadata and controls
129 lines (111 loc) · 3.77 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <atomic>
#include <functional>
#include <memory>
namespace facebook {
namespace react {
/*
* A cross-platform abstraction for observing a run loop life cycle.
*/
class RunLoopObserver {
public:
using Unique = std::unique_ptr<RunLoopObserver const>;
/*
* The concept of an owner.
* A run loop observer normally observes a run loop running on a different
* thread. That implies that this other thread (the run loop thread) will call
* methods of this class owned by some other thread. To make it safe, we need
* to ensure that at the moment of the calling the observer still exists. To
* do so, we use an owner object (a weak pointer) that must retain (possibly
* indirectly) the observer. The platform-specific code should convert the
* weak pointer (owner) to a strong one right before calling the observer,
* ensuring the safety of calling; right after the call, the strong pointer
* should be safely released.
*
* Note, in the case when the pointer to the actual owner will be available
* later, only after calling the constructor of the class, the caller can
* create a dummy pointer beforehand and then merge it (using
* `shared_ptr<X>(shared_ptr<Y> const &, X *)`) with the actual one (sharing
* the control block).
*/
using Owner = std::shared_ptr<void const>;
using WeakOwner = std::weak_ptr<void const>;
/*
* Run loop activity stages which run loop observers can be observe.
*/
enum Activity : int32_t {
None = 0,
BeforeWaiting = 1 << 0,
AfterWaiting = 1 << 1,
};
/*
* A delegate interface.
*/
class Delegate {
public:
/*
* Called on every run loop tick at moments corresponding to requested
* activities.
*
* A platform-specific implementation guarantees that the owner pointer
* is retained during this call.
* Will be called on the thread associated with the run loop.
*/
virtual void activityDidChange(Delegate const *delegate, Activity activity)
const noexcept = 0;
virtual ~Delegate() noexcept = default;
};
using Factory = std::function<std::unique_ptr<RunLoopObserver>(
Activity activities,
WeakOwner const &owner)>;
/*
* Constructs a run loop observer.
*/
RunLoopObserver(Activity activities, WeakOwner const &owner) noexcept;
virtual ~RunLoopObserver() noexcept = default;
/*
* Sets the delegate.
* Must be called just once.
*/
void setDelegate(Delegate const *delegate) const noexcept;
/*
* Enables or disables run loop observing.
* It can be used to save CPU cycles during periods of time when observing
* is not required.
* A newly constructed run time observer is initially disabled.
*/
void enable() const noexcept;
void disable() const noexcept;
/*
* Returns true if called on a thread associated with the run loop.
* Must be implemented in subclasses.
*/
virtual bool isOnRunLoopThread() const noexcept = 0;
/*
* Returns an owner associated with the observer.
* It might be useful to ensure the safety of consequent asynchronous calls.
*/
WeakOwner getOwner() const noexcept;
protected:
/*
* Must be implemented in subclasses.
*/
virtual void startObserving() const noexcept = 0;
virtual void stopObserving() const noexcept = 0;
/*
* Called by subclasses to generate a call on a delegate.
*/
void activityDidChange(Activity activity) const noexcept;
Activity const activities_{};
WeakOwner const owner_;
mutable Delegate const *delegate_{nullptr};
mutable std::atomic<bool> enabled_{false};
};
} // namespace react
} // namespace facebook