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"
18 #include "mozilla/Atomics.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/Monitor.h"
26 class TimerThread MOZ_FINAL
: public nsIRunnable
,
30 typedef mozilla::Monitor Monitor
;
31 typedef mozilla::TimeStamp TimeStamp
;
32 typedef mozilla::TimeDuration TimeDuration
;
35 NS_HIDDEN_(nsresult
) InitLocks();
37 NS_DECL_THREADSAFE_ISUPPORTS
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
);
51 bool IsOnTimerThread() const
53 return mThread
== NS_GetCurrentThread();
59 mozilla::Atomic
<int32_t> mInitInProgress
;
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
;
76 nsTArray
<nsTimerImpl
*> mTimers
;
79 struct TimerAdditionComparator
{
80 TimerAdditionComparator(const mozilla::TimeStamp
&aNow
,
81 nsTimerImpl
*aTimerToInsert
) :
84 , timerToInsert(aTimerToInsert
)
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 {
101 const mozilla::TimeStamp
&now
;
103 const nsTimerImpl
* const timerToInsert
;
107 #endif /* TimerThread_h___ */