ipc/glue/CrossProcessMutex_posix.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "CrossProcessMutex.h"
michael@0 7 #include "mozilla/unused.h"
michael@0 8 #include "nsDebug.h"
michael@0 9 #include "nsISupportsImpl.h"
michael@0 10
michael@0 11 namespace {
michael@0 12
michael@0 13 struct MutexData {
michael@0 14 pthread_mutex_t mMutex;
michael@0 15 mozilla::Atomic<int32_t> mCount;
michael@0 16 };
michael@0 17
michael@0 18 }
michael@0 19
michael@0 20 namespace mozilla {
michael@0 21
michael@0 22 CrossProcessMutex::CrossProcessMutex(const char*)
michael@0 23 : mSharedBuffer(nullptr)
michael@0 24 , mMutex(nullptr)
michael@0 25 , mCount(nullptr)
michael@0 26 {
michael@0 27 mSharedBuffer = new ipc::SharedMemoryBasic;
michael@0 28 if (!mSharedBuffer->Create(sizeof(MutexData))) {
michael@0 29 MOZ_CRASH();
michael@0 30 }
michael@0 31
michael@0 32 if (!mSharedBuffer->Map(sizeof(MutexData))) {
michael@0 33 MOZ_CRASH();
michael@0 34 }
michael@0 35
michael@0 36 MutexData* data = static_cast<MutexData*>(mSharedBuffer->memory());
michael@0 37
michael@0 38 if (!data) {
michael@0 39 MOZ_CRASH();
michael@0 40 }
michael@0 41
michael@0 42 mMutex = &(data->mMutex);
michael@0 43 mCount = &(data->mCount);
michael@0 44
michael@0 45 *mCount = 1;
michael@0 46
michael@0 47 pthread_mutexattr_t mutexAttributes;
michael@0 48 pthread_mutexattr_init(&mutexAttributes);
michael@0 49 // Make the mutex reentrant so it behaves the same as a win32 mutex
michael@0 50 if (pthread_mutexattr_settype(&mutexAttributes, PTHREAD_MUTEX_RECURSIVE)) {
michael@0 51 MOZ_CRASH();
michael@0 52 }
michael@0 53 if (pthread_mutexattr_setpshared(&mutexAttributes, PTHREAD_PROCESS_SHARED)) {
michael@0 54 MOZ_CRASH();
michael@0 55 }
michael@0 56
michael@0 57 if (pthread_mutex_init(mMutex, &mutexAttributes)) {
michael@0 58 MOZ_CRASH();
michael@0 59 }
michael@0 60
michael@0 61 MOZ_COUNT_CTOR(CrossProcessMutex);
michael@0 62 }
michael@0 63
michael@0 64 CrossProcessMutex::CrossProcessMutex(CrossProcessMutexHandle aHandle)
michael@0 65 : mSharedBuffer(nullptr)
michael@0 66 , mMutex(nullptr)
michael@0 67 , mCount(nullptr)
michael@0 68 {
michael@0 69 if (!ipc::SharedMemoryBasic::IsHandleValid(aHandle)) {
michael@0 70 MOZ_CRASH();
michael@0 71 }
michael@0 72
michael@0 73 mSharedBuffer = new ipc::SharedMemoryBasic(aHandle);
michael@0 74
michael@0 75 if (!mSharedBuffer->Map(sizeof(MutexData))) {
michael@0 76 MOZ_CRASH();
michael@0 77 }
michael@0 78
michael@0 79 MutexData* data = static_cast<MutexData*>(mSharedBuffer->memory());
michael@0 80
michael@0 81 if (!data) {
michael@0 82 MOZ_CRASH();
michael@0 83 }
michael@0 84
michael@0 85 mMutex = &(data->mMutex);
michael@0 86 mCount = &(data->mCount);
michael@0 87 (*mCount)++;
michael@0 88
michael@0 89 MOZ_COUNT_CTOR(CrossProcessMutex);
michael@0 90 }
michael@0 91
michael@0 92 CrossProcessMutex::~CrossProcessMutex()
michael@0 93 {
michael@0 94 int32_t count = --(*mCount);
michael@0 95
michael@0 96 if (count == 0) {
michael@0 97 // Nothing can be done if the destroy fails so ignore return code.
michael@0 98 unused << pthread_mutex_destroy(mMutex);
michael@0 99 }
michael@0 100
michael@0 101 delete mSharedBuffer;
michael@0 102 MOZ_COUNT_DTOR(CrossProcessMutex);
michael@0 103 }
michael@0 104
michael@0 105 void
michael@0 106 CrossProcessMutex::Lock()
michael@0 107 {
michael@0 108 MOZ_ASSERT(*mCount > 0, "Attempting to lock mutex with zero ref count");
michael@0 109 pthread_mutex_lock(mMutex);
michael@0 110 }
michael@0 111
michael@0 112 void
michael@0 113 CrossProcessMutex::Unlock()
michael@0 114 {
michael@0 115 MOZ_ASSERT(*mCount > 0, "Attempting to unlock mutex with zero ref count");
michael@0 116 pthread_mutex_unlock(mMutex);
michael@0 117 }
michael@0 118
michael@0 119 CrossProcessMutexHandle
michael@0 120 CrossProcessMutex::ShareToProcess(base::ProcessHandle aHandle)
michael@0 121 {
michael@0 122 CrossProcessMutexHandle result = ipc::SharedMemoryBasic::NULLHandle();
michael@0 123
michael@0 124 if (mSharedBuffer && !mSharedBuffer->ShareToProcess(aHandle, &result)) {
michael@0 125 MOZ_CRASH();
michael@0 126 }
michael@0 127
michael@0 128 return result;
michael@0 129 }
michael@0 130
michael@0 131 }

mercurial