2014-06-30 15:39:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 11:12:37 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-12-16 06:13:17 +00:00
|
|
|
|
|
|
|
#ifndef TimerThread_h___
|
|
|
|
#define TimerThread_h___
|
|
|
|
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
|
|
|
|
#include "nsTimerImpl.h"
|
2013-03-05 00:04:59 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2009-04-03 16:43:08 +00:00
|
|
|
#include "nsTArray.h"
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2013-08-22 15:14:42 +00:00
|
|
|
#include "mozilla/Atomics.h"
|
2012-06-05 23:51:58 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-04-29 19:21:57 +00:00
|
|
|
#include "mozilla/Monitor.h"
|
2013-08-24 06:12:51 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
class TimeStamp;
|
|
|
|
}
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
class TimerThread MOZ_FINAL
|
|
|
|
: public nsIRunnable
|
|
|
|
, public nsIObserver
|
2001-12-16 06:13:17 +00:00
|
|
|
{
|
|
|
|
public:
|
2011-04-29 19:21:57 +00:00
|
|
|
typedef mozilla::Monitor Monitor;
|
2010-07-15 13:59:24 +00:00
|
|
|
typedef mozilla::TimeStamp TimeStamp;
|
|
|
|
typedef mozilla::TimeDuration TimeDuration;
|
|
|
|
|
2001-12-16 06:13:17 +00:00
|
|
|
TimerThread();
|
2014-06-02 12:08:21 +00:00
|
|
|
nsresult InitLocks();
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2001-12-16 06:13:17 +00:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2003-10-31 02:31:13 +00:00
|
|
|
NS_DECL_NSIOBSERVER
|
2014-02-07 06:05:24 +00:00
|
|
|
|
2014-06-02 12:08:21 +00:00
|
|
|
nsresult Init();
|
|
|
|
nsresult Shutdown();
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
nsresult AddTimer(nsTimerImpl* aTimer);
|
|
|
|
nsresult TimerDelayChanged(nsTimerImpl* aTimer);
|
|
|
|
nsresult RemoveTimer(nsTimerImpl* aTimer);
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2003-10-31 02:31:13 +00:00
|
|
|
void DoBeforeSleep();
|
|
|
|
void DoAfterSleep();
|
|
|
|
|
2013-03-05 00:04:59 +00:00
|
|
|
bool IsOnTimerThread() const
|
|
|
|
{
|
|
|
|
return mThread == NS_GetCurrentThread();
|
|
|
|
}
|
|
|
|
|
2001-12-16 06:13:17 +00:00
|
|
|
private:
|
2004-01-15 06:14:18 +00:00
|
|
|
~TimerThread();
|
|
|
|
|
2014-02-07 06:05:24 +00:00
|
|
|
mozilla::Atomic<bool> mInitInProgress;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mInitialized;
|
2004-05-11 09:38:50 +00:00
|
|
|
|
2002-04-25 21:07:54 +00:00
|
|
|
// These two internal helper methods must be called while mLock is held.
|
|
|
|
// AddTimerInternal returns the position where the timer was added in the
|
|
|
|
// list, or -1 if it failed.
|
2014-05-27 07:15:35 +00:00
|
|
|
int32_t AddTimerInternal(nsTimerImpl* aTimer);
|
|
|
|
bool RemoveTimerInternal(nsTimerImpl* aTimer);
|
|
|
|
void ReleaseTimerInternal(nsTimerImpl* aTimer);
|
2001-12-16 06:13:17 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
2011-04-29 19:21:57 +00:00
|
|
|
Monitor mMonitor;
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mShutdown;
|
|
|
|
bool mWaiting;
|
2014-03-03 05:12:51 +00:00
|
|
|
bool mNotified;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mSleeping;
|
2014-02-07 06:05:24 +00:00
|
|
|
|
2009-04-03 16:43:08 +00:00
|
|
|
nsTArray<nsTimerImpl*> mTimers;
|
2001-12-16 06:13:17 +00:00
|
|
|
};
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
struct TimerAdditionComparator
|
|
|
|
{
|
|
|
|
TimerAdditionComparator(const mozilla::TimeStamp& aNow,
|
|
|
|
nsTimerImpl* aTimerToInsert) :
|
2013-02-20 18:21:09 +00:00
|
|
|
now(aNow)
|
2013-02-13 15:11:53 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
, timerToInsert(aTimerToInsert)
|
|
|
|
#endif
|
2014-05-27 07:15:35 +00:00
|
|
|
{
|
|
|
|
}
|
2013-02-13 15:11:53 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
bool LessThan(nsTimerImpl* aFromArray, nsTimerImpl* aNewTimer) const
|
|
|
|
{
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(aNewTimer == timerToInsert, "Unexpected timer ordering");
|
2013-02-13 15:11:53 +00:00
|
|
|
|
|
|
|
// Skip any overdue timers.
|
2014-05-27 07:15:35 +00:00
|
|
|
return aFromArray->mTimeout <= now ||
|
|
|
|
aFromArray->mTimeout <= aNewTimer->mTimeout;
|
2013-02-13 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
bool Equals(nsTimerImpl* aFromArray, nsTimerImpl* aNewTimer) const
|
|
|
|
{
|
2013-04-04 17:28:18 +00:00
|
|
|
return false;
|
2013-02-13 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-05-27 07:15:35 +00:00
|
|
|
const mozilla::TimeStamp& now;
|
2013-02-13 15:11:53 +00:00
|
|
|
#ifdef DEBUG
|
2014-05-27 07:15:35 +00:00
|
|
|
const nsTimerImpl* const timerToInsert;
|
2013-02-13 15:11:53 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2001-12-16 06:13:17 +00:00
|
|
|
#endif /* TimerThread_h___ */
|