| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 // The basis for all native run loops on the Mac is the CFRunLoop. It can be | 5 // The basis for all native run loops on the Mac is the CFRunLoop. It can be |
| 6 // used directly, it can be used as the driving force behind the similar | 6 // used directly, it can be used as the driving force behind the similar |
| 7 // Foundation NSRunLoop, and it can be used to implement higher-level event | 7 // Foundation NSRunLoop, and it can be used to implement higher-level event |
| 8 // loops such as the NSApplication event loop. | 8 // loops such as the NSApplication event loop. |
| 9 // | 9 // |
| 10 // This file introduces a basic CFRunLoop-based implementation of the | 10 // This file introduces a basic CFRunLoop-based implementation of the |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 virtual void Run(Delegate* delegate); | 50 virtual void Run(Delegate* delegate); |
| 51 virtual void DoRun(Delegate* delegate) = 0; | 51 virtual void DoRun(Delegate* delegate) = 0; |
| 52 | 52 |
| 53 virtual void ScheduleWork(); | 53 virtual void ScheduleWork(); |
| 54 virtual void ScheduleDelayedWork(const Time& delayed_work_time); | 54 virtual void ScheduleDelayedWork(const Time& delayed_work_time); |
| 55 | 55 |
| 56 protected: | 56 protected: |
| 57 // The thread's run loop. | 57 // The thread's run loop. |
| 58 CFRunLoopRef run_loop_; | 58 CFRunLoopRef run_loop_; |
| 59 | 59 |
| 60 // The recursion depth of the currently-executing CFRunLoopRun loop on the |
| 61 // run loop's thread. 0 if no run loops are running inside of whatever scope |
| 62 // the object was created in. |
| 63 int nesting_level_; |
| 64 |
| 60 private: | 65 private: |
| 61 // Timer callback scheduled by ScheduleDelayedWork. This does not do any | 66 // Timer callback scheduled by ScheduleDelayedWork. This does not do any |
| 62 // work, but it signals delayed_work_source_ so that delayed work can be | 67 // work, but it signals delayed_work_source_ so that delayed work can be |
| 63 // performed within the appropriate priority constraints. | 68 // performed within the appropriate priority constraints. |
| 64 static void RunDelayedWorkTimer(CFRunLoopTimerRef timer, void* info); | 69 static void RunDelayedWorkTimer(CFRunLoopTimerRef timer, void* info); |
| 65 | 70 |
| 66 // Perform highest-priority work. This is associated with work_source_ | 71 // Perform highest-priority work. This is associated with work_source_ |
| 67 // signalled by ScheduleWork. | 72 // signalled by ScheduleWork. The static method calls the instance method; |
| 68 static void RunWork(void* info); | 73 // the instance method returns true if work was done. |
| 74 static void RunWorkSource(void* info); |
| 75 bool RunWork(); |
| 69 | 76 |
| 70 // Perform delayed-priority work. This is associated with | 77 // Perform delayed-priority work. This is associated with |
| 71 // delayed_work_source_ signalled by RunDelayedWorkTimer, and is responsible | 78 // delayed_work_source_ signalled by RunDelayedWorkTimer, and is responsible |
| 72 // for calling ScheduleDelayedWork again if appropriate. | 79 // for calling ScheduleDelayedWork again if appropriate. The static method |
| 73 static void RunDelayedWork(void* info); | 80 // calls the instance method; the instance method returns true if more |
| 81 // delayed work is available. |
| 82 static void RunDelayedWorkSource(void* info); |
| 83 bool RunDelayedWork(); |
| 84 |
| 85 // Perform idle-priority work. This is normally called by PreWaitObserver, |
| 86 // but is also associated with idle_work_source_. When this function |
| 87 // actually does perform idle work, it will resignal that source. The |
| 88 // static method calls the instance method; the instance method returns |
| 89 // true if idle work was done. |
| 90 static void RunIdleWorkSource(void* info); |
| 91 bool RunIdleWork(); |
| 92 |
| 93 // Perform work that may have been deferred because it was not runnable |
| 94 // within a nested run loop. This is associated with |
| 95 // nesting_deferred_work_source_ and is signalled by EnterExitObserver when |
| 96 // a run loop exits, so that an outer loop will be able to perform the |
| 97 // necessary tasks. The static method calls the instance method; the |
| 98 // instance method returns true if anything was done. |
| 99 static void RunNestingDeferredWorkSource(void* info); |
| 100 bool RunNestingDeferredWork(); |
| 74 | 101 |
| 75 // Observer callback responsible for performing idle-priority work, before | 102 // Observer callback responsible for performing idle-priority work, before |
| 76 // the run loop goes to sleep. Associated with idle_work_observer_. | 103 // the run loop goes to sleep. Associated with idle_work_observer_. |
| 77 static void RunIdleWork(CFRunLoopObserverRef observer, | 104 static void PreWaitObserver(CFRunLoopObserverRef observer, |
| 78 CFRunLoopActivity activity, void* info); | 105 CFRunLoopActivity activity, void* info); |
| 79 | 106 |
| 80 // The timer, sources, and observer are described above alongside their | 107 // Observer callback called when the run loop starts and stops, at the |
| 108 // beginning and end of calls to CFRunLoopRun. This is used to maintain |
| 109 // nesting_level_. Associated with enter_exit_observer_. |
| 110 static void EnterExitObserver(CFRunLoopObserverRef observer, |
| 111 CFRunLoopActivity activity, void* info); |
| 112 |
| 113 // Called by EnterExitObserver after performing maintenance on nesting_level_. |
| 114 // This allows subclasses an opportunity to perform additional processing on |
| 115 // the basis of run loops starting and stopping. |
| 116 virtual void EnterExitRunLoop(CFRunLoopActivity activity); |
| 117 |
| 118 // The timer, sources, and observers are described above alongside their |
| 81 // callbacks. | 119 // callbacks. |
| 82 CFRunLoopTimerRef delayed_work_timer_; | 120 CFRunLoopTimerRef delayed_work_timer_; |
| 83 CFRunLoopSourceRef work_source_; | 121 CFRunLoopSourceRef work_source_; |
| 84 CFRunLoopSourceRef delayed_work_source_; | 122 CFRunLoopSourceRef delayed_work_source_; |
| 85 CFRunLoopObserverRef idle_work_observer_; | 123 CFRunLoopSourceRef idle_work_source_; |
| 124 CFRunLoopSourceRef nesting_deferred_work_source_; |
| 125 CFRunLoopObserverRef pre_wait_observer_; |
| 126 CFRunLoopObserverRef enter_exit_observer_; |
| 86 | 127 |
| 87 // (weak) Delegate passed as an argument to the innermost Run call. | 128 // (weak) Delegate passed as an argument to the innermost Run call. |
| 88 Delegate* delegate_; | 129 Delegate* delegate_; |
| 89 | 130 |
| 90 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoopBase); | 131 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoopBase); |
| 91 }; | 132 }; |
| 92 | 133 |
| 93 class MessagePumpCFRunLoop : public MessagePumpCFRunLoopBase { | 134 class MessagePumpCFRunLoop : public MessagePumpCFRunLoopBase { |
| 94 public: | 135 public: |
| 95 MessagePumpCFRunLoop(); | 136 MessagePumpCFRunLoop(); |
| 96 virtual ~MessagePumpCFRunLoop(); | |
| 97 | 137 |
| 98 virtual void DoRun(Delegate* delegate); | 138 virtual void DoRun(Delegate* delegate); |
| 99 virtual void Quit(); | 139 virtual void Quit(); |
| 100 | 140 |
| 101 private: | 141 private: |
| 102 // Observer callback called when the run loop starts and stops, at the | 142 virtual void EnterExitRunLoop(CFRunLoopActivity activity); |
| 103 // beginning and end of calls to CFRunLoopRun. This is used to maintain | |
| 104 // nesting_level_ and to handle deferred loop quits. Associated with | |
| 105 // enter_exit_observer_. | |
| 106 static void EnterExitRunLoop(CFRunLoopObserverRef observer, | |
| 107 CFRunLoopActivity activity, void* info); | |
| 108 | |
| 109 // Observer for EnterExitRunLoop. | |
| 110 CFRunLoopObserverRef enter_exit_observer_; | |
| 111 | |
| 112 // The recursion depth of the currently-executing CFRunLoopRun loop on the | |
| 113 // run loop's thread. 0 if no run loops are running inside of whatever scope | |
| 114 // the object was created in. | |
| 115 int nesting_level_; | |
| 116 | 143 |
| 117 // The recursion depth (calculated in the same way as nesting_level_) of the | 144 // The recursion depth (calculated in the same way as nesting_level_) of the |
| 118 // innermost executing CFRunLoopRun loop started by a call to Run. | 145 // innermost executing CFRunLoopRun loop started by a call to Run. |
| 119 int innermost_quittable_; | 146 int innermost_quittable_; |
| 120 | 147 |
| 121 // True if Quit is called to stop the innermost MessagePump | 148 // True if Quit is called to stop the innermost MessagePump |
| 122 // (innermost_quittable_) but some other CFRunLoopRun loop (nesting_level_) | 149 // (innermost_quittable_) but some other CFRunLoopRun loop (nesting_level_) |
| 123 // is running inside the MessagePump's innermost Run call. | 150 // is running inside the MessagePump's innermost Run call. |
| 124 bool quit_pending_; | 151 bool quit_pending_; |
| 125 | 152 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 // thread. Otherwise, returns a new instance of MessagePumpNSRunLoop. | 199 // thread. Otherwise, returns a new instance of MessagePumpNSRunLoop. |
| 173 static MessagePump* Create(); | 200 static MessagePump* Create(); |
| 174 | 201 |
| 175 private: | 202 private: |
| 176 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); | 203 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); |
| 177 }; | 204 }; |
| 178 | 205 |
| 179 } // namespace base | 206 } // namespace base |
| 180 | 207 |
| 181 #endif // BASE_MESSAGE_PUMP_MAC_H_ | 208 #endif // BASE_MESSAGE_PUMP_MAC_H_ |
| OLD | NEW |