2016-05-28 00:54:31 +03: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
|
2013-12-20 00:19:25 +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/. */
|
|
|
|
|
|
|
|
#include "CrossProcessMutex.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2013-12-20 00:19:25 +04:00
|
|
|
#include "nsDebug.h"
|
2014-02-27 01:36:35 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
2013-12-20 00:19:25 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct MutexData {
|
|
|
|
pthread_mutex_t mMutex;
|
|
|
|
mozilla::Atomic<int32_t> mCount;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-12-20 00:19:25 +04:00
|
|
|
|
|
|
|
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), mCount(nullptr) {
|
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), mCount(nullptr) {
|
2016-02-18 18:56:15 +03:00
|
|
|
mSharedBuffer = new ipc::SharedMemoryBasic;
|
|
|
|
|
|
|
|
if (!mSharedBuffer->IsHandleValid(aHandle)) {
|
2013-12-20 00:19:25 +04:00
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
2017-04-18 19:24:58 +03:00
|
|
|
if (!mSharedBuffer->SetHandle(aHandle, ipc::SharedMemory::RightsReadWrite)) {
|
2016-02-18 18:56:15 +03:00
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2013-12-20 00:19:25 +04:00
|
|
|
|
|
|
|
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.
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << pthread_mutex_destroy(mMutex);
|
2013-12-20 00:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-01 11:40:35 +03:00
|
|
|
CrossProcessMutexHandle 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;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|