Backed out changesets d8fd745a0095 and 30b7ebdf5c99 (bug 924480) for robocop-3 failures.
[gecko.git] / xpcom / threads / TimerThread.h
blob81d4f6cd9cacf3ad02653fa7bfd3587c23f63ba9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef TimerThread_h___
7 #define TimerThread_h___
9 #include "nsIObserver.h"
10 #include "nsIRunnable.h"
11 #include "nsIThread.h"
13 #include "nsTimerImpl.h"
14 #include "nsThreadUtils.h"
16 #include "nsTArray.h"
18 #include "mozilla/Atomics.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/Monitor.h"
22 namespace mozilla {
23 class TimeStamp;
26 class TimerThread MOZ_FINAL : public nsIRunnable,
27 public nsIObserver
29 public:
30 typedef mozilla::Monitor Monitor;
31 typedef mozilla::TimeStamp TimeStamp;
32 typedef mozilla::TimeDuration TimeDuration;
34 TimerThread();
35 NS_HIDDEN_(nsresult) InitLocks();
37 NS_DECL_THREADSAFE_ISUPPORTS
38 NS_DECL_NSIRUNNABLE
39 NS_DECL_NSIOBSERVER
41 NS_HIDDEN_(nsresult) Init();
42 NS_HIDDEN_(nsresult) Shutdown();
44 nsresult AddTimer(nsTimerImpl *aTimer);
45 nsresult TimerDelayChanged(nsTimerImpl *aTimer);
46 nsresult RemoveTimer(nsTimerImpl *aTimer);
48 void DoBeforeSleep();
49 void DoAfterSleep();
51 bool IsOnTimerThread() const
53 return mThread == NS_GetCurrentThread();
56 private:
57 ~TimerThread();
59 mozilla::Atomic<int32_t> mInitInProgress;
60 bool mInitialized;
62 // These two internal helper methods must be called while mLock is held.
63 // AddTimerInternal returns the position where the timer was added in the
64 // list, or -1 if it failed.
65 int32_t AddTimerInternal(nsTimerImpl *aTimer);
66 bool RemoveTimerInternal(nsTimerImpl *aTimer);
67 void ReleaseTimerInternal(nsTimerImpl *aTimer);
69 nsCOMPtr<nsIThread> mThread;
70 Monitor mMonitor;
72 bool mShutdown;
73 bool mWaiting;
74 bool mSleeping;
76 nsTArray<nsTimerImpl*> mTimers;
79 struct TimerAdditionComparator {
80 TimerAdditionComparator(const mozilla::TimeStamp &aNow,
81 nsTimerImpl *aTimerToInsert) :
82 now(aNow)
83 #ifdef DEBUG
84 , timerToInsert(aTimerToInsert)
85 #endif
88 bool LessThan(nsTimerImpl *fromArray, nsTimerImpl *newTimer) const {
89 NS_ABORT_IF_FALSE(newTimer == timerToInsert, "Unexpected timer ordering");
91 // Skip any overdue timers.
92 return fromArray->mTimeout <= now ||
93 fromArray->mTimeout <= newTimer->mTimeout;
96 bool Equals(nsTimerImpl* fromArray, nsTimerImpl* newTimer) const {
97 return false;
100 private:
101 const mozilla::TimeStamp &now;
102 #ifdef DEBUG
103 const nsTimerImpl * const timerToInsert;
104 #endif
107 #endif /* TimerThread_h___ */