Bug 1858509 add thread-safety annotations around MediaSourceDemuxer::mMonitor r=alwu
[gecko.git] / ipc / glue / CrossProcessSemaphore_posix.cpp
blob3b32a897ee7d1075491611a67218d6e85d6e30ca
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "CrossProcessSemaphore.h"
8 #include "mozilla/Unused.h"
9 #include "nsDebug.h"
10 #include "nsISupportsImpl.h"
11 #include <errno.h>
13 static const uint64_t kNsPerMs = 1000000;
14 static const uint64_t kNsPerSec = 1000000000;
16 namespace {
18 struct SemaphoreData {
19 sem_t mSemaphore;
20 mozilla::Atomic<int32_t> mRefCount;
21 uint32_t mInitialValue;
24 } // namespace
26 namespace mozilla {
28 /* static */
29 CrossProcessSemaphore* CrossProcessSemaphore::Create(const char*,
30 uint32_t aInitialValue) {
31 RefPtr<ipc::SharedMemoryBasic> sharedBuffer = new ipc::SharedMemoryBasic;
32 if (!sharedBuffer->Create(sizeof(SemaphoreData))) {
33 return nullptr;
36 if (!sharedBuffer->Map(sizeof(SemaphoreData))) {
37 return nullptr;
40 SemaphoreData* data = static_cast<SemaphoreData*>(sharedBuffer->memory());
42 if (!data) {
43 return nullptr;
46 if (sem_init(&data->mSemaphore, 1, aInitialValue)) {
47 return nullptr;
50 CrossProcessSemaphore* sem = new CrossProcessSemaphore;
51 sem->mSharedBuffer = sharedBuffer;
52 sem->mSemaphore = &data->mSemaphore;
53 sem->mRefCount = &data->mRefCount;
54 *sem->mRefCount = 1;
56 data->mInitialValue = aInitialValue;
58 return sem;
61 /* static */
62 CrossProcessSemaphore* CrossProcessSemaphore::Create(
63 CrossProcessSemaphoreHandle aHandle) {
64 RefPtr<ipc::SharedMemoryBasic> sharedBuffer = new ipc::SharedMemoryBasic;
66 if (!sharedBuffer->IsHandleValid(aHandle)) {
67 return nullptr;
70 if (!sharedBuffer->SetHandle(std::move(aHandle),
71 ipc::SharedMemory::RightsReadWrite)) {
72 return nullptr;
75 if (!sharedBuffer->Map(sizeof(SemaphoreData))) {
76 return nullptr;
79 sharedBuffer->CloseHandle();
81 SemaphoreData* data = static_cast<SemaphoreData*>(sharedBuffer->memory());
83 if (!data) {
84 return nullptr;
87 int32_t oldCount = data->mRefCount++;
88 if (oldCount == 0) {
89 // The other side has already let go of their CrossProcessSemaphore, so now
90 // mSemaphore is garbage. We need to re-initialize it.
91 if (sem_init(&data->mSemaphore, 1, data->mInitialValue)) {
92 data->mRefCount--;
93 return nullptr;
97 CrossProcessSemaphore* sem = new CrossProcessSemaphore;
98 sem->mSharedBuffer = sharedBuffer;
99 sem->mSemaphore = &data->mSemaphore;
100 sem->mRefCount = &data->mRefCount;
101 return sem;
104 CrossProcessSemaphore::CrossProcessSemaphore()
105 : mSemaphore(nullptr), mRefCount(nullptr) {
106 MOZ_COUNT_CTOR(CrossProcessSemaphore);
109 CrossProcessSemaphore::~CrossProcessSemaphore() {
110 int32_t oldCount = --(*mRefCount);
112 if (oldCount == 0) {
113 // Nothing can be done if the destroy fails so ignore return code.
114 Unused << sem_destroy(mSemaphore);
117 MOZ_COUNT_DTOR(CrossProcessSemaphore);
120 bool CrossProcessSemaphore::Wait(const Maybe<TimeDuration>& aWaitTime) {
121 MOZ_ASSERT(*mRefCount > 0,
122 "Attempting to wait on a semaphore with zero ref count");
123 int ret;
124 if (aWaitTime.isSome()) {
125 struct timespec ts;
126 if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
127 return false;
130 uint64_t ns = uint64_t(kNsPerMs * aWaitTime->ToMilliseconds()) + ts.tv_nsec;
131 ts.tv_sec += ns / kNsPerSec;
132 ts.tv_nsec = ns % kNsPerSec;
134 while ((ret = sem_timedwait(mSemaphore, &ts)) == -1 && errno == EINTR) {
136 } else {
137 while ((ret = sem_wait(mSemaphore)) == -1 && errno == EINTR) {
140 return ret == 0;
143 void CrossProcessSemaphore::Signal() {
144 MOZ_ASSERT(*mRefCount > 0,
145 "Attempting to signal a semaphore with zero ref count");
146 sem_post(mSemaphore);
149 CrossProcessSemaphoreHandle CrossProcessSemaphore::CloneHandle() {
150 CrossProcessSemaphoreHandle result = ipc::SharedMemoryBasic::NULLHandle();
152 if (mSharedBuffer) {
153 result = mSharedBuffer->CloneHandle();
154 if (!result) {
155 MOZ_CRASH();
159 return result;
162 void CrossProcessSemaphore::CloseHandle() { mSharedBuffer->CloseHandle(); }
164 } // namespace mozilla