Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(810)

Side by Side Diff: base/message_loop/message_pump_mac.mm

Issue 289863005: [Mac] Maximise timer slack for background tabs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: A few small fixes Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #import "base/message_loop/message_pump_mac.h" 5 #import "base/message_loop/message_pump_mac.h"
6 6
7 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
8 8
9 #include <dlfcn.h>
9 #include <limits> 10 #include <limits>
10 #include <stack> 11 #include <stack>
11 12
12 #include "base/format_macros.h" 13 #include "base/format_macros.h"
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/mac/scoped_cftyperef.h" 15 #include "base/mac/scoped_cftyperef.h"
15 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
17 #include "base/message_loop/timer_slack.h"
16 #include "base/run_loop.h" 18 #include "base/run_loop.h"
17 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
18 #include "base/time/time.h" 20 #include "base/time/time.h"
19 21
20 #if !defined(OS_IOS) 22 #if !defined(OS_IOS)
21 #import <AppKit/AppKit.h> 23 #import <AppKit/AppKit.h>
22 #endif // !defined(OS_IOS) 24 #endif // !defined(OS_IOS)
23 25
24 namespace { 26 namespace {
25 27
26 void NoOp(void* info) { 28 void NoOp(void* info) {
27 } 29 }
28 30
29 const CFTimeInterval kCFTimeIntervalMax = 31 const CFTimeInterval kCFTimeIntervalMax =
30 std::numeric_limits<CFTimeInterval>::max(); 32 std::numeric_limits<CFTimeInterval>::max();
31 33
32 #if !defined(OS_IOS) 34 #if !defined(OS_IOS)
33 // Set to true if MessagePumpMac::Create() is called before NSApp is 35 // Set to true if MessagePumpMac::Create() is called before NSApp is
34 // initialized. Only accessed from the main thread. 36 // initialized. Only accessed from the main thread.
35 bool g_not_using_cr_app = false; 37 bool g_not_using_cr_app = false;
36 #endif 38 #endif
37 39
40 // Call through to CFRunLoopTimerSetTolerance(), which is only available on
41 // OS X 10.9.
42 void SetTimerTolerance(CFRunLoopTimerRef timer, CFTimeInterval tolerance) {
43 typedef void (*CFRunLoopTimerSetTolerancePtr)(CFRunLoopTimerRef timer,
44 CFTimeInterval tolerance);
45
46 static CFRunLoopTimerSetTolerancePtr settimertolerance_function_ptr = NULL;
47 static bool function_lookup_error = false;
Avi (use Gerrit) 2014/05/29 14:24:16 Is this thread-safe? We could (theoretically) get
jeremy 2014/06/02 10:21:28 Done.
48
49 if (function_lookup_error)
50 return;
51
52 if (!settimertolerance_function_ptr) {
53 settimertolerance_function_ptr =
54 reinterpret_cast<CFRunLoopTimerSetTolerancePtr>(
55 dlsym(RTLD_DEFAULT, "CFRunLoopTimerSetTolerance"));
56 if (!settimertolerance_function_ptr) {
57 function_lookup_error = true;
58 return;
59 }
60 }
61
62 settimertolerance_function_ptr(timer, tolerance);
63 }
64
38 } // namespace 65 } // namespace
39 66
40 namespace base { 67 namespace base {
41 68
42 // A scoper for autorelease pools created from message pump run loops. 69 // A scoper for autorelease pools created from message pump run loops.
43 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare 70 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare
44 // case where an autorelease pool needs to be passed in. 71 // case where an autorelease pool needs to be passed in.
45 class MessagePumpScopedAutoreleasePool { 72 class MessagePumpScopedAutoreleasePool {
46 public: 73 public:
47 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) : 74 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) :
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 std::stack<TimeTicks> loop_wait_times_; 302 std::stack<TimeTicks> loop_wait_times_;
276 std::stack<TimeTicks> work_source_times_; 303 std::stack<TimeTicks> work_source_times_;
277 304
278 DISALLOW_COPY_AND_ASSIGN(MessagePumpInstrumentation); 305 DISALLOW_COPY_AND_ASSIGN(MessagePumpInstrumentation);
279 }; 306 };
280 307
281 // Must be called on the run loop thread. 308 // Must be called on the run loop thread.
282 MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase() 309 MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
283 : delegate_(NULL), 310 : delegate_(NULL),
284 delayed_work_fire_time_(kCFTimeIntervalMax), 311 delayed_work_fire_time_(kCFTimeIntervalMax),
312 timer_slack_(base::TIMER_SLACK_NONE),
285 nesting_level_(0), 313 nesting_level_(0),
286 run_nesting_level_(0), 314 run_nesting_level_(0),
287 deepest_nesting_level_(0), 315 deepest_nesting_level_(0),
288 delegateless_work_(false), 316 delegateless_work_(false),
289 delegateless_idle_work_(false) { 317 delegateless_idle_work_(false) {
290 run_loop_ = CFRunLoopGetCurrent(); 318 run_loop_ = CFRunLoopGetCurrent();
291 CFRetain(run_loop_); 319 CFRetain(run_loop_);
292 320
293 // Set a repeating timer with a preposterous firing time and interval. The 321 // Set a repeating timer with a preposterous firing time and interval. The
294 // timer will effectively never fire as-is. The firing time will be adjusted 322 // timer will effectively never fire as-is. The firing time will be adjusted
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 CFRunLoopSourceSignal(work_source_); 459 CFRunLoopSourceSignal(work_source_);
432 CFRunLoopWakeUp(run_loop_); 460 CFRunLoopWakeUp(run_loop_);
433 } 461 }
434 462
435 // Must be called on the run loop thread. 463 // Must be called on the run loop thread.
436 void MessagePumpCFRunLoopBase::ScheduleDelayedWork( 464 void MessagePumpCFRunLoopBase::ScheduleDelayedWork(
437 const TimeTicks& delayed_work_time) { 465 const TimeTicks& delayed_work_time) {
438 TimeDelta delta = delayed_work_time - TimeTicks::Now(); 466 TimeDelta delta = delayed_work_time - TimeTicks::Now();
439 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF(); 467 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF();
440 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_); 468 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_);
469 if (timer_slack_ == TIMER_SLACK_MAXIMUM) {
470 SetTimerTolerance(delayed_work_timer_, delta.InSecondsF() * 0.5);
471 } else {
472 SetTimerTolerance(delayed_work_timer_, 0);
473 }
474 }
475
476 void MessagePumpCFRunLoopBase::SetTimerSlack(TimerSlack timer_slack) {
477 timer_slack_ = timer_slack;
441 } 478 }
442 479
443 // Called from the run loop. 480 // Called from the run loop.
444 // static 481 // static
445 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer, 482 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer,
446 void* info) { 483 void* info) {
447 MessagePumpCFRunLoopBase* self = static_cast<MessagePumpCFRunLoopBase*>(info); 484 MessagePumpCFRunLoopBase* self = static_cast<MessagePumpCFRunLoopBase*>(info);
448 485
449 // The timer won't fire again until it's reset. 486 // The timer won't fire again until it's reset.
450 self->delayed_work_fire_time_ = kCFTimeIntervalMax; 487 self->delayed_work_fire_time_ = kCFTimeIntervalMax;
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after
954 [NSApplication sharedApplication]; 991 [NSApplication sharedApplication];
955 g_not_using_cr_app = true; 992 g_not_using_cr_app = true;
956 return new MessagePumpNSApplication; 993 return new MessagePumpNSApplication;
957 #endif 994 #endif
958 } 995 }
959 996
960 return new MessagePumpNSRunLoop; 997 return new MessagePumpNSRunLoop;
961 } 998 }
962 999
963 } // namespace base 1000 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698