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_Mutex_h
|
|
|
|
#define mozilla_Mutex_h
|
|
|
|
|
|
|
|
#include "prlock.h"
|
|
|
|
|
|
|
|
#include "mozilla/BlockingResourceBase.h"
|
2011-11-15 23:50:19 -08:00
|
|
|
#include "mozilla/GuardObjects.h"
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// Provides:
|
|
|
|
//
|
|
|
|
// - Mutex, a non-recursive mutex
|
2014-06-26 18:35:39 -07:00
|
|
|
// - MutexAutoLock, an RAII class for ensuring that Mutexes are properly
|
2009-04-18 18:54:23 -07:00
|
|
|
// locked and unlocked
|
|
|
|
// - MutexAutoUnlock, complementary sibling to MutexAutoLock
|
|
|
|
//
|
2013-05-24 10:10:47 -07:00
|
|
|
// - OffTheBooksMutex, a non-recursive mutex that doesn't do leak checking
|
|
|
|
// - OffTheBooksMutexAuto{Lock,Unlock} - Like MutexAuto{Lock,Unlock}, but for
|
|
|
|
// an OffTheBooksMutex.
|
|
|
|
//
|
|
|
|
// Using MutexAutoLock/MutexAutoUnlock etc. is MUCH preferred to making bare
|
|
|
|
// calls to Lock and Unlock.
|
2009-04-18 18:54:23 -07:00
|
|
|
//
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
2013-05-24 10:10:47 -07:00
|
|
|
* OffTheBooksMutex is identical to Mutex, except that OffTheBooksMutex doesn't
|
|
|
|
* include leak checking. Sometimes you want to intentionally "leak" a mutex
|
|
|
|
* until shutdown; in these cases, OffTheBooksMutex is for you.
|
|
|
|
*/
|
|
|
|
class NS_COM_GLUE OffTheBooksMutex : BlockingResourceBase
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
|
|
|
* @param aName A name which can reference this lock
|
|
|
|
* @returns If failure, nullptr
|
|
|
|
* If success, a valid Mutex* which must be destroyed
|
|
|
|
* by Mutex::DestroyMutex()
|
|
|
|
**/
|
2014-07-28 10:19:06 -07:00
|
|
|
explicit OffTheBooksMutex(const char* aName)
|
2014-06-26 18:35:39 -07:00
|
|
|
: BlockingResourceBase(aName, eMutex)
|
|
|
|
{
|
|
|
|
mLock = PR_NewLock();
|
|
|
|
if (!mLock) {
|
|
|
|
NS_RUNTIMEABORT("Can't allocate mozilla::Mutex");
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
~OffTheBooksMutex()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mLock,
|
|
|
|
"improperly constructed Lock or double free");
|
|
|
|
// NSPR does consistency checks for us
|
|
|
|
PR_DestroyLock(mLock);
|
|
|
|
mLock = 0;
|
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
#ifndef DEBUG
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
|
|
|
* Lock
|
|
|
|
* @see prlock.h
|
|
|
|
**/
|
|
|
|
void Lock() { PR_Lock(mLock); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlock
|
|
|
|
* @see prlock.h
|
|
|
|
**/
|
|
|
|
void Unlock() { PR_Unlock(mLock); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AssertCurrentThreadOwns
|
|
|
|
* @see prlock.h
|
|
|
|
**/
|
|
|
|
void AssertCurrentThreadOwns() const {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AssertNotCurrentThreadOwns
|
|
|
|
* @see prlock.h
|
|
|
|
**/
|
|
|
|
void AssertNotCurrentThreadOwns() const {}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
#else
|
2014-06-26 18:35:39 -07:00
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
void AssertCurrentThreadOwns() const
|
|
|
|
{
|
|
|
|
PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mLock);
|
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
void AssertNotCurrentThreadOwns() const
|
|
|
|
{
|
|
|
|
// FIXME bug 476536
|
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
#endif // ifndef DEBUG
|
|
|
|
|
|
|
|
private:
|
2014-06-26 18:35:39 -07:00
|
|
|
OffTheBooksMutex();
|
|
|
|
OffTheBooksMutex(const OffTheBooksMutex&);
|
|
|
|
OffTheBooksMutex& operator=(const OffTheBooksMutex&);
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
PRLock* mLock;
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
friend class CondVar;
|
2009-04-18 18:54:23 -07:00
|
|
|
};
|
|
|
|
|
2013-05-24 10:10:47 -07:00
|
|
|
/**
|
|
|
|
* Mutex
|
|
|
|
* When possible, use MutexAutoLock/MutexAutoUnlock to lock/unlock this
|
|
|
|
* mutex within a scope, instead of calling Lock/Unlock directly.
|
|
|
|
*/
|
|
|
|
class NS_COM_GLUE Mutex : public OffTheBooksMutex
|
|
|
|
{
|
|
|
|
public:
|
2014-07-28 10:19:06 -07:00
|
|
|
explicit Mutex(const char* aName)
|
2014-06-26 18:35:39 -07:00
|
|
|
: OffTheBooksMutex(aName)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(Mutex);
|
|
|
|
}
|
2013-05-24 10:10:47 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
~Mutex()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(Mutex);
|
|
|
|
}
|
2013-05-24 10:10:47 -07:00
|
|
|
|
|
|
|
private:
|
2014-06-26 18:35:39 -07:00
|
|
|
Mutex();
|
|
|
|
Mutex(const Mutex&);
|
|
|
|
Mutex& operator=(const Mutex&);
|
2013-05-24 10:10:47 -07:00
|
|
|
};
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MutexAutoLock
|
2014-06-26 18:35:39 -07:00
|
|
|
* Acquires the Mutex when it enters scope, and releases it when it leaves
|
2009-04-18 18:54:23 -07:00
|
|
|
* scope.
|
|
|
|
*
|
|
|
|
* MUCH PREFERRED to bare calls to Mutex.Lock and Unlock.
|
2014-06-26 18:35:39 -07:00
|
|
|
*/
|
2012-02-15 19:11:34 -08:00
|
|
|
template<typename T>
|
2013-04-11 20:21:40 -07:00
|
|
|
class NS_COM_GLUE MOZ_STACK_CLASS BaseAutoLock
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* The constructor aquires the given lock. The destructor
|
|
|
|
* releases the lock.
|
|
|
|
*
|
|
|
|
* @param aLock A valid mozilla::Mutex* returned by
|
|
|
|
* mozilla::Mutex::NewMutex.
|
|
|
|
**/
|
2014-07-28 10:19:06 -07:00
|
|
|
explicit BaseAutoLock(T& aLock MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2014-06-26 18:35:39 -07:00
|
|
|
: mLock(&aLock)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
NS_ASSERTION(mLock, "null mutex");
|
|
|
|
mLock->Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~BaseAutoLock(void)
|
|
|
|
{
|
|
|
|
mLock->Unlock();
|
|
|
|
}
|
|
|
|
|
2009-04-18 18:54:23 -07:00
|
|
|
private:
|
2014-06-26 18:35:39 -07:00
|
|
|
BaseAutoLock();
|
|
|
|
BaseAutoLock(BaseAutoLock&);
|
|
|
|
BaseAutoLock& operator=(BaseAutoLock&);
|
|
|
|
static void* operator new(size_t) CPP_THROW_NEW;
|
|
|
|
static void operator delete(void*);
|
|
|
|
|
|
|
|
T* mLock;
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2009-04-18 18:54:23 -07:00
|
|
|
};
|
|
|
|
|
2012-02-15 19:11:34 -08:00
|
|
|
typedef BaseAutoLock<Mutex> MutexAutoLock;
|
2013-05-24 10:10:47 -07:00
|
|
|
typedef BaseAutoLock<OffTheBooksMutex> OffTheBooksMutexAutoLock;
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MutexAutoUnlock
|
2014-06-26 18:35:39 -07:00
|
|
|
* Releases the Mutex when it enters scope, and re-acquires it when it leaves
|
2009-04-18 18:54:23 -07:00
|
|
|
* scope.
|
|
|
|
*
|
|
|
|
* MUCH PREFERRED to bare calls to Mutex.Unlock and Lock.
|
2014-06-26 18:35:39 -07:00
|
|
|
*/
|
2012-02-15 19:11:34 -08:00
|
|
|
template<typename T>
|
2014-06-26 18:35:39 -07:00
|
|
|
class NS_COM_GLUE MOZ_STACK_CLASS BaseAutoUnlock
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-07-28 10:19:06 -07:00
|
|
|
explicit BaseAutoUnlock(T& aLock MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2014-06-26 18:35:39 -07:00
|
|
|
: mLock(&aLock)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
NS_ASSERTION(mLock, "null lock");
|
|
|
|
mLock->Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~BaseAutoUnlock()
|
|
|
|
{
|
|
|
|
mLock->Lock();
|
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
private:
|
2014-06-26 18:35:39 -07:00
|
|
|
BaseAutoUnlock();
|
|
|
|
BaseAutoUnlock(BaseAutoUnlock&);
|
|
|
|
BaseAutoUnlock& operator=(BaseAutoUnlock&);
|
|
|
|
static void* operator new(size_t) CPP_THROW_NEW;
|
|
|
|
static void operator delete(void*);
|
|
|
|
|
|
|
|
T* mLock;
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2009-04-18 18:54:23 -07:00
|
|
|
};
|
|
|
|
|
2012-02-15 19:11:34 -08:00
|
|
|
typedef BaseAutoUnlock<Mutex> MutexAutoUnlock;
|
2013-05-24 10:10:47 -07:00
|
|
|
typedef BaseAutoUnlock<OffTheBooksMutex> OffTheBooksMutexAutoUnlock;
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ifndef mozilla_Mutex_h
|