2014-06-30 19:39:45 +04: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 15:12:37 +04: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 09:13:17 +03:00
|
|
|
|
|
|
|
#ifndef TimerThread_h___
|
|
|
|
#define TimerThread_h___
|
|
|
|
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
|
|
|
|
#include "nsTimerImpl.h"
|
2013-03-05 04:04:59 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2009-04-03 20:43:08 +04:00
|
|
|
#include "nsTArray.h"
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2013-08-22 19:14:42 +04:00
|
|
|
#include "mozilla/Atomics.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-04-29 23:21:57 +04:00
|
|
|
#include "mozilla/Monitor.h"
|
2013-08-24 10:12:51 +04:00
|
|
|
|
2017-04-21 00:56:07 +03:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-08-24 10:12:51 +04:00
|
|
|
namespace mozilla {
|
|
|
|
class TimeStamp;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class TimerThread final
|
2014-05-27 11:15:35 +04:00
|
|
|
: public nsIRunnable
|
|
|
|
, public nsIObserver
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
|
|
|
public:
|
2011-04-29 23:21:57 +04:00
|
|
|
typedef mozilla::Monitor Monitor;
|
2010-07-15 17:59:24 +04:00
|
|
|
typedef mozilla::TimeStamp TimeStamp;
|
|
|
|
typedef mozilla::TimeDuration TimeDuration;
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
TimerThread();
|
2014-06-02 16:08:21 +04:00
|
|
|
nsresult InitLocks();
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2001-12-16 09:13:17 +03:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2003-10-31 05:31:13 +03:00
|
|
|
NS_DECL_NSIOBSERVER
|
2014-02-07 10:05:24 +04:00
|
|
|
|
2014-06-02 16:08:21 +04:00
|
|
|
nsresult Shutdown();
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsresult AddTimer(nsTimerImpl* aTimer);
|
2017-01-11 22:59:19 +03:00
|
|
|
nsresult RemoveTimer(nsTimerImpl* aTimer);
|
2017-05-25 04:14:29 +03:00
|
|
|
TimeStamp FindNextFireTimeForCurrentThread(TimeStamp aDefault, uint32_t aSearchBound);
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2003-10-31 05:31:13 +03:00
|
|
|
void DoBeforeSleep();
|
|
|
|
void DoAfterSleep();
|
|
|
|
|
2013-03-05 04:04:59 +04:00
|
|
|
bool IsOnTimerThread() const
|
|
|
|
{
|
2017-06-01 23:44:20 +03:00
|
|
|
return mThread->SerialEventTarget()->IsOnCurrentThread();
|
2013-03-05 04:04:59 +04:00
|
|
|
}
|
|
|
|
|
2017-06-01 03:13:20 +03:00
|
|
|
uint32_t
|
|
|
|
AllowedEarlyFiringMicroseconds() const;
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
private:
|
2004-01-15 09:14:18 +03:00
|
|
|
~TimerThread();
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mInitialized;
|
2004-05-11 13:38:50 +04:00
|
|
|
|
2017-04-21 00:56:06 +03:00
|
|
|
// These internal helper methods must be called while mMonitor is held.
|
2017-04-21 00:56:07 +03:00
|
|
|
// AddTimerInternal returns false if the insertion failed.
|
|
|
|
bool AddTimerInternal(nsTimerImpl* aTimer);
|
2014-05-27 11:15:35 +04:00
|
|
|
bool RemoveTimerInternal(nsTimerImpl* aTimer);
|
2017-04-21 00:56:07 +03:00
|
|
|
void RemoveLeadingCanceledTimersInternal();
|
2017-04-21 00:56:07 +03:00
|
|
|
void RemoveFirstTimerInternal();
|
2017-05-01 21:42:11 +03:00
|
|
|
nsresult Init();
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2015-07-22 20:39:34 +03:00
|
|
|
already_AddRefed<nsTimerImpl> PostTimerEvent(already_AddRefed<nsTimerImpl> aTimerRef);
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
nsCOMPtr<nsIThread> mThread;
|
2011-04-29 23:21:57 +04:00
|
|
|
Monitor mMonitor;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mShutdown;
|
|
|
|
bool mWaiting;
|
2014-03-03 09:12:51 +04:00
|
|
|
bool mNotified;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mSleeping;
|
2014-02-07 10:05:24 +04:00
|
|
|
|
2017-04-21 00:56:07 +03:00
|
|
|
class Entry final : public nsTimerImplHolder
|
2017-04-21 00:56:07 +03:00
|
|
|
{
|
2017-04-21 00:56:07 +03:00
|
|
|
const TimeStamp mTimeout;
|
2017-04-21 00:56:07 +03:00
|
|
|
|
2017-04-21 00:56:07 +03:00
|
|
|
public:
|
2017-04-21 00:56:07 +03:00
|
|
|
Entry(const TimeStamp& aMinTimeout, const TimeStamp& aTimeout,
|
|
|
|
nsTimerImpl* aTimerImpl)
|
2017-04-21 00:56:07 +03:00
|
|
|
: nsTimerImplHolder(aTimerImpl)
|
|
|
|
, mTimeout(std::max(aMinTimeout, aTimeout))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTimerImpl*
|
|
|
|
Value() const
|
|
|
|
{
|
|
|
|
return mTimerImpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsTimerImpl>
|
|
|
|
Take()
|
|
|
|
{
|
|
|
|
if (mTimerImpl) {
|
|
|
|
mTimerImpl->SetHolder(nullptr);
|
|
|
|
}
|
|
|
|
return mTimerImpl.forget();
|
|
|
|
}
|
2017-04-21 00:56:07 +03:00
|
|
|
|
2017-04-21 00:56:07 +03:00
|
|
|
static bool
|
|
|
|
UniquePtrLessThan(UniquePtr<Entry>& aLeft, UniquePtr<Entry>& aRight)
|
2017-04-21 00:56:07 +03:00
|
|
|
{
|
2017-04-21 00:56:07 +03:00
|
|
|
// This is reversed because std::push_heap() sorts the "largest" to
|
|
|
|
// the front of the heap. We want that to be the earliest timer.
|
|
|
|
return aRight->mTimeout < aLeft->mTimeout;
|
2017-04-21 00:56:07 +03:00
|
|
|
}
|
2017-06-05 16:49:54 +03:00
|
|
|
|
|
|
|
TimeStamp Timeout() const
|
|
|
|
{
|
|
|
|
return mTimeout;
|
|
|
|
}
|
2017-04-21 00:56:07 +03:00
|
|
|
};
|
|
|
|
|
2017-04-21 00:56:07 +03:00
|
|
|
nsTArray<UniquePtr<Entry>> mTimers;
|
2017-06-01 03:13:20 +03:00
|
|
|
uint32_t mAllowedEarlyFiringMicroseconds;
|
2001-12-16 09:13:17 +03:00
|
|
|
};
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
struct TimerAdditionComparator
|
|
|
|
{
|
|
|
|
TimerAdditionComparator(const mozilla::TimeStamp& aNow,
|
|
|
|
nsTimerImpl* aTimerToInsert) :
|
2013-02-20 22:21:09 +04:00
|
|
|
now(aNow)
|
2013-02-13 19:11:53 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
, timerToInsert(aTimerToInsert)
|
|
|
|
#endif
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
|
|
|
}
|
2013-02-13 19:11:53 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
bool LessThan(nsTimerImpl* aFromArray, nsTimerImpl* aNewTimer) const
|
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aNewTimer == timerToInsert, "Unexpected timer ordering");
|
2013-02-13 19:11:53 +04:00
|
|
|
|
|
|
|
// Skip any overdue timers.
|
2014-05-27 11:15:35 +04:00
|
|
|
return aFromArray->mTimeout <= now ||
|
|
|
|
aFromArray->mTimeout <= aNewTimer->mTimeout;
|
2013-02-13 19:11:53 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
bool Equals(nsTimerImpl* aFromArray, nsTimerImpl* aNewTimer) const
|
|
|
|
{
|
2013-04-04 21:28:18 +04:00
|
|
|
return false;
|
2013-02-13 19:11:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-05-27 11:15:35 +04:00
|
|
|
const mozilla::TimeStamp& now;
|
2013-02-13 19:11:53 +04:00
|
|
|
#ifdef DEBUG
|
2014-05-27 11:15:35 +04:00
|
|
|
const nsTimerImpl* const timerToInsert;
|
2013-02-13 19:11:53 +04:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
#endif /* TimerThread_h___ */
|