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 TRY_CALL_PTHREADS(pthread_mutexattr_setpolicy_np(&attr
, POLICY_KIND
),
70 "mozilla::detail::MutexImpl::MutexImpl: "
71 "pthread_mutexattr_setpolicy_np failed");
77 pthread_mutex_init(&platformData()->ptMutex
, attrp
),
78 "mozilla::detail::MutexImpl::MutexImpl: pthread_mutex_init failed");
80 #if defined(ATTR_REQUIRED)
81 TRY_CALL_PTHREADS(pthread_mutexattr_destroy(&attr
),
82 "mozilla::detail::MutexImpl::MutexImpl: "
83 "pthread_mutexattr_destroy failed");
87 mozilla::detail::MutexImpl::~MutexImpl() {
89 pthread_mutex_destroy(&platformData()->ptMutex
),
90 "mozilla::detail::MutexImpl::~MutexImpl: pthread_mutex_destroy failed");
93 inline void mozilla::detail::MutexImpl::mutexLock() {
95 pthread_mutex_lock(&platformData()->ptMutex
),
96 "mozilla::detail::MutexImpl::mutexLock: pthread_mutex_lock failed");
99 bool mozilla::detail::MutexImpl::tryLock() { return mutexTryLock(); }
101 bool mozilla::detail::MutexImpl::mutexTryLock() {
102 int result
= pthread_mutex_trylock(&platformData()->ptMutex
);
107 if (result
== EBUSY
) {
111 REPORT_PTHREADS_ERROR(
113 "mozilla::detail::MutexImpl::mutexTryLock: pthread_mutex_trylock failed");
116 void mozilla::detail::MutexImpl::lock() { mutexLock(); }
118 void mozilla::detail::MutexImpl::unlock() {
120 pthread_mutex_unlock(&platformData()->ptMutex
),
121 "mozilla::detail::MutexImpl::unlock: pthread_mutex_unlock failed");
124 #undef TRY_CALL_PTHREADS
126 mozilla::detail::MutexImpl::PlatformData
*
127 mozilla::detail::MutexImpl::platformData() {
128 static_assert(sizeof(platformData_
) >= sizeof(PlatformData
),
129 "platformData_ is too small");
130 return reinterpret_cast<PlatformData
*>(platformData_
);