Bug 1769628 [wpt PR 34081] - Update wpt metadata, a=testonly
[gecko.git] / mozglue / misc / Mutex_posix.cpp
blob7378a544f2c73498bc05420358c4f397a87af42d
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 "mozilla/Assertions.h"
9 #include <errno.h>
10 #include <pthread.h>
11 #include <stdio.h>
13 #if defined(XP_DARWIN)
14 # include <pthread_spis.h>
15 #endif
17 #include "mozilla/PlatformMutex.h"
18 #include "MutexPlatformData_posix.h"
20 #define REPORT_PTHREADS_ERROR(result, msg) \
21 { \
22 errno = result; \
23 perror(msg); \
24 MOZ_CRASH(msg); \
27 #define TRY_CALL_PTHREADS(call, msg) \
28 { \
29 int result = (call); \
30 if (result != 0) { \
31 REPORT_PTHREADS_ERROR(result, msg); \
32 } \
35 mozilla::detail::MutexImpl::MutexImpl() {
36 pthread_mutexattr_t* attrp = nullptr;
38 #if defined(DEBUG)
39 # define MUTEX_KIND PTHREAD_MUTEX_ERRORCHECK
40 // Linux with glibc, FreeBSD and macOS 10.14+ support adaptive mutexes that
41 // spin for a short number of tries before sleeping. NSPR's locks did this,
42 // too, and it seems like a reasonable thing to do.
43 #elif (defined(__linux__) && defined(__GLIBC__)) || defined(__FreeBSD__)
44 # define MUTEX_KIND PTHREAD_MUTEX_ADAPTIVE_NP
45 #elif defined(XP_DARWIN)
46 # if defined(PTHREAD_MUTEX_POLICY_FIRSTFIT_NP)
47 # define POLICY_KIND PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
48 # else
49 # define POLICY_KIND (3) // The definition is missing in old SDKs
50 # endif
51 #endif
53 #if defined(MUTEX_KIND) || defined(POLICY_KIND)
54 # define ATTR_REQUIRED
55 #endif
57 #if defined(ATTR_REQUIRED)
58 pthread_mutexattr_t attr;
60 TRY_CALL_PTHREADS(
61 pthread_mutexattr_init(&attr),
62 "mozilla::detail::MutexImpl::MutexImpl: pthread_mutexattr_init failed");
64 # if defined(MUTEX_KIND)
65 TRY_CALL_PTHREADS(pthread_mutexattr_settype(&attr, MUTEX_KIND),
66 "mozilla::detail::MutexImpl::MutexImpl: "
67 "pthread_mutexattr_settype failed");
68 # elif defined(POLICY_KIND)
69 if (__builtin_available(macOS 10.14, *)) {
70 TRY_CALL_PTHREADS(pthread_mutexattr_setpolicy_np(&attr, POLICY_KIND),
71 "mozilla::detail::MutexImpl::MutexImpl: "
72 "pthread_mutexattr_setpolicy_np failed");
74 # endif
75 attrp = &attr;
76 #endif
78 TRY_CALL_PTHREADS(
79 pthread_mutex_init(&platformData()->ptMutex, attrp),
80 "mozilla::detail::MutexImpl::MutexImpl: pthread_mutex_init failed");
82 #if defined(ATTR_REQUIRED)
83 TRY_CALL_PTHREADS(pthread_mutexattr_destroy(&attr),
84 "mozilla::detail::MutexImpl::MutexImpl: "
85 "pthread_mutexattr_destroy failed");
86 #endif
89 mozilla::detail::MutexImpl::~MutexImpl() {
90 TRY_CALL_PTHREADS(
91 pthread_mutex_destroy(&platformData()->ptMutex),
92 "mozilla::detail::MutexImpl::~MutexImpl: pthread_mutex_destroy failed");
95 inline void mozilla::detail::MutexImpl::mutexLock() {
96 TRY_CALL_PTHREADS(
97 pthread_mutex_lock(&platformData()->ptMutex),
98 "mozilla::detail::MutexImpl::mutexLock: pthread_mutex_lock failed");
101 bool mozilla::detail::MutexImpl::tryLock() { return mutexTryLock(); }
103 bool mozilla::detail::MutexImpl::mutexTryLock() {
104 int result = pthread_mutex_trylock(&platformData()->ptMutex);
105 if (result == 0) {
106 return true;
109 if (result == EBUSY) {
110 return false;
113 REPORT_PTHREADS_ERROR(
114 result,
115 "mozilla::detail::MutexImpl::mutexTryLock: pthread_mutex_trylock failed");
118 void mozilla::detail::MutexImpl::lock() { mutexLock(); }
120 void mozilla::detail::MutexImpl::unlock() {
121 TRY_CALL_PTHREADS(
122 pthread_mutex_unlock(&platformData()->ptMutex),
123 "mozilla::detail::MutexImpl::unlock: pthread_mutex_unlock failed");
126 #undef TRY_CALL_PTHREADS
128 mozilla::detail::MutexImpl::PlatformData*
129 mozilla::detail::MutexImpl::platformData() {
130 static_assert(sizeof(platformData_) >= sizeof(PlatformData),
131 "platformData_ is too small");
132 return reinterpret_cast<PlatformData*>(platformData_);