2013-12-20 00:19:25 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#include "CrossProcessMutex.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "nsDebug.h"
|
2014-02-27 01:36:35 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
2013-12-20 00:19:25 +04:00
|
|
|
|
2014-11-07 22:17:14 +03:00
|
|
|
#ifdef OS_MACOSX
|
|
|
|
#include "nsCocoaFeatures.h"
|
|
|
|
#endif
|
|
|
|
|
2013-12-20 00:19:25 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct MutexData {
|
|
|
|
pthread_mutex_t mMutex;
|
|
|
|
mozilla::Atomic<int32_t> mCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-06-21 01:06:36 +04:00
|
|
|
static void
|
|
|
|
InitMutex(pthread_mutex_t* mMutex)
|
|
|
|
{
|
|
|
|
pthread_mutexattr_t mutexAttributes;
|
|
|
|
pthread_mutexattr_init(&mutexAttributes);
|
|
|
|
// Make the mutex reentrant so it behaves the same as a win32 mutex
|
|
|
|
if (pthread_mutexattr_settype(&mutexAttributes, PTHREAD_MUTEX_RECURSIVE)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
if (pthread_mutexattr_setpshared(&mutexAttributes, PTHREAD_PROCESS_SHARED)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_mutex_init(mMutex, &mutexAttributes)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:19:25 +04:00
|
|
|
CrossProcessMutex::CrossProcessMutex(const char*)
|
2014-07-16 23:39:03 +04:00
|
|
|
: mMutex(nullptr)
|
2013-12-20 00:19:25 +04:00
|
|
|
, mCount(nullptr)
|
|
|
|
{
|
2014-11-07 22:17:14 +03:00
|
|
|
#ifdef OS_MACOSX
|
|
|
|
if (!nsCocoaFeatures::OnLionOrLater()) {
|
|
|
|
// Don't allow using the cross-process mutex before OS X 10.7 because it
|
|
|
|
// probably doesn't work very well. See discussion in bug 1072093 for more
|
|
|
|
// details.
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-20 00:19:25 +04:00
|
|
|
mSharedBuffer = new ipc::SharedMemoryBasic;
|
|
|
|
if (!mSharedBuffer->Create(sizeof(MutexData))) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSharedBuffer->Map(sizeof(MutexData))) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexData* data = static_cast<MutexData*>(mSharedBuffer->memory());
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
mMutex = &(data->mMutex);
|
|
|
|
mCount = &(data->mCount);
|
|
|
|
|
|
|
|
*mCount = 1;
|
2014-06-21 01:06:36 +04:00
|
|
|
InitMutex(mMutex);
|
2013-12-20 00:19:25 +04:00
|
|
|
|
|
|
|
MOZ_COUNT_CTOR(CrossProcessMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
CrossProcessMutex::CrossProcessMutex(CrossProcessMutexHandle aHandle)
|
2014-07-16 23:39:03 +04:00
|
|
|
: mMutex(nullptr)
|
2013-12-20 00:19:25 +04:00
|
|
|
, mCount(nullptr)
|
|
|
|
{
|
|
|
|
if (!ipc::SharedMemoryBasic::IsHandleValid(aHandle)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
mSharedBuffer = new ipc::SharedMemoryBasic(aHandle);
|
|
|
|
|
|
|
|
if (!mSharedBuffer->Map(sizeof(MutexData))) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexData* data = static_cast<MutexData*>(mSharedBuffer->memory());
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
mMutex = &(data->mMutex);
|
|
|
|
mCount = &(data->mCount);
|
2014-06-21 01:06:36 +04:00
|
|
|
int32_t count = (*mCount)++;
|
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
// The other side has already let go of their CrossProcessMutex, so now
|
|
|
|
// mMutex is garbage. We need to re-initialize it.
|
|
|
|
InitMutex(mMutex);
|
|
|
|
}
|
2013-12-20 00:19:25 +04:00
|
|
|
|
|
|
|
MOZ_COUNT_CTOR(CrossProcessMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
CrossProcessMutex::~CrossProcessMutex()
|
|
|
|
{
|
|
|
|
int32_t count = --(*mCount);
|
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
// Nothing can be done if the destroy fails so ignore return code.
|
|
|
|
unused << pthread_mutex_destroy(mMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(CrossProcessMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CrossProcessMutex::Lock()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(*mCount > 0, "Attempting to lock mutex with zero ref count");
|
|
|
|
pthread_mutex_lock(mMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CrossProcessMutex::Unlock()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(*mCount > 0, "Attempting to unlock mutex with zero ref count");
|
|
|
|
pthread_mutex_unlock(mMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
CrossProcessMutexHandle
|
2015-04-01 11:40:35 +03:00
|
|
|
CrossProcessMutex::ShareToProcess(base::ProcessId aTargetPid)
|
2013-12-20 00:19:25 +04:00
|
|
|
{
|
|
|
|
CrossProcessMutexHandle result = ipc::SharedMemoryBasic::NULLHandle();
|
|
|
|
|
2015-04-01 11:40:35 +03:00
|
|
|
if (mSharedBuffer && !mSharedBuffer->ShareToProcess(aTargetPid, &result)) {
|
2013-12-20 00:19:25 +04:00
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|