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

Unified Diff: base/timer/rtc_alarm_chromeos.h

Issue 641943002: components: Introduce AlarmTimer class and use it for GCM heartbeat (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make some Timer variables protected to reduce duplication, clean up comments Created 6 years, 2 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/timer/rtc_alarm_chromeos.h
diff --git a/base/timer/rtc_alarm_chromeos.h b/base/timer/rtc_alarm_chromeos.h
new file mode 100644
index 0000000000000000000000000000000000000000..187ddaccef0e269b5c361ad9e9d5b5396d9650a2
--- /dev/null
+++ b/base/timer/rtc_alarm_chromeos.h
@@ -0,0 +1,91 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_TIMER_RTC_ALARM_CHROMEOS_H_
+#define BASE_TIMER_RTC_ALARM_CHROMEOS_H_
+
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/time/time.h"
+#include "base/timer/alarm_timer.h"
+
+namespace base {
+class MessageLoopProxy;
+
+// This class manages a Real Time Clock (RTC) alarm, a feature that is available
+// from linux version 3.11 onwards. It creates a file descriptor for the RTC
+// alarm timer and then watches that file descriptor to see when it can be read
+// without blocking, indicating that the timer has fired.
+//
+// A major problem for this class is that watching file descriptors is only
+// available on a MessageLoopForIO but there is no guarantee the timer is going
+// to be created on one. To get around this, the timer has a dedicated thread
+// with a MessageLoopForIO that posts tasks back to the thread that started the
+// timer.
+//
+// This class is tested through the AlarmTimer unittests.
+class RtcAlarmChromeos : public AlarmTimer::Delegate,
+ public MessageLoopForIO::Watcher {
+ public:
+ RtcAlarmChromeos();
+
+ // AlarmTimer::Delagate overrides.
+ virtual bool Init(WeakPtr<AlarmTimer> timer) override;
+ virtual void Stop() override;
+ virtual void Reset(TimeDelta delay) override;
+
+ // MessageLoopForIO::Watcher overrides.
+ virtual void OnFileCanReadWithoutBlocking(int fd) override;
+ virtual void OnFileCanWriteWithoutBlocking(int fd) override;
+
+ protected:
+ // Needs to be protected because AlarmTimer::Delegate is a refcounted class.
+ virtual ~RtcAlarmChromeos();
+
+ private:
+ // Actually performs the system calls to set up the timer. This must be
+ // called on a MessageLoopForIO.
+ void ResetImpl(TimeDelta delay, int event_id);
+
+ // Callback that is run when the timer fires. Must be run on
+ // |origin_message_loop_|.
+ void OnTimerFired(int event_id);
+
+ // File descriptor associated with the alarm timer.
+ int alarm_fd_;
+
+ // Message loop which initially started the timer.
+ scoped_refptr<MessageLoopProxy> origin_message_loop_;
+
+ // The parent timer that should be informed when the timer fires. We may end
+ // up outliving the parent so we need to ensure the reference is valid before
+ // we try to call it.
+ WeakPtr<AlarmTimer> parent_;
+
+ // Manages watching file descriptors.
+ scoped_ptr<MessageLoopForIO::FileDescriptorWatcher> fd_watcher_;
+
+ // These two variables are used for coordinating between the thread that
+ // started the timer and the IO thread being used to watch the timer file
+ // descriptor. When Reset() is called, the original thread increments
+ // |origin_event_id_| and binds its value to ResetImpl(), which gets posted to
+ // the IO thread. When the IO thread runs, it saves this value in
+ // |io_event_id_|. Later, when the timer fires, the IO thread binds the value
+ // of |io_event_id_| to OnTimerFired() and posts it to the original thread.
+ // When the original thread runs OnTimerFired(), it calls
+ // parent_->OnTimerFired() only if |origin_event_id_| matches the event id
+ // that was passed in to it. This is used to get around a race condition
+ // where the user resets the timer on the original thread, while the event is
+ // being fired on the IO thread at the same time.
+ int origin_event_id_;
+ int io_event_id_;
+
+ DISALLOW_COPY_AND_ASSIGN(RtcAlarmChromeos);
+};
+
+} // namespace base
+#endif // BASE_TIMER_RTC_ALARM_CHROMEOS_H_

Powered by Google App Engine
This is Rietveld 408576698