OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ | |
6 #define MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <map> | |
11 #include <queue> | |
12 | |
13 #include "mojo/public/cpp/bindings/callback.h" | |
14 #include "mojo/public/cpp/system/core.h" | |
15 | |
16 namespace mojo { | |
17 | |
18 class RunLoopHandler; | |
19 | |
20 // Watches handles for signals and calls event handlers when they occur. Also | |
21 // executes delayed tasks. This class should only be used by a single thread. | |
22 class RunLoop { | |
23 public: | |
24 RunLoop(); | |
25 ~RunLoop(); | |
26 | |
27 // Sets up state needed for RunLoop. This must be invoked before creating a | |
28 // RunLoop. | |
29 static void SetUp(); | |
30 | |
31 // Cleans state created by Setup(). | |
32 static void TearDown(); | |
33 | |
34 // Returns the RunLoop for the current thread. Returns null if not yet | |
35 // created. | |
36 static RunLoop* current(); | |
37 | |
38 // Registers a RunLoopHandler for the specified handle. It is an error to | |
39 // register more than one handler for a handle, and crashes the process. | |
40 // | |
41 // The handler's OnHandleReady() method is invoked after one of the signals in | |
42 // |handle_signals| occurs. Note that the handler remains registered until | |
43 // explicitly removed or an error occurs. | |
44 // | |
45 // The handler's OnHandleError() method is invoked if the deadline elapses, an | |
46 // error is detected, or the RunLoop is being destroyed. The handler is | |
47 // automatically unregistered before calling OnHandleError(), so it will not | |
48 // receive any further notifications. | |
49 void AddHandler(RunLoopHandler* handler, | |
50 const Handle& handle, | |
51 MojoHandleSignals handle_signals, | |
52 MojoDeadline deadline); | |
53 void RemoveHandler(const Handle& handle); | |
54 bool HasHandler(const Handle& handle) const; | |
55 | |
56 // Runs the loop servicing handles and tasks as they are ready. This returns | |
57 // when Quit() is invoked, or there are no more handles nor tasks. | |
58 void Run(); | |
59 | |
60 // Runs the loop servicing any handles and tasks that are ready. Does not wait | |
61 // for handles or tasks to become ready before returning. Returns early if | |
62 // Quit() is invoked. | |
63 void RunUntilIdle(); | |
64 | |
65 void Quit(); | |
66 | |
67 // Adds a task to be performed after delay has elapsed. Must be posted to the | |
68 // current thread's RunLoop. | |
69 void PostDelayedTask(const Closure& task, MojoTimeTicks delay); | |
70 | |
71 private: | |
72 struct RunState; | |
73 struct WaitState; | |
74 | |
75 // Contains the data needed to track a request to AddHandler(). | |
76 struct HandlerData { | |
77 HandlerData() | |
78 : handler(nullptr), | |
79 handle_signals(MOJO_HANDLE_SIGNAL_NONE), | |
80 deadline(0), | |
81 id(0) {} | |
82 | |
83 RunLoopHandler* handler; | |
84 MojoHandleSignals handle_signals; | |
85 MojoTimeTicks deadline; | |
86 // See description of |RunLoop::next_handler_id_| for details. | |
87 int id; | |
88 }; | |
89 | |
90 typedef std::map<Handle, HandlerData> HandleToHandlerData; | |
91 | |
92 // Used for NotifyHandlers to specify whether HandlerData's |deadline| | |
93 // should be checked prior to notifying. | |
94 enum CheckDeadline { CHECK_DEADLINE, IGNORE_DEADLINE }; | |
95 | |
96 // Mode of operation of the run loop. | |
97 enum RunMode { UNTIL_EMPTY, UNTIL_IDLE }; | |
98 | |
99 // Runs the loop servicing any handles and tasks that are ready. If | |
100 // |run_mode| is |UNTIL_IDLE|, does not wait for handles or tasks to become | |
101 // ready before returning. Returns early if Quit() is invoked. | |
102 void RunInternal(RunMode run_mode); | |
103 | |
104 // Do one unit of delayed work, if eligible. Returns true is a task was run. | |
105 bool DoDelayedWork(); | |
106 | |
107 // Waits for a handle to be ready or until the next task must be run. Returns | |
108 // after servicing at least one handle (or there are no more handles) unless | |
109 // a task must be run or |non_blocking| is true, in which case it will also | |
110 // return if no task is registered and servicing at least one handle would | |
111 // require blocking. Returns true if a RunLoopHandler was notified. | |
112 bool Wait(bool non_blocking); | |
113 | |
114 // Notifies handlers of |error|. If |check| == CHECK_DEADLINE, this will | |
115 // only notify handlers whose deadline has expired and skips the rest. | |
116 // Returns true if a RunLoopHandler was notified. | |
117 bool NotifyHandlers(MojoResult error, CheckDeadline check); | |
118 | |
119 // Returns the state needed to pass to WaitMany(). | |
120 WaitState GetWaitState(bool non_blocking) const; | |
121 | |
122 HandleToHandlerData handler_data_; | |
123 | |
124 // If non-null we're running (inside Run()). Member references a value on the | |
125 // stack. | |
126 RunState* run_state_; | |
127 | |
128 // An ever increasing value assigned to each HandlerData::id. Used to detect | |
129 // uniqueness while notifying. That is, while notifying expired timers we copy | |
130 // |handler_data_| and only notify handlers whose id match. If the id does not | |
131 // match it means the handler was removed then added so that we shouldn't | |
132 // notify it. | |
133 int next_handler_id_; | |
134 | |
135 struct PendingTask { | |
136 PendingTask(const Closure& task, | |
137 MojoTimeTicks runtime, | |
138 uint64_t sequence_number); | |
139 PendingTask(const PendingTask& other); | |
140 ~PendingTask(); | |
141 | |
142 bool operator<(const PendingTask& other) const; | |
143 | |
144 Closure task; | |
145 MojoTimeTicks run_time; | |
146 uint64_t sequence_number; | |
147 }; | |
148 // An ever increasing sequence number attached to each pending task in order | |
149 // to preserve relative order of tasks posted at the 'same' time. | |
150 uint64_t next_sequence_number_; | |
151 typedef std::priority_queue<PendingTask> DelayedTaskQueue; | |
152 DelayedTaskQueue delayed_tasks_; | |
153 | |
154 DISALLOW_COPY_AND_ASSIGN(RunLoop); | |
155 }; | |
156 | |
157 } // namespace mojo | |
158 | |
159 #endif // MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ | |
OLD | NEW |