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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_TIMER_RTC_ALARM_CHROMEOS_H_
6 #define BASE_TIMER_RTC_ALARM_CHROMEOS_H_
7
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/time/time.h"
14 #include "base/timer/alarm_timer.h"
15
16 namespace base {
17 class MessageLoopProxy;
18
19 // This class manages a Real Time Clock (RTC) alarm, a feature that is available
20 // from linux version 3.11 onwards. It creates a file descriptor for the RTC
21 // alarm timer and then watches that file descriptor to see when it can be read
22 // without blocking, indicating that the timer has fired.
23 //
24 // A major problem for this class is that watching file descriptors is only
25 // available on a MessageLoopForIO but there is no guarantee the timer is going
26 // to be created on one. To get around this, the timer has a dedicated thread
27 // with a MessageLoopForIO that posts tasks back to the thread that started the
28 // timer.
29 //
30 // This class is tested through the AlarmTimer unittests.
31 class RtcAlarmChromeos : public AlarmTimer::Delegate,
32 public MessageLoopForIO::Watcher {
33 public:
34 RtcAlarmChromeos();
35
36 // AlarmTimer::Delagate overrides.
37 virtual bool Init(WeakPtr<AlarmTimer> timer) override;
38 virtual void Stop() override;
39 virtual void Reset(TimeDelta delay) override;
40
41 // MessageLoopForIO::Watcher overrides.
42 virtual void OnFileCanReadWithoutBlocking(int fd) override;
43 virtual void OnFileCanWriteWithoutBlocking(int fd) override;
44
45 protected:
46 // Needs to be protected because AlarmTimer::Delegate is a refcounted class.
47 virtual ~RtcAlarmChromeos();
48
49 private:
50 // Actually performs the system calls to set up the timer. This must be
51 // called on a MessageLoopForIO.
52 void ResetImpl(TimeDelta delay, int event_id);
53
54 // Callback that is run when the timer fires. Must be run on
55 // |origin_message_loop_|.
56 void OnTimerFired(int event_id);
57
58 // File descriptor associated with the alarm timer.
59 int alarm_fd_;
60
61 // Message loop which initially started the timer.
62 scoped_refptr<MessageLoopProxy> origin_message_loop_;
63
64 // The parent timer that should be informed when the timer fires. We may end
65 // up outliving the parent so we need to ensure the reference is valid before
66 // we try to call it.
67 WeakPtr<AlarmTimer> parent_;
68
69 // Manages watching file descriptors.
70 scoped_ptr<MessageLoopForIO::FileDescriptorWatcher> fd_watcher_;
71
72 // These two variables are used for coordinating between the thread that
73 // started the timer and the IO thread being used to watch the timer file
74 // descriptor. When Reset() is called, the original thread increments
75 // |origin_event_id_| and binds its value to ResetImpl(), which gets posted to
76 // the IO thread. When the IO thread runs, it saves this value in
77 // |io_event_id_|. Later, when the timer fires, the IO thread binds the value
78 // of |io_event_id_| to OnTimerFired() and posts it to the original thread.
79 // When the original thread runs OnTimerFired(), it calls
80 // parent_->OnTimerFired() only if |origin_event_id_| matches the event id
81 // that was passed in to it. This is used to get around a race condition
82 // where the user resets the timer on the original thread, while the event is
83 // being fired on the IO thread at the same time.
84 int origin_event_id_;
85 int io_event_id_;
86
87 DISALLOW_COPY_AND_ASSIGN(RtcAlarmChromeos);
88 };
89
90 } // namespace base
91 #endif // BASE_TIMER_RTC_ALARM_CHROMEOS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698