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 |
(...skipping 22 matching lines...) Expand all Loading... |
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); |
41 bool HasHandler(const Handle& handle) const; | 41 bool HasHandler(const Handle& handle) const; |
42 | 42 |
43 // Runs the loop servicing handles as they are ready. This returns when Quit() | 43 // Runs the loop servicing handles and tasks as they are ready. This returns |
44 // is invoked, or there no more handles. | 44 // when Quit() is invoked, or there are no more handles nor tasks. |
45 void Run(); | 45 void Run(); |
46 | 46 |
47 // Runs the loop servicing any handles that are ready. Does not wait for | 47 // Runs the loop servicing any handles and tasks that are ready. Does not wait |
48 // handles to become ready before returning. Returns early if Quit() is | 48 // for handles or tasks to become ready before returning. Returns early if |
49 // invoked. | 49 // Quit() is invoked. |
50 void RunUntilIdle(); | 50 void RunUntilIdle(); |
51 | 51 |
52 void Quit(); | 52 void Quit(); |
53 | 53 |
54 // Adds a task to be performed after delay has elapsed. Must be posted to the | 54 // Adds a task to be performed after delay has elapsed. Must be posted to the |
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; |
(...skipping 16 matching lines...) Expand all Loading... |
76 | 76 |
77 typedef std::map<Handle, HandlerData> HandleToHandlerData; | 77 typedef std::map<Handle, HandlerData> HandleToHandlerData; |
78 | 78 |
79 // Used for NotifyHandlers to specify whether HandlerData's |deadline| | 79 // Used for NotifyHandlers to specify whether HandlerData's |deadline| |
80 // should be checked prior to notifying. | 80 // should be checked prior to notifying. |
81 enum CheckDeadline { | 81 enum CheckDeadline { |
82 CHECK_DEADLINE, | 82 CHECK_DEADLINE, |
83 IGNORE_DEADLINE | 83 IGNORE_DEADLINE |
84 }; | 84 }; |
85 | 85 |
86 // Do one unit of delayed work, if eligible. | 86 // Mode of operation of the run loop. |
87 void DoDelayedWork(); | 87 enum RunMode { |
| 88 UNTIL_EMPTY, |
| 89 UNTIL_IDLE |
| 90 }; |
88 | 91 |
89 // Waits for a handle to be ready. Returns after servicing at least one | 92 // Runs the loop servicing any handles and tasks that are ready. If |
90 // handle (or there are no more handles) unless |non_blocking| is true, | 93 // |run_mode| is |UNTIL_IDLE|, does not wait for handles or tasks to become |
91 // in which case it will also return if servicing at least one handle | 94 // ready before returning. Returns early if Quit() is invoked. |
92 // would require blocking. Returns true if a RunLoopHandler was notified. | 95 void RunInternal(RunMode run_mode); |
| 96 |
| 97 // Do one unit of delayed work, if eligible. Returns true is a task was run. |
| 98 bool DoDelayedWork(); |
| 99 |
| 100 // Waits for a handle to be ready or until the next task must be run. Returns |
| 101 // after servicing at least one handle (or there are no more handles) unless |
| 102 // a task must be run or |non_blocking| is true, in which case it will also |
| 103 // return if no task is registered and servicing at least one handle would |
| 104 // require blocking. Returns true if a RunLoopHandler was notified. |
93 bool Wait(bool non_blocking); | 105 bool Wait(bool non_blocking); |
94 | 106 |
95 // Notifies handlers of |error|. If |check| == CHECK_DEADLINE, this will | 107 // Notifies handlers of |error|. If |check| == CHECK_DEADLINE, this will |
96 // only notify handlers whose deadline has expired and skips the rest. | 108 // only notify handlers whose deadline has expired and skips the rest. |
97 // Returns true if a RunLoopHandler was notified. | 109 // Returns true if a RunLoopHandler was notified. |
98 bool NotifyHandlers(MojoResult error, CheckDeadline check); | 110 bool NotifyHandlers(MojoResult error, CheckDeadline check); |
99 | 111 |
100 // Removes the first invalid handle. This is called if MojoWaitMany() finds an | 112 // Removes the first invalid handle. This is called if MojoWaitMany() finds an |
101 // invalid handle. Returns true if a RunLoopHandler was notified. | 113 // invalid handle. Returns true if a RunLoopHandler was notified. |
102 bool RemoveFirstInvalidHandle(const WaitState& wait_state); | 114 bool RemoveFirstInvalidHandle(const WaitState& wait_state); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 uint64_t next_sequence_number_; | 146 uint64_t next_sequence_number_; |
135 typedef std::priority_queue<PendingTask> DelayedTaskQueue; | 147 typedef std::priority_queue<PendingTask> DelayedTaskQueue; |
136 DelayedTaskQueue delayed_tasks_; | 148 DelayedTaskQueue delayed_tasks_; |
137 | 149 |
138 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoop); | 150 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoop); |
139 }; | 151 }; |
140 | 152 |
141 } // namespace mojo | 153 } // namespace mojo |
142 | 154 |
143 #endif // MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ | 155 #endif // MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ |
OLD | NEW |