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

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: Fix review comments 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>
Mark Mentovai 2014/06/03 13:15:22 This belongs in the C system header section (with
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"
Mark Mentovai 2014/06/03 13:15:22 Sort: message < metrics.
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;
47
48 static dispatch_once_t get_timer_tolerance_function_ptr_once;
49 dispatch_once(&get_timer_tolerance_function_ptr_once, ^{
50 settimertolerance_function_ptr =
51 reinterpret_cast<CFRunLoopTimerSetTolerancePtr>(
52 dlsym(RTLD_DEFAULT, "CFRunLoopTimerSetTolerance"));
Mark Mentovai 2014/06/03 13:15:22 Don’t use RTLD_DEFAULT, use a handle to CoreFounda
53 });
54
55 if (settimertolerance_function_ptr)
56 settimertolerance_function_ptr(timer, tolerance);
57 }
58
38 } // namespace 59 } // namespace
39 60
40 namespace base { 61 namespace base {
41 62
42 // A scoper for autorelease pools created from message pump run loops. 63 // A scoper for autorelease pools created from message pump run loops.
43 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare 64 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare
44 // case where an autorelease pool needs to be passed in. 65 // case where an autorelease pool needs to be passed in.
45 class MessagePumpScopedAutoreleasePool { 66 class MessagePumpScopedAutoreleasePool {
46 public: 67 public:
47 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) : 68 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) :
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 std::stack<TimeTicks> loop_wait_times_; 296 std::stack<TimeTicks> loop_wait_times_;
276 std::stack<TimeTicks> work_source_times_; 297 std::stack<TimeTicks> work_source_times_;
277 298
278 DISALLOW_COPY_AND_ASSIGN(MessagePumpInstrumentation); 299 DISALLOW_COPY_AND_ASSIGN(MessagePumpInstrumentation);
279 }; 300 };
280 301
281 // Must be called on the run loop thread. 302 // Must be called on the run loop thread.
282 MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase() 303 MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
283 : delegate_(NULL), 304 : delegate_(NULL),
284 delayed_work_fire_time_(kCFTimeIntervalMax), 305 delayed_work_fire_time_(kCFTimeIntervalMax),
306 timer_slack_(base::TIMER_SLACK_NONE),
285 nesting_level_(0), 307 nesting_level_(0),
286 run_nesting_level_(0), 308 run_nesting_level_(0),
287 deepest_nesting_level_(0), 309 deepest_nesting_level_(0),
288 delegateless_work_(false), 310 delegateless_work_(false),
289 delegateless_idle_work_(false) { 311 delegateless_idle_work_(false) {
290 run_loop_ = CFRunLoopGetCurrent(); 312 run_loop_ = CFRunLoopGetCurrent();
291 CFRetain(run_loop_); 313 CFRetain(run_loop_);
292 314
293 // Set a repeating timer with a preposterous firing time and interval. The 315 // 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 316 // 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_); 453 CFRunLoopSourceSignal(work_source_);
432 CFRunLoopWakeUp(run_loop_); 454 CFRunLoopWakeUp(run_loop_);
433 } 455 }
434 456
435 // Must be called on the run loop thread. 457 // Must be called on the run loop thread.
436 void MessagePumpCFRunLoopBase::ScheduleDelayedWork( 458 void MessagePumpCFRunLoopBase::ScheduleDelayedWork(
437 const TimeTicks& delayed_work_time) { 459 const TimeTicks& delayed_work_time) {
438 TimeDelta delta = delayed_work_time - TimeTicks::Now(); 460 TimeDelta delta = delayed_work_time - TimeTicks::Now();
439 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF(); 461 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF();
440 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_); 462 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_);
463 if (timer_slack_ == TIMER_SLACK_MAXIMUM) {
464 SetTimerTolerance(delayed_work_timer_, delta.InSecondsF() * 0.5);
465 } else {
466 SetTimerTolerance(delayed_work_timer_, 0);
467 }
468 }
469
470 void MessagePumpCFRunLoopBase::SetTimerSlack(TimerSlack timer_slack) {
471 timer_slack_ = timer_slack;
441 } 472 }
442 473
443 // Called from the run loop. 474 // Called from the run loop.
444 // static 475 // static
445 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer, 476 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer,
446 void* info) { 477 void* info) {
447 MessagePumpCFRunLoopBase* self = static_cast<MessagePumpCFRunLoopBase*>(info); 478 MessagePumpCFRunLoopBase* self = static_cast<MessagePumpCFRunLoopBase*>(info);
448 479
449 // The timer won't fire again until it's reset. 480 // The timer won't fire again until it's reset.
450 self->delayed_work_fire_time_ = kCFTimeIntervalMax; 481 self->delayed_work_fire_time_ = kCFTimeIntervalMax;
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after
954 [NSApplication sharedApplication]; 985 [NSApplication sharedApplication];
955 g_not_using_cr_app = true; 986 g_not_using_cr_app = true;
956 return new MessagePumpNSApplication; 987 return new MessagePumpNSApplication;
957 #endif 988 #endif
958 } 989 }
959 990
960 return new MessagePumpNSRunLoop; 991 return new MessagePumpNSRunLoop;
961 } 992 }
962 993
963 } // namespace base 994 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698