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 | 10 |
| 11 #include "mojo/public/cpp/bindings/callback.h" |
10 #include "mojo/public/cpp/system/core.h" | 12 #include "mojo/public/cpp/system/core.h" |
11 | 13 |
12 namespace mojo { | 14 namespace mojo { |
13 | 15 |
14 class RunLoopHandler; | 16 class RunLoopHandler; |
15 | 17 |
16 class RunLoop { | 18 class RunLoop { |
17 public: | 19 public: |
18 RunLoop(); | 20 RunLoop(); |
19 ~RunLoop(); | 21 ~RunLoop(); |
(...skipping 22 matching lines...) Expand all Loading... |
42 // is invoked, or there no more handles. | 44 // is invoked, or there no more handles. |
43 void Run(); | 45 void Run(); |
44 | 46 |
45 // Runs the loop servicing any handles that are ready. Does not wait for | 47 // Runs the loop servicing any handles that are ready. Does not wait for |
46 // handles to become ready before returning. Returns early if Quit() is | 48 // handles to become ready before returning. Returns early if Quit() is |
47 // invoked. | 49 // invoked. |
48 void RunUntilIdle(); | 50 void RunUntilIdle(); |
49 | 51 |
50 void Quit(); | 52 void Quit(); |
51 | 53 |
| 54 // Adds a task to be performed after delay has elapsed. Must be posted to the |
| 55 // current thread's RunLoop. |
| 56 void PostDelayedTask(const Closure& task, MojoTimeTicks delay); |
| 57 |
52 private: | 58 private: |
53 struct RunState; | 59 struct RunState; |
54 struct WaitState; | 60 struct WaitState; |
55 | 61 |
56 // Contains the data needed to track a request to AddHandler(). | 62 // Contains the data needed to track a request to AddHandler(). |
57 struct HandlerData { | 63 struct HandlerData { |
58 HandlerData() | 64 HandlerData() |
59 : handler(NULL), | 65 : handler(NULL), |
60 handle_signals(MOJO_HANDLE_SIGNAL_NONE), | 66 handle_signals(MOJO_HANDLE_SIGNAL_NONE), |
61 deadline(0), | 67 deadline(0), |
62 id(0) {} | 68 id(0) {} |
63 | 69 |
64 RunLoopHandler* handler; | 70 RunLoopHandler* handler; |
65 MojoHandleSignals handle_signals; | 71 MojoHandleSignals handle_signals; |
66 MojoTimeTicks deadline; | 72 MojoTimeTicks deadline; |
67 // See description of |RunLoop::next_handler_id_| for details. | 73 // See description of |RunLoop::next_handler_id_| for details. |
68 int id; | 74 int id; |
69 }; | 75 }; |
70 | 76 |
71 typedef std::map<Handle, HandlerData> HandleToHandlerData; | 77 typedef std::map<Handle, HandlerData> HandleToHandlerData; |
72 | 78 |
| 79 // Do one unit of delayed work, if eligible. |
| 80 void DoDelayedWork(); |
| 81 |
73 // Waits for a handle to be ready. Returns after servicing at least one | 82 // Waits for a handle to be ready. Returns after servicing at least one |
74 // handle (or there are no more handles) unless |non_blocking| is true, | 83 // handle (or there are no more handles) unless |non_blocking| is true, |
75 // in which case it will also return if servicing at least one handle | 84 // in which case it will also return if servicing at least one handle |
76 // would require blocking. Returns true if a RunLoopHandler was notified. | 85 // would require blocking. Returns true if a RunLoopHandler was notified. |
77 bool Wait(bool non_blocking); | 86 bool Wait(bool non_blocking); |
78 | 87 |
79 // Notifies any handlers whose deadline has expired. Returns true if a | 88 // Notifies any handlers whose deadline has expired. Returns true if a |
80 // RunLoopHandler was notified. | 89 // RunLoopHandler was notified. |
81 bool NotifyDeadlineExceeded(); | 90 bool NotifyDeadlineExceeded(); |
82 | 91 |
(...skipping 10 matching lines...) Expand all Loading... |
93 // stack. | 102 // stack. |
94 RunState* run_state_; | 103 RunState* run_state_; |
95 | 104 |
96 // An ever increasing value assigned to each HandlerData::id. Used to detect | 105 // An ever increasing value assigned to each HandlerData::id. Used to detect |
97 // uniqueness while notifying. That is, while notifying expired timers we copy | 106 // uniqueness while notifying. That is, while notifying expired timers we copy |
98 // |handler_data_| and only notify handlers whose id match. If the id does not | 107 // |handler_data_| and only notify handlers whose id match. If the id does not |
99 // match it means the handler was removed then added so that we shouldn't | 108 // match it means the handler was removed then added so that we shouldn't |
100 // notify it. | 109 // notify it. |
101 int next_handler_id_; | 110 int next_handler_id_; |
102 | 111 |
| 112 struct PendingTask { |
| 113 PendingTask(const Closure& task, |
| 114 MojoTimeTicks runtime, |
| 115 uint64_t sequence_number); |
| 116 ~PendingTask(); |
| 117 |
| 118 bool operator<(const PendingTask& other) const; |
| 119 |
| 120 Closure task; |
| 121 MojoTimeTicks run_time; |
| 122 uint64_t sequence_number; |
| 123 }; |
| 124 // An ever increasing sequence number attached to each pending task in order |
| 125 // to preserve relative order of tasks posted at the 'same' time. |
| 126 uint64_t next_sequence_number_; |
| 127 typedef std::priority_queue<PendingTask> DelayedTaskQueue; |
| 128 DelayedTaskQueue delayed_tasks_; |
| 129 |
103 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoop); | 130 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoop); |
104 }; | 131 }; |
105 | 132 |
106 } // namespace mojo | 133 } // namespace mojo |
107 | 134 |
108 #endif // MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ | 135 #endif // MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_ |
OLD | NEW |