2014-06-30 08:39:45 -07: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 04:12:37 -07: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/. */
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
#ifndef mozilla_CondVar_h
|
|
|
|
#define mozilla_CondVar_h
|
|
|
|
|
|
|
|
#include "prcvar.h"
|
|
|
|
|
|
|
|
#include "mozilla/BlockingResourceBase.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
|
2014-03-28 13:08:22 -07:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
#endif //MOZILLA_INTERNAL_API
|
|
|
|
|
2009-04-18 18:54:23 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CondVar
|
2014-06-26 18:35:39 -07:00
|
|
|
* Vanilla condition variable. Please don't use this unless you have a
|
2011-04-29 12:21:57 -07:00
|
|
|
* compelling reason --- Monitor provides a simpler API.
|
|
|
|
*/
|
2014-08-27 15:47:27 -07:00
|
|
|
class CondVar : BlockingResourceBase
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
|
|
|
* CondVar
|
|
|
|
*
|
|
|
|
* The CALLER owns |aLock|.
|
|
|
|
*
|
|
|
|
* @param aLock A Mutex to associate with this condition variable.
|
|
|
|
* @param aName A name which can reference this monitor
|
|
|
|
* @returns If failure, nullptr.
|
|
|
|
* If success, a valid Monitor* which must be destroyed
|
|
|
|
* by Monitor::DestroyMonitor()
|
|
|
|
**/
|
|
|
|
CondVar(Mutex& aLock, const char* aName)
|
|
|
|
: BlockingResourceBase(aName, eCondVar)
|
|
|
|
, mLock(&aLock)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(CondVar);
|
|
|
|
// |aLock| must necessarily already be known to the deadlock detector
|
|
|
|
mCvar = PR_NewCondVar(mLock->mLock);
|
|
|
|
if (!mCvar) {
|
|
|
|
NS_RUNTIMEABORT("Can't allocate mozilla::CondVar");
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ~CondVar
|
|
|
|
* Clean up after this CondVar, but NOT its associated Mutex.
|
|
|
|
**/
|
|
|
|
~CondVar()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mCvar && mLock,
|
|
|
|
"improperly constructed CondVar or double free");
|
|
|
|
PR_DestroyCondVar(mCvar);
|
|
|
|
mCvar = 0;
|
|
|
|
mLock = 0;
|
|
|
|
MOZ_COUNT_DTOR(CondVar);
|
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
#ifndef DEBUG
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
|
|
|
* Wait
|
|
|
|
* @see prcvar.h
|
|
|
|
**/
|
|
|
|
nsresult Wait(PRIntervalTime aInterval = PR_INTERVAL_NO_TIMEOUT)
|
|
|
|
{
|
2014-03-28 13:08:22 -07:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2014-06-26 18:35:39 -07:00
|
|
|
GeckoProfilerSleepRAII profiler_sleep;
|
2014-03-28 13:08:22 -07:00
|
|
|
#endif //MOZILLA_INTERNAL_API
|
2014-06-26 18:35:39 -07:00
|
|
|
// NSPR checks for lock ownership
|
|
|
|
return PR_WaitCondVar(mCvar, aInterval) == PR_SUCCESS ? NS_OK :
|
|
|
|
NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-05-04 21:57:15 -07:00
|
|
|
#else
|
2014-06-26 18:35:39 -07:00
|
|
|
nsresult Wait(PRIntervalTime aInterval = PR_INTERVAL_NO_TIMEOUT);
|
2009-05-04 21:57:15 -07:00
|
|
|
#endif // ifndef DEBUG
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
|
|
|
* Notify
|
|
|
|
* @see prcvar.h
|
|
|
|
**/
|
|
|
|
nsresult Notify()
|
|
|
|
{
|
|
|
|
// NSPR checks for lock ownership
|
|
|
|
return PR_NotifyCondVar(mCvar) == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NotifyAll
|
|
|
|
* @see prcvar.h
|
|
|
|
**/
|
|
|
|
nsresult NotifyAll()
|
|
|
|
{
|
|
|
|
// NSPR checks for lock ownership
|
|
|
|
return PR_NotifyAllCondVar(mCvar) == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
|
|
|
* AssertCurrentThreadOwnsMutex
|
|
|
|
* @see Mutex::AssertCurrentThreadOwns
|
|
|
|
**/
|
|
|
|
void AssertCurrentThreadOwnsMutex()
|
|
|
|
{
|
|
|
|
mLock->AssertCurrentThreadOwns();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AssertNotCurrentThreadOwnsMutex
|
|
|
|
* @see Mutex::AssertNotCurrentThreadOwns
|
|
|
|
**/
|
|
|
|
void AssertNotCurrentThreadOwnsMutex()
|
|
|
|
{
|
|
|
|
mLock->AssertNotCurrentThreadOwns();
|
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
#else
|
2014-06-26 18:35:39 -07:00
|
|
|
void AssertCurrentThreadOwnsMutex() {}
|
|
|
|
void AssertNotCurrentThreadOwnsMutex() {}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
#endif // ifdef DEBUG
|
|
|
|
|
|
|
|
private:
|
2014-06-26 18:35:39 -07:00
|
|
|
CondVar();
|
|
|
|
CondVar(CondVar&);
|
|
|
|
CondVar& operator=(CondVar&);
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
Mutex* mLock;
|
|
|
|
PRCondVar* mCvar;
|
2009-04-18 18:54:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ifndef mozilla_CondVar_h
|