1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/threads/TimerThread.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef TimerThread_h___ 1.10 +#define TimerThread_h___ 1.11 + 1.12 +#include "nsIObserver.h" 1.13 +#include "nsIRunnable.h" 1.14 +#include "nsIThread.h" 1.15 + 1.16 +#include "nsTimerImpl.h" 1.17 +#include "nsThreadUtils.h" 1.18 + 1.19 +#include "nsTArray.h" 1.20 + 1.21 +#include "mozilla/Atomics.h" 1.22 +#include "mozilla/Attributes.h" 1.23 +#include "mozilla/Monitor.h" 1.24 + 1.25 +namespace mozilla { 1.26 +class TimeStamp; 1.27 +} 1.28 + 1.29 +class TimerThread MOZ_FINAL : public nsIRunnable, 1.30 + public nsIObserver 1.31 +{ 1.32 +public: 1.33 + typedef mozilla::Monitor Monitor; 1.34 + typedef mozilla::TimeStamp TimeStamp; 1.35 + typedef mozilla::TimeDuration TimeDuration; 1.36 + 1.37 + TimerThread(); 1.38 + NS_HIDDEN_(nsresult) InitLocks(); 1.39 + 1.40 + NS_DECL_THREADSAFE_ISUPPORTS 1.41 + NS_DECL_NSIRUNNABLE 1.42 + NS_DECL_NSIOBSERVER 1.43 + 1.44 + NS_HIDDEN_(nsresult) Init(); 1.45 + NS_HIDDEN_(nsresult) Shutdown(); 1.46 + 1.47 + nsresult AddTimer(nsTimerImpl *aTimer); 1.48 + nsresult TimerDelayChanged(nsTimerImpl *aTimer); 1.49 + nsresult RemoveTimer(nsTimerImpl *aTimer); 1.50 + 1.51 + void DoBeforeSleep(); 1.52 + void DoAfterSleep(); 1.53 + 1.54 + bool IsOnTimerThread() const 1.55 + { 1.56 + return mThread == NS_GetCurrentThread(); 1.57 + } 1.58 + 1.59 +private: 1.60 + ~TimerThread(); 1.61 + 1.62 + mozilla::Atomic<bool> mInitInProgress; 1.63 + bool mInitialized; 1.64 + 1.65 + // These two internal helper methods must be called while mLock is held. 1.66 + // AddTimerInternal returns the position where the timer was added in the 1.67 + // list, or -1 if it failed. 1.68 + int32_t AddTimerInternal(nsTimerImpl *aTimer); 1.69 + bool RemoveTimerInternal(nsTimerImpl *aTimer); 1.70 + void ReleaseTimerInternal(nsTimerImpl *aTimer); 1.71 + 1.72 + nsCOMPtr<nsIThread> mThread; 1.73 + Monitor mMonitor; 1.74 + 1.75 + bool mShutdown; 1.76 + bool mWaiting; 1.77 + bool mNotified; 1.78 + bool mSleeping; 1.79 + 1.80 + nsTArray<nsTimerImpl*> mTimers; 1.81 +}; 1.82 + 1.83 +struct TimerAdditionComparator { 1.84 + TimerAdditionComparator(const mozilla::TimeStamp &aNow, 1.85 + nsTimerImpl *aTimerToInsert) : 1.86 + now(aNow) 1.87 +#ifdef DEBUG 1.88 + , timerToInsert(aTimerToInsert) 1.89 +#endif 1.90 + {} 1.91 + 1.92 + bool LessThan(nsTimerImpl *fromArray, nsTimerImpl *newTimer) const { 1.93 + NS_ABORT_IF_FALSE(newTimer == timerToInsert, "Unexpected timer ordering"); 1.94 + 1.95 + // Skip any overdue timers. 1.96 + return fromArray->mTimeout <= now || 1.97 + fromArray->mTimeout <= newTimer->mTimeout; 1.98 + } 1.99 + 1.100 + bool Equals(nsTimerImpl* fromArray, nsTimerImpl* newTimer) const { 1.101 + return false; 1.102 + } 1.103 + 1.104 +private: 1.105 + const mozilla::TimeStamp &now; 1.106 +#ifdef DEBUG 1.107 + const nsTimerImpl * const timerToInsert; 1.108 +#endif 1.109 +}; 1.110 + 1.111 +#endif /* TimerThread_h___ */