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

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: Really rebase 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
« no previous file with comments | « base/message_loop/message_pump_mac.h ('k') | base/message_loop/timer_slack.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include <dlfcn.h>
7 #import <Foundation/Foundation.h> 8 #import <Foundation/Foundation.h>
8 9
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"
16 #include "base/message_loop/timer_slack.h"
15 #include "base/metrics/histogram.h" 17 #include "base/metrics/histogram.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;
47
48 static dispatch_once_t get_timer_tolerance_function_ptr_once;
49 dispatch_once(&get_timer_tolerance_function_ptr_once, ^{
50 NSBundle* bundle =[NSBundle
51 bundleWithPath:@"/System/Library/Frameworks/CoreFoundation.framework"];
52 const char* path = [[bundle executablePath] fileSystemRepresentation];
53 CHECK(path);
54 void* library_handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
55 CHECK(library_handle) << dlerror();
56 settimertolerance_function_ptr =
57 reinterpret_cast<CFRunLoopTimerSetTolerancePtr>(
58 dlsym(library_handle, "CFRunLoopTimerSetTolerance"));
59
60 dlclose(library_handle);
61 });
62
63 if (settimertolerance_function_ptr)
64 settimertolerance_function_ptr(timer, tolerance);
65 }
66
38 } // namespace 67 } // namespace
39 68
40 namespace base { 69 namespace base {
41 70
42 // A scoper for autorelease pools created from message pump run loops. 71 // A scoper for autorelease pools created from message pump run loops.
43 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare 72 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare
44 // case where an autorelease pool needs to be passed in. 73 // case where an autorelease pool needs to be passed in.
45 class MessagePumpScopedAutoreleasePool { 74 class MessagePumpScopedAutoreleasePool {
46 public: 75 public:
47 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) : 76 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) :
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 std::stack<TimeTicks> loop_wait_times_; 304 std::stack<TimeTicks> loop_wait_times_;
276 std::stack<TimeTicks> work_source_times_; 305 std::stack<TimeTicks> work_source_times_;
277 306
278 DISALLOW_COPY_AND_ASSIGN(MessagePumpInstrumentation); 307 DISALLOW_COPY_AND_ASSIGN(MessagePumpInstrumentation);
279 }; 308 };
280 309
281 // Must be called on the run loop thread. 310 // Must be called on the run loop thread.
282 MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase() 311 MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
283 : delegate_(NULL), 312 : delegate_(NULL),
284 delayed_work_fire_time_(kCFTimeIntervalMax), 313 delayed_work_fire_time_(kCFTimeIntervalMax),
314 timer_slack_(base::TIMER_SLACK_NONE),
285 nesting_level_(0), 315 nesting_level_(0),
286 run_nesting_level_(0), 316 run_nesting_level_(0),
287 deepest_nesting_level_(0), 317 deepest_nesting_level_(0),
288 delegateless_work_(false), 318 delegateless_work_(false),
289 delegateless_idle_work_(false) { 319 delegateless_idle_work_(false) {
290 run_loop_ = CFRunLoopGetCurrent(); 320 run_loop_ = CFRunLoopGetCurrent();
291 CFRetain(run_loop_); 321 CFRetain(run_loop_);
292 322
293 // Set a repeating timer with a preposterous firing time and interval. The 323 // 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 324 // 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_); 461 CFRunLoopSourceSignal(work_source_);
432 CFRunLoopWakeUp(run_loop_); 462 CFRunLoopWakeUp(run_loop_);
433 } 463 }
434 464
435 // Must be called on the run loop thread. 465 // Must be called on the run loop thread.
436 void MessagePumpCFRunLoopBase::ScheduleDelayedWork( 466 void MessagePumpCFRunLoopBase::ScheduleDelayedWork(
437 const TimeTicks& delayed_work_time) { 467 const TimeTicks& delayed_work_time) {
438 TimeDelta delta = delayed_work_time - TimeTicks::Now(); 468 TimeDelta delta = delayed_work_time - TimeTicks::Now();
439 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF(); 469 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF();
440 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_); 470 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_);
471 if (timer_slack_ == TIMER_SLACK_MAXIMUM) {
472 SetTimerTolerance(delayed_work_timer_, delta.InSecondsF() * 0.5);
473 } else {
474 SetTimerTolerance(delayed_work_timer_, 0);
475 }
476 }
477
478 void MessagePumpCFRunLoopBase::SetTimerSlack(TimerSlack timer_slack) {
479 timer_slack_ = timer_slack;
441 } 480 }
442 481
443 // Called from the run loop. 482 // Called from the run loop.
444 // static 483 // static
445 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer, 484 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer,
446 void* info) { 485 void* info) {
447 MessagePumpCFRunLoopBase* self = static_cast<MessagePumpCFRunLoopBase*>(info); 486 MessagePumpCFRunLoopBase* self = static_cast<MessagePumpCFRunLoopBase*>(info);
448 487
449 // The timer won't fire again until it's reset. 488 // The timer won't fire again until it's reset.
450 self->delayed_work_fire_time_ = kCFTimeIntervalMax; 489 self->delayed_work_fire_time_ = kCFTimeIntervalMax;
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after
954 [NSApplication sharedApplication]; 993 [NSApplication sharedApplication];
955 g_not_using_cr_app = true; 994 g_not_using_cr_app = true;
956 return new MessagePumpNSApplication; 995 return new MessagePumpNSApplication;
957 #endif 996 #endif
958 } 997 }
959 998
960 return new MessagePumpNSRunLoop; 999 return new MessagePumpNSRunLoop;
961 } 1000 }
962 1001
963 } // namespace base 1002 } // namespace base
OLDNEW
« no previous file with comments | « base/message_loop/message_pump_mac.h ('k') | base/message_loop/timer_slack.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698