зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1423000 - Move mozjemalloc mutexes to a separate header. r=njn
Also change the definition of the StaticMutex constructor to unconfuse clang-format. --HG-- extra : rebase_source : aa2aa07c8c324e1253c20b34d850230579d45632
This commit is contained in:
Родитель
938618a5e1
Коммит
ab9b4f676d
|
@ -0,0 +1,141 @@
|
|||
/* -*- 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
|
||||
* 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/. */
|
||||
|
||||
#ifndef Mutex_h
|
||||
#define Mutex_h
|
||||
|
||||
#if defined(XP_WIN)
|
||||
#include <windows.h>
|
||||
#elif defined(XP_DARWIN)
|
||||
#include <libkern/OSAtomic.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include "mozilla/GuardObjects.h"
|
||||
|
||||
// Mutexes based on spinlocks. We can't use normal pthread spinlocks in all
|
||||
// places, because they require malloc()ed memory, which causes bootstrapping
|
||||
// issues in some cases. We also can't use constructors, because for statics,
|
||||
// they would fire after the first use of malloc, resetting the locks.
|
||||
struct Mutex
|
||||
{
|
||||
#if defined(XP_WIN)
|
||||
CRITICAL_SECTION mMutex;
|
||||
#elif defined(XP_DARWIN)
|
||||
OSSpinLock mMutex;
|
||||
#else
|
||||
pthread_mutex_t mMutex;
|
||||
#endif
|
||||
|
||||
// Initializes a mutex. Returns whether initialization succeeded.
|
||||
inline bool Init()
|
||||
{
|
||||
#if defined(XP_WIN)
|
||||
if (!InitializeCriticalSectionAndSpinCount(&mMutex, 5000)) {
|
||||
return false;
|
||||
}
|
||||
#elif defined(XP_DARWIN)
|
||||
mMutex = OS_SPINLOCK_INIT;
|
||||
#elif defined(XP_LINUX) && !defined(ANDROID)
|
||||
pthread_mutexattr_t attr;
|
||||
if (pthread_mutexattr_init(&attr) != 0) {
|
||||
return false;
|
||||
}
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
|
||||
if (pthread_mutex_init(&mMutex, &attr) != 0) {
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
return false;
|
||||
}
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
#else
|
||||
if (pthread_mutex_init(&mMutex, nullptr) != 0) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void Lock()
|
||||
{
|
||||
#if defined(XP_WIN)
|
||||
EnterCriticalSection(&mMutex);
|
||||
#elif defined(XP_DARWIN)
|
||||
OSSpinLockLock(&mMutex);
|
||||
#else
|
||||
pthread_mutex_lock(&mMutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline void Unlock()
|
||||
{
|
||||
#if defined(XP_WIN)
|
||||
LeaveCriticalSection(&mMutex);
|
||||
#elif defined(XP_DARWIN)
|
||||
OSSpinLockUnlock(&mMutex);
|
||||
#else
|
||||
pthread_mutex_unlock(&mMutex);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
// Mutex that can be used for static initialization.
|
||||
// On Windows, CRITICAL_SECTION requires a function call to be initialized,
|
||||
// but for the initialization lock, a static initializer calling the
|
||||
// function would be called too late. We need no-function-call
|
||||
// initialization, which SRWLock provides.
|
||||
// Ideally, we'd use the same type of locks everywhere, but SRWLocks
|
||||
// everywhere incur a performance penalty. See bug 1418389.
|
||||
#if defined(XP_WIN)
|
||||
struct StaticMutex
|
||||
{
|
||||
SRWLOCK mMutex;
|
||||
|
||||
constexpr StaticMutex()
|
||||
: mMutex(SRWLOCK_INIT)
|
||||
{
|
||||
}
|
||||
|
||||
inline void Lock() { AcquireSRWLockExclusive(&mMutex); }
|
||||
|
||||
inline void Unlock() { ReleaseSRWLockExclusive(&mMutex); }
|
||||
};
|
||||
#else
|
||||
struct StaticMutex : public Mutex
|
||||
{
|
||||
#if defined(XP_DARWIN)
|
||||
#define STATIC_MUTEX_INIT OS_SPINLOCK_INIT
|
||||
#elif defined(XP_LINUX) && !defined(ANDROID)
|
||||
#define STATIC_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
||||
#else
|
||||
#define STATIC_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
|
||||
#endif
|
||||
constexpr StaticMutex()
|
||||
: Mutex{ STATIC_MUTEX_INIT }
|
||||
{
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
struct MOZ_RAII AutoLock
|
||||
{
|
||||
explicit AutoLock(T& aMutex MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
||||
: mMutex(aMutex)
|
||||
{
|
||||
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
||||
mMutex.Lock();
|
||||
}
|
||||
|
||||
~AutoLock() { mMutex.Unlock(); }
|
||||
|
||||
private:
|
||||
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER;
|
||||
T& mMutex;
|
||||
};
|
||||
|
||||
using MutexAutoLock = AutoLock<Mutex>;
|
||||
|
||||
#endif
|
|
@ -129,7 +129,6 @@
|
|||
#include "mozilla/Attributes.h"
|
||||
#include "mozilla/CheckedInt.h"
|
||||
#include "mozilla/DoublyLinkedList.h"
|
||||
#include "mozilla/GuardObjects.h"
|
||||
#include "mozilla/Likely.h"
|
||||
#include "mozilla/MathAlgorithms.h"
|
||||
#include "mozilla/Sprintf.h"
|
||||
|
@ -141,6 +140,7 @@
|
|||
#include "mozilla/Unused.h"
|
||||
#include "mozilla/fallible.h"
|
||||
#include "rb.h"
|
||||
#include "Mutex.h"
|
||||
#include "Utils.h"
|
||||
|
||||
using namespace mozilla;
|
||||
|
@ -536,82 +536,6 @@ static size_t opt_dirty_max = DIRTY_MAX_DEFAULT;
|
|||
static void*
|
||||
base_alloc(size_t aSize);
|
||||
|
||||
// Mutexes based on spinlocks. We can't use normal pthread spinlocks in all
|
||||
// places, because they require malloc()ed memory, which causes bootstrapping
|
||||
// issues in some cases.
|
||||
struct Mutex
|
||||
{
|
||||
#if defined(XP_WIN)
|
||||
CRITICAL_SECTION mMutex;
|
||||
#elif defined(XP_DARWIN)
|
||||
OSSpinLock mMutex;
|
||||
#else
|
||||
pthread_mutex_t mMutex;
|
||||
#endif
|
||||
|
||||
inline bool Init();
|
||||
|
||||
inline void Lock();
|
||||
|
||||
inline void Unlock();
|
||||
};
|
||||
|
||||
// Mutex that can be used for static initialization.
|
||||
// On Windows, CRITICAL_SECTION requires a function call to be initialized,
|
||||
// but for the initialization lock, a static initializer calling the
|
||||
// function would be called too late. We need no-function-call
|
||||
// initialization, which SRWLock provides.
|
||||
// Ideally, we'd use the same type of locks everywhere, but SRWLocks
|
||||
// everywhere incur a performance penalty. See bug 1418389.
|
||||
#if defined(XP_WIN)
|
||||
struct StaticMutex
|
||||
{
|
||||
SRWLOCK mMutex;
|
||||
|
||||
constexpr StaticMutex()
|
||||
: mMutex(SRWLOCK_INIT)
|
||||
{
|
||||
}
|
||||
|
||||
inline void Lock();
|
||||
|
||||
inline void Unlock();
|
||||
};
|
||||
#else
|
||||
struct StaticMutex : public Mutex
|
||||
{
|
||||
constexpr StaticMutex()
|
||||
#if defined(XP_DARWIN)
|
||||
: Mutex{ OS_SPINLOCK_INIT }
|
||||
#elif defined(XP_LINUX) && !defined(ANDROID)
|
||||
: Mutex{ PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP }
|
||||
#else
|
||||
: Mutex{ PTHREAD_MUTEX_INITIALIZER }
|
||||
#endif
|
||||
{
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
struct MOZ_RAII AutoLock
|
||||
{
|
||||
explicit AutoLock(T& aMutex MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
||||
: mMutex(aMutex)
|
||||
{
|
||||
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
||||
mMutex.Lock();
|
||||
}
|
||||
|
||||
~AutoLock() { mMutex.Unlock(); }
|
||||
|
||||
private:
|
||||
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER;
|
||||
T& mMutex;
|
||||
};
|
||||
|
||||
using MutexAutoLock = AutoLock<Mutex>;
|
||||
|
||||
// Set to true once the allocator has been initialized.
|
||||
static Atomic<bool> malloc_initialized(false);
|
||||
|
||||
|
@ -1362,80 +1286,6 @@ extern "C" MOZ_EXPORT int
|
|||
pthread_atfork(void (*)(void), void (*)(void), void (*)(void));
|
||||
#endif
|
||||
|
||||
// ***************************************************************************
|
||||
// Begin mutex. We can't use normal pthread mutexes in all places, because
|
||||
// they require malloc()ed memory, which causes bootstrapping issues in some
|
||||
// cases. We also can't use constructors, because for statics, they would fire
|
||||
// after the first use of malloc, resetting the locks.
|
||||
|
||||
// Initializes a mutex. Returns whether initialization succeeded.
|
||||
bool
|
||||
Mutex::Init()
|
||||
{
|
||||
#if defined(XP_WIN)
|
||||
if (!InitializeCriticalSectionAndSpinCount(&mMutex, 5000)) {
|
||||
return false;
|
||||
}
|
||||
#elif defined(XP_DARWIN)
|
||||
mMutex = OS_SPINLOCK_INIT;
|
||||
#elif defined(XP_LINUX) && !defined(ANDROID)
|
||||
pthread_mutexattr_t attr;
|
||||
if (pthread_mutexattr_init(&attr) != 0) {
|
||||
return false;
|
||||
}
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
|
||||
if (pthread_mutex_init(&mMutex, &attr) != 0) {
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
return false;
|
||||
}
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
#else
|
||||
if (pthread_mutex_init(&mMutex, nullptr) != 0) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
Mutex::Lock()
|
||||
{
|
||||
#if defined(XP_WIN)
|
||||
EnterCriticalSection(&mMutex);
|
||||
#elif defined(XP_DARWIN)
|
||||
OSSpinLockLock(&mMutex);
|
||||
#else
|
||||
pthread_mutex_lock(&mMutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
Mutex::Unlock()
|
||||
{
|
||||
#if defined(XP_WIN)
|
||||
LeaveCriticalSection(&mMutex);
|
||||
#elif defined(XP_DARWIN)
|
||||
OSSpinLockUnlock(&mMutex);
|
||||
#else
|
||||
pthread_mutex_unlock(&mMutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(XP_WIN)
|
||||
void
|
||||
StaticMutex::Lock()
|
||||
{
|
||||
AcquireSRWLockExclusive(&mMutex);
|
||||
}
|
||||
|
||||
void
|
||||
StaticMutex::Unlock()
|
||||
{
|
||||
ReleaseSRWLockExclusive(&mMutex);
|
||||
}
|
||||
#endif
|
||||
|
||||
// End mutex.
|
||||
// ***************************************************************************
|
||||
// Begin Utility functions/macros.
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче