OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ | 5 #ifndef MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ |
6 #define MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ | 6 #define MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <queue> | 9 #include <queue> |
10 | 10 |
11 #include "mojo/public/cpp/bindings/callback.h" | 11 #include "mojo/public/cpp/bindings/callback.h" |
12 #include "mojo/public/cpp/system/core.h" | 12 #include "mojo/public/cpp/system/core.h" |
13 | 13 |
14 namespace mojo { | 14 namespace mojo { |
15 | 15 |
16 class RunLoopHandler; | 16 class RunLoopHandler; |
17 | 17 |
18 class RunLoop { | 18 class RunLoop { |
19 public: | 19 public: |
20 RunLoop(); | 20 RunLoop(); |
21 ~RunLoop(); | 21 ~RunLoop(); |
22 | 22 |
23 // Sets up state needed for RunLoop. This must be invoked before creating a | 23 // Sets up state needed for RunLoop. This must be invoked before creating a |
24 // RunLoop. | 24 // RunLoop. |
25 static void SetUp(); | 25 static void SetUp(); |
26 | 26 |
27 // Cleans state created by Setup(). | 27 // Cleans state created by Setup(). |
28 static void TearDown(); | 28 static void TearDown(); |
29 | 29 |
30 // Returns the RunLoop for the current thread. Returns NULL if not yet | 30 // Returns the RunLoop for the current thread. Returns null if not yet |
31 // created. | 31 // created. |
32 static RunLoop* current(); | 32 static RunLoop* current(); |
33 | 33 |
34 // Registers a RunLoopHandler for the specified handle. Only one handler can | 34 // Registers a RunLoopHandler for the specified handle. Only one handler can |
35 // be registered for a specified handle. | 35 // be registered for a specified handle. |
36 void AddHandler(RunLoopHandler* handler, | 36 void AddHandler(RunLoopHandler* handler, |
37 const Handle& handle, | 37 const Handle& handle, |
38 MojoHandleSignals handle_signals, | 38 MojoHandleSignals handle_signals, |
39 MojoDeadline deadline); | 39 MojoDeadline deadline); |
40 void RemoveHandler(const Handle& handle); | 40 void RemoveHandler(const Handle& handle); |
(...skipping 14 matching lines...) Expand all Loading... |
55 // current thread's RunLoop. | 55 // current thread's RunLoop. |
56 void PostDelayedTask(const Closure& task, MojoTimeTicks delay); | 56 void PostDelayedTask(const Closure& task, MojoTimeTicks delay); |
57 | 57 |
58 private: | 58 private: |
59 struct RunState; | 59 struct RunState; |
60 struct WaitState; | 60 struct WaitState; |
61 | 61 |
62 // Contains the data needed to track a request to AddHandler(). | 62 // Contains the data needed to track a request to AddHandler(). |
63 struct HandlerData { | 63 struct HandlerData { |
64 HandlerData() | 64 HandlerData() |
65 : handler(NULL), | 65 : handler(nullptr), |
66 handle_signals(MOJO_HANDLE_SIGNAL_NONE), | 66 handle_signals(MOJO_HANDLE_SIGNAL_NONE), |
67 deadline(0), | 67 deadline(0), |
68 id(0) {} | 68 id(0) {} |
69 | 69 |
70 RunLoopHandler* handler; | 70 RunLoopHandler* handler; |
71 MojoHandleSignals handle_signals; | 71 MojoHandleSignals handle_signals; |
72 MojoTimeTicks deadline; | 72 MojoTimeTicks deadline; |
73 // See description of |RunLoop::next_handler_id_| for details. | 73 // See description of |RunLoop::next_handler_id_| for details. |
74 int id; | 74 int id; |
75 }; | 75 }; |
(...skipping 29 matching lines...) Expand all Loading... |
105 | 105 |
106 // Removes the first invalid handle. This is called if MojoWaitMany() finds an | 106 // Removes the first invalid handle. This is called if MojoWaitMany() finds an |
107 // invalid handle. Returns true if a RunLoopHandler was notified. | 107 // invalid handle. Returns true if a RunLoopHandler was notified. |
108 bool RemoveFirstInvalidHandle(const WaitState& wait_state); | 108 bool RemoveFirstInvalidHandle(const WaitState& wait_state); |
109 | 109 |
110 // Returns the state needed to pass to WaitMany(). | 110 // Returns the state needed to pass to WaitMany(). |
111 WaitState GetWaitState(bool non_blocking) const; | 111 WaitState GetWaitState(bool non_blocking) const; |
112 | 112 |
113 HandleToHandlerData handler_data_; | 113 HandleToHandlerData handler_data_; |
114 | 114 |
115 // If non-NULL we're running (inside Run()). Member references a value on the | 115 // If non-null we're running (inside Run()). Member references a value on the |
116 // stack. | 116 // stack. |
117 RunState* run_state_; | 117 RunState* run_state_; |
118 | 118 |
119 // An ever increasing value assigned to each HandlerData::id. Used to detect | 119 // An ever increasing value assigned to each HandlerData::id. Used to detect |
120 // uniqueness while notifying. That is, while notifying expired timers we copy | 120 // uniqueness while notifying. That is, while notifying expired timers we copy |
121 // |handler_data_| and only notify handlers whose id match. If the id does not | 121 // |handler_data_| and only notify handlers whose id match. If the id does not |
122 // match it means the handler was removed then added so that we shouldn't | 122 // match it means the handler was removed then added so that we shouldn't |
123 // notify it. | 123 // notify it. |
124 int next_handler_id_; | 124 int next_handler_id_; |
125 | 125 |
(...skipping 14 matching lines...) Expand all Loading... |
140 uint64_t next_sequence_number_; | 140 uint64_t next_sequence_number_; |
141 typedef std::priority_queue<PendingTask> DelayedTaskQueue; | 141 typedef std::priority_queue<PendingTask> DelayedTaskQueue; |
142 DelayedTaskQueue delayed_tasks_; | 142 DelayedTaskQueue delayed_tasks_; |
143 | 143 |
144 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoop); | 144 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoop); |
145 }; | 145 }; |
146 | 146 |
147 } // namespace mojo | 147 } // namespace mojo |
148 | 148 |
149 #endif // MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ | 149 #endif // MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ |
OLD | NEW |