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_UTILITY_RUN_LOOP_H_ | 5 #ifndef MOJO_PUBLIC_UTILITY_RUN_LOOP_H_ |
6 #define MOJO_PUBLIC_UTILITY_RUN_LOOP_H_ | 6 #define MOJO_PUBLIC_UTILITY_RUN_LOOP_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "mojo/public/system/core_cpp.h" | 10 #include "mojo/public/system/core_cpp.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 void AddHandler(RunLoopHandler* handler, | 34 void AddHandler(RunLoopHandler* handler, |
35 const Handle& handle, | 35 const Handle& handle, |
36 MojoWaitFlags wait_flags, | 36 MojoWaitFlags wait_flags, |
37 MojoDeadline deadline); | 37 MojoDeadline deadline); |
38 void RemoveHandler(const Handle& handle); | 38 void RemoveHandler(const Handle& handle); |
39 bool HasHandler(const Handle& handle) const; | 39 bool HasHandler(const Handle& handle) const; |
40 | 40 |
41 // Runs the loop servicing handles as they are ready. This returns when Quit() | 41 // Runs the loop servicing handles as they are ready. This returns when Quit() |
42 // is invoked, or there no more handles. | 42 // is invoked, or there no more handles. |
43 void Run(); | 43 void Run(); |
| 44 |
| 45 // 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 |
| 47 // invoked. |
| 48 void RunUntilIdle(); |
| 49 |
44 void Quit(); | 50 void Quit(); |
45 | 51 |
46 private: | 52 private: |
47 struct RunState; | 53 struct RunState; |
48 struct WaitState; | 54 struct WaitState; |
49 | 55 |
50 // Contains the data needed to track a request to AddHandler(). | 56 // Contains the data needed to track a request to AddHandler(). |
51 struct HandlerData { | 57 struct HandlerData { |
52 HandlerData() | 58 HandlerData() |
53 : handler(NULL), | 59 : handler(NULL), |
54 wait_flags(MOJO_WAIT_FLAG_NONE), | 60 wait_flags(MOJO_WAIT_FLAG_NONE), |
55 deadline(0), | 61 deadline(0), |
56 id(0) {} | 62 id(0) {} |
57 | 63 |
58 RunLoopHandler* handler; | 64 RunLoopHandler* handler; |
59 MojoWaitFlags wait_flags; | 65 MojoWaitFlags wait_flags; |
60 MojoTimeTicks deadline; | 66 MojoTimeTicks deadline; |
61 // See description of |RunLoop::next_handler_id_| for details. | 67 // See description of |RunLoop::next_handler_id_| for details. |
62 int id; | 68 int id; |
63 }; | 69 }; |
64 | 70 |
65 typedef std::map<Handle, HandlerData> HandleToHandlerData; | 71 typedef std::map<Handle, HandlerData> HandleToHandlerData; |
66 | 72 |
67 // Waits for a handle to be ready. Returns after servicing at least one | 73 // Waits for a handle to be ready. Returns after servicing at least one |
68 // handle (or there are no more handles). | 74 // handle (or there are no more handles) unless |non_blocking| is true, |
69 void Wait(); | 75 // in which case it will also return if servicing at least one handle |
| 76 // would require blocking. Returns true if a RunLoopHandler was notified. |
| 77 bool Wait(bool non_blocking); |
70 | 78 |
71 // Notifies any handlers whose deadline has expired. | 79 // Notifies any handlers whose deadline has expired. Returns true if a |
72 void NotifyDeadlineExceeded(); | 80 // RunLoopHandler was notified. |
| 81 bool NotifyDeadlineExceeded(); |
73 | 82 |
74 // Removes the first invalid handle. This is called if MojoWaitMany() finds an | 83 // Removes the first invalid handle. This is called if MojoWaitMany() finds an |
75 // invalid handle. | 84 // invalid handle. Returns true if a RunLoopHandler was notified. |
76 void RemoveFirstInvalidHandle(const WaitState& wait_state); | 85 bool RemoveFirstInvalidHandle(const WaitState& wait_state); |
77 | 86 |
78 // Returns the state needed to pass to WaitMany(). | 87 // Returns the state needed to pass to WaitMany(). |
79 WaitState GetWaitState() const; | 88 WaitState GetWaitState(bool non_blocking) const; |
80 | 89 |
81 HandleToHandlerData handler_data_; | 90 HandleToHandlerData handler_data_; |
82 | 91 |
83 // If non-NULL we're running (inside Run()). Member references a value on the | 92 // If non-NULL we're running (inside Run()). Member references a value on the |
84 // stack. | 93 // stack. |
85 RunState* run_state_; | 94 RunState* run_state_; |
86 | 95 |
87 // An ever increasing value assigned to each HandlerData::id. Used to detect | 96 // An ever increasing value assigned to each HandlerData::id. Used to detect |
88 // uniqueness while notifying. That is, while notifying expired timers we copy | 97 // uniqueness while notifying. That is, while notifying expired timers we copy |
89 // |handler_data_| and only notify handlers whose id match. If the id does not | 98 // |handler_data_| and only notify handlers whose id match. If the id does not |
90 // match it means the handler was removed then added so that we shouldn't | 99 // match it means the handler was removed then added so that we shouldn't |
91 // notify it. | 100 // notify it. |
92 int next_handler_id_; | 101 int next_handler_id_; |
93 | 102 |
94 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoop); | 103 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoop); |
95 }; | 104 }; |
96 | 105 |
97 } // namespace mojo | 106 } // namespace mojo |
98 | 107 |
99 #endif // MOJO_PUBLIC_UTILITY_RUN_LOOP_H_ | 108 #endif // MOJO_PUBLIC_UTILITY_RUN_LOOP_H_ |
OLD | NEW |