xpcom/threads/TimerThread.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial