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"
13 #if defined(XP_DARWIN)
14 # include <pthread_spis.h>
17 #include "mozilla/PlatformMutex.h"
18 #include "MutexPlatformData_posix.h"
20 #define REPORT_PTHREADS_ERROR(result, msg) \
27 #define TRY_CALL_PTHREADS(call, msg) \
29 int result = (call); \
31 REPORT_PTHREADS_ERROR(result, msg); \
35 mozilla::detail::MutexImpl::MutexImpl() {
36 pthread_mutexattr_t
* attrp
= nullptr;
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
49 # define POLICY_KIND (3) // The definition is missing in old SDKs
53 #if defined(MUTEX_KIND) || defined(POLICY_KIND)
54 # define ATTR_REQUIRED
57 #if defined(ATTR_REQUIRED)
58 pthread_mutexattr_t attr
;
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");
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");
89 mozilla::detail::MutexImpl::~MutexImpl() {
91 pthread_mutex_destroy(&platformData()->ptMutex
),
92 "mozilla::detail::MutexImpl::~MutexImpl: pthread_mutex_destroy failed");
95 inline void mozilla::detail::MutexImpl::mutexLock() {
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
);
109 if (result
== EBUSY
) {
113 REPORT_PTHREADS_ERROR(
115 "mozilla::detail::MutexImpl::mutexTryLock: pthread_mutex_trylock failed");
118 void mozilla::detail::MutexImpl::lock() { mutexLock(); }
120 void mozilla::detail::MutexImpl::unlock() {
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_
);