Bug 1755481: correct documentation of `nsIClipboard::getData`. r=mccr8
[gecko.git] / xpcom / threads / TimerThread.h
blobf2ee487a666845339bb28b0ca8251c3fd6ef874e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef TimerThread_h___
8 #define TimerThread_h___
10 #include "nsIObserver.h"
11 #include "nsIRunnable.h"
12 #include "nsIThread.h"
14 #include "nsTimerImpl.h"
15 #include "nsThreadUtils.h"
17 #include "nsTArray.h"
19 #include "mozilla/Atomics.h"
20 #include "mozilla/Attributes.h"
21 #include "mozilla/Monitor.h"
22 #include "mozilla/ProfilerUtils.h"
23 #include "mozilla/UniquePtr.h"
25 #include <algorithm>
27 namespace mozilla {
28 class TimeStamp;
29 } // namespace mozilla
31 class TimerThread final : public mozilla::Runnable, public nsIObserver {
32 public:
33 typedef mozilla::Monitor Monitor;
34 typedef mozilla::MutexAutoLock MutexAutoLock;
35 typedef mozilla::TimeStamp TimeStamp;
36 typedef mozilla::TimeDuration TimeDuration;
38 TimerThread();
39 nsresult InitLocks();
41 NS_DECL_ISUPPORTS_INHERITED
42 NS_DECL_NSIRUNNABLE
43 NS_DECL_NSIOBSERVER
45 nsresult Shutdown();
47 nsresult AddTimer(nsTimerImpl* aTimer, const MutexAutoLock& aProofOfLock);
48 nsresult RemoveTimer(nsTimerImpl* aTimer, const MutexAutoLock& aProofOfLock);
49 TimeStamp FindNextFireTimeForCurrentThread(TimeStamp aDefault,
50 uint32_t aSearchBound);
52 void DoBeforeSleep();
53 void DoAfterSleep();
55 bool IsOnTimerThread() const {
56 return mThread->SerialEventTarget()->IsOnCurrentThread();
59 uint32_t AllowedEarlyFiringMicroseconds();
61 private:
62 ~TimerThread();
64 bool mInitialized;
66 // These internal helper methods must be called while mMonitor is held.
67 // AddTimerInternal returns false if the insertion failed.
68 bool AddTimerInternal(nsTimerImpl* aTimer);
69 bool RemoveTimerInternal(nsTimerImpl* aTimer);
70 void RemoveLeadingCanceledTimersInternal();
71 void RemoveFirstTimerInternal();
72 nsresult Init();
74 void PostTimerEvent(already_AddRefed<nsTimerImpl> aTimerRef);
76 nsCOMPtr<nsIThread> mThread;
77 // Lock ordering requirements:
78 // (optional) ThreadWrapper::sMutex ->
79 // (optional) nsTimerImpl::mMutex ->
80 // TimerThread::mMonitor
81 Monitor mMonitor;
83 bool mShutdown;
84 bool mWaiting;
85 bool mNotified;
86 bool mSleeping;
88 class Entry final : public nsTimerImplHolder {
89 const TimeStamp mTimeout;
91 public:
92 // Entries are created with the TimerImpl's mutex held.
93 // nsTimerImplHolder() will call SetHolder()
94 Entry(const TimeStamp& aMinTimeout, const TimeStamp& aTimeout,
95 nsTimerImpl* aTimerImpl)
96 : nsTimerImplHolder(aTimerImpl),
97 mTimeout(std::max(aMinTimeout, aTimeout)) {}
99 nsTimerImpl* Value() const { return mTimerImpl; }
101 // Called with the Monitor held, but not the TimerImpl's mutex
102 already_AddRefed<nsTimerImpl> Take() {
103 if (mTimerImpl) {
104 MOZ_ASSERT(mTimerImpl->mHolder == this);
105 mTimerImpl->SetHolder(nullptr);
107 return mTimerImpl.forget();
110 static bool UniquePtrLessThan(mozilla::UniquePtr<Entry>& aLeft,
111 mozilla::UniquePtr<Entry>& aRight) {
112 // This is reversed because std::push_heap() sorts the "largest" to
113 // the front of the heap. We want that to be the earliest timer.
114 return aRight->mTimeout < aLeft->mTimeout;
117 TimeStamp Timeout() const { return mTimeout; }
120 nsTArray<mozilla::UniquePtr<Entry>> mTimers;
121 // Set only at the start of the thread's Run():
122 uint32_t mAllowedEarlyFiringMicroseconds;
123 ProfilerThreadId mProfilerThreadId;
126 #endif /* TimerThread_h___ */