1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/message_loop/message_pump_default.h"
6 
7 #include "base/auto_reset.h"
8 #include "base/logging.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "build/build_config.h"
11 
12 #if defined(OS_MACOSX)
13 #include <mach/thread_policy.h>
14 
15 #include "base/mac/mach_logging.h"
16 #include "base/mac/scoped_mach_port.h"
17 #include "base/mac/scoped_nsautorelease_pool.h"
18 #endif
19 
20 namespace base {
21 
MessagePumpDefault()22 MessagePumpDefault::MessagePumpDefault()
23     : keep_running_(true),
24       event_(WaitableEvent::ResetPolicy::AUTOMATIC,
25              WaitableEvent::InitialState::NOT_SIGNALED) {}
26 
27 MessagePumpDefault::~MessagePumpDefault() = default;
28 
Run(Delegate * delegate)29 void MessagePumpDefault::Run(Delegate* delegate) {
30   AutoReset<bool> auto_reset_keep_running(&keep_running_, true);
31 
32   for (;;) {
33 #if defined(OS_MACOSX)
34     mac::ScopedNSAutoreleasePool autorelease_pool;
35 #endif
36 
37     bool did_work = delegate->DoWork();
38     if (!keep_running_)
39       break;
40 
41     did_work |= delegate->DoDelayedWork(&delayed_work_time_);
42     if (!keep_running_)
43       break;
44 
45     if (did_work)
46       continue;
47 
48     did_work = delegate->DoIdleWork();
49     if (!keep_running_)
50       break;
51 
52     if (did_work)
53       continue;
54 
55     ThreadRestrictions::ScopedAllowWait allow_wait;
56     if (delayed_work_time_.is_null()) {
57       event_.Wait();
58     } else {
59       // No need to handle already expired |delayed_work_time_| in any special
60       // way. When |delayed_work_time_| is in the past TimeWaitUntil returns
61       // promptly and |delayed_work_time_| will re-initialized on a next
62       // DoDelayedWork call which has to be called in order to get here again.
63       event_.TimedWaitUntil(delayed_work_time_);
64     }
65     // Since event_ is auto-reset, we don't need to do anything special here
66     // other than service each delegate method.
67   }
68 }
69 
Quit()70 void MessagePumpDefault::Quit() {
71   keep_running_ = false;
72 }
73 
ScheduleWork()74 void MessagePumpDefault::ScheduleWork() {
75   // Since this can be called on any thread, we need to ensure that our Run
76   // loop wakes up.
77   event_.Signal();
78 }
79 
ScheduleDelayedWork(const TimeTicks & delayed_work_time)80 void MessagePumpDefault::ScheduleDelayedWork(
81     const TimeTicks& delayed_work_time) {
82   // We know that we can't be blocked on Wait right now since this method can
83   // only be called on the same thread as Run, so we only need to update our
84   // record of how long to sleep when we do sleep.
85   delayed_work_time_ = delayed_work_time;
86 }
87 
88 #if defined(OS_MACOSX)
SetTimerSlack(TimerSlack timer_slack)89 void MessagePumpDefault::SetTimerSlack(TimerSlack timer_slack) {
90   thread_latency_qos_policy_data_t policy{};
91   policy.thread_latency_qos_tier = timer_slack == TIMER_SLACK_MAXIMUM
92                                        ? LATENCY_QOS_TIER_3
93                                        : LATENCY_QOS_TIER_UNSPECIFIED;
94   mac::ScopedMachSendRight thread_port(mach_thread_self());
95   kern_return_t kr =
96       thread_policy_set(thread_port.get(), THREAD_LATENCY_QOS_POLICY,
97                         reinterpret_cast<thread_policy_t>(&policy),
98                         THREAD_LATENCY_QOS_POLICY_COUNT);
99   MACH_DVLOG_IF(1, kr != KERN_SUCCESS, kr) << "thread_policy_set";
100 }
101 #endif
102 
103 }  // namespace base
104