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

Unified 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, 7 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 side-by-side diff with in-line comments
Download patch
Index: base/message_loop/message_pump_mac.mm
diff --git a/base/message_loop/message_pump_mac.mm b/base/message_loop/message_pump_mac.mm
index 891cc347448ff70b30f9357ff431d46aad613f59..04ecb84c0bf9a66f9f0748e20425f0aafd801b49 100644
--- a/base/message_loop/message_pump_mac.mm
+++ b/base/message_loop/message_pump_mac.mm
@@ -6,6 +6,7 @@
#import <Foundation/Foundation.h>
+#include <dlfcn.h>
#include <limits>
#include <stack>
@@ -13,6 +14,7 @@
#include "base/logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/metrics/histogram.h"
+#include "base/message_loop/timer_slack.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
@@ -35,6 +37,31 @@ const CFTimeInterval kCFTimeIntervalMax =
bool g_not_using_cr_app = false;
#endif
+// Call through to CFRunLoopTimerSetTolerance(), which is only available on
+// OS X 10.9.
+void SetTimerTolerance(CFRunLoopTimerRef timer, CFTimeInterval tolerance) {
+ typedef void (*CFRunLoopTimerSetTolerancePtr)(CFRunLoopTimerRef timer,
+ CFTimeInterval tolerance);
+
+ static CFRunLoopTimerSetTolerancePtr settimertolerance_function_ptr = NULL;
+ 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.
+
+ if (function_lookup_error)
+ return;
+
+ if (!settimertolerance_function_ptr) {
+ settimertolerance_function_ptr =
+ reinterpret_cast<CFRunLoopTimerSetTolerancePtr>(
+ dlsym(RTLD_DEFAULT, "CFRunLoopTimerSetTolerance"));
+ if (!settimertolerance_function_ptr) {
+ function_lookup_error = true;
+ return;
+ }
+ }
+
+ settimertolerance_function_ptr(timer, tolerance);
+}
+
} // namespace
namespace base {
@@ -282,6 +309,7 @@ class MessagePumpInstrumentation {
MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
: delegate_(NULL),
delayed_work_fire_time_(kCFTimeIntervalMax),
+ timer_slack_(base::TIMER_SLACK_NONE),
nesting_level_(0),
run_nesting_level_(0),
deepest_nesting_level_(0),
@@ -438,6 +466,15 @@ void MessagePumpCFRunLoopBase::ScheduleDelayedWork(
TimeDelta delta = delayed_work_time - TimeTicks::Now();
delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF();
CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_);
+ if (timer_slack_ == TIMER_SLACK_MAXIMUM) {
+ SetTimerTolerance(delayed_work_timer_, delta.InSecondsF() * 0.5);
+ } else {
+ SetTimerTolerance(delayed_work_timer_, 0);
+ }
+}
+
+void MessagePumpCFRunLoopBase::SetTimerSlack(TimerSlack timer_slack) {
+ timer_slack_ = timer_slack;
}
// Called from the run loop.

Powered by Google App Engine
This is Rietveld 408576698