2014-01-20 05:57:00 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-01-25 15:36:41 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
|
2016-08-08 20:37:54 +03:00
|
|
|
#include <errno.h>
|
2016-03-21 07:29:29 +03:00
|
|
|
#include <pthread.h>
|
2016-07-09 01:48:48 +03:00
|
|
|
#include <stdio.h>
|
2020-10-30 16:10:29 +03:00
|
|
|
|
|
|
|
#if defined(XP_DARWIN)
|
|
|
|
# include <pthread_spis.h>
|
|
|
|
#endif
|
2016-03-17 21:53:00 +03:00
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
#include "mozilla/PlatformMutex.h"
|
|
|
|
#include "MutexPlatformData_posix.h"
|
2014-01-20 05:57:00 +04:00
|
|
|
|
2018-05-02 17:11:53 +03:00
|
|
|
#define REPORT_PTHREADS_ERROR(result, msg) \
|
|
|
|
{ \
|
|
|
|
errno = result; \
|
|
|
|
perror(msg); \
|
|
|
|
MOZ_CRASH(msg); \
|
|
|
|
}
|
|
|
|
|
2016-07-09 01:48:48 +03:00
|
|
|
#define TRY_CALL_PTHREADS(call, msg) \
|
2016-08-08 20:37:54 +03:00
|
|
|
{ \
|
|
|
|
int result = (call); \
|
|
|
|
if (result != 0) { \
|
2018-05-02 17:11:53 +03:00
|
|
|
REPORT_PTHREADS_ERROR(result, msg); \
|
2016-08-08 20:37:54 +03:00
|
|
|
} \
|
|
|
|
}
|
2016-07-09 01:48:48 +03:00
|
|
|
|
2020-10-30 16:10:29 +03:00
|
|
|
mozilla::detail::MutexImpl::MutexImpl() {
|
|
|
|
pthread_mutexattr_t* attrp = nullptr;
|
2020-10-28 15:37:58 +03:00
|
|
|
|
2020-10-30 16:10:29 +03:00
|
|
|
#if defined(DEBUG)
|
|
|
|
# define MUTEX_KIND PTHREAD_MUTEX_ERRORCHECK
|
|
|
|
// Linux with glibc, FreeBSD and macOS 10.14+ support adaptive mutexes that
|
|
|
|
// spin for a short number of tries before sleeping. NSPR's locks did this,
|
|
|
|
// too, and it seems like a reasonable thing to do.
|
|
|
|
#elif (defined(__linux__) && defined(__GLIBC__)) || defined(__FreeBSD__)
|
|
|
|
# define MUTEX_KIND PTHREAD_MUTEX_ADAPTIVE_NP
|
|
|
|
#elif defined(XP_DARWIN)
|
|
|
|
# if defined(PTHREAD_MUTEX_POLICY_FIRSTFIT_NP)
|
|
|
|
# define POLICY_KIND PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
|
2020-10-28 15:37:58 +03:00
|
|
|
# else
|
2020-10-30 16:10:29 +03:00
|
|
|
# define POLICY_KIND (3) // The definition is missing in old SDKs
|
2020-10-28 15:37:58 +03:00
|
|
|
# endif
|
2020-10-20 15:07:30 +03:00
|
|
|
#endif
|
|
|
|
|
2020-10-30 16:10:29 +03:00
|
|
|
#if defined(MUTEX_KIND) || defined(POLICY_KIND)
|
2020-10-20 15:07:30 +03:00
|
|
|
# define ATTR_REQUIRED
|
2017-03-21 18:20:36 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ATTR_REQUIRED)
|
2016-03-22 22:51:00 +03:00
|
|
|
pthread_mutexattr_t attr;
|
2016-07-09 01:48:48 +03:00
|
|
|
|
|
|
|
TRY_CALL_PTHREADS(
|
|
|
|
pthread_mutexattr_init(&attr),
|
2017-02-09 04:52:55 +03:00
|
|
|
"mozilla::detail::MutexImpl::MutexImpl: pthread_mutexattr_init failed");
|
2016-07-09 01:48:48 +03:00
|
|
|
|
2020-10-30 16:10:29 +03:00
|
|
|
# if defined(MUTEX_KIND)
|
2017-03-21 18:20:36 +03:00
|
|
|
TRY_CALL_PTHREADS(pthread_mutexattr_settype(&attr, MUTEX_KIND),
|
2017-02-09 04:52:55 +03:00
|
|
|
"mozilla::detail::MutexImpl::MutexImpl: "
|
|
|
|
"pthread_mutexattr_settype failed");
|
2020-10-30 16:10:29 +03:00
|
|
|
# elif defined(POLICY_KIND)
|
|
|
|
if (__builtin_available(macOS 10.14, *)) {
|
|
|
|
TRY_CALL_PTHREADS(pthread_mutexattr_setpolicy_np(&attr, POLICY_KIND),
|
|
|
|
"mozilla::detail::MutexImpl::MutexImpl: "
|
|
|
|
"pthread_mutexattr_setpolicy_np failed");
|
|
|
|
}
|
|
|
|
# endif
|
2016-03-22 22:51:00 +03:00
|
|
|
attrp = &attr;
|
|
|
|
#endif
|
|
|
|
|
2016-07-09 01:48:48 +03:00
|
|
|
TRY_CALL_PTHREADS(
|
|
|
|
pthread_mutex_init(&platformData()->ptMutex, attrp),
|
2017-02-09 04:52:55 +03:00
|
|
|
"mozilla::detail::MutexImpl::MutexImpl: pthread_mutex_init failed");
|
2016-03-22 22:51:00 +03:00
|
|
|
|
2017-03-21 18:20:36 +03:00
|
|
|
#if defined(ATTR_REQUIRED)
|
2016-07-09 01:48:48 +03:00
|
|
|
TRY_CALL_PTHREADS(pthread_mutexattr_destroy(&attr),
|
2017-02-09 04:52:55 +03:00
|
|
|
"mozilla::detail::MutexImpl::MutexImpl: "
|
|
|
|
"pthread_mutexattr_destroy failed");
|
2016-03-22 22:51:00 +03:00
|
|
|
#endif
|
2014-01-20 05:57:00 +04:00
|
|
|
}
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
mozilla::detail::MutexImpl::~MutexImpl() {
|
2016-07-09 01:48:48 +03:00
|
|
|
TRY_CALL_PTHREADS(
|
|
|
|
pthread_mutex_destroy(&platformData()->ptMutex),
|
2017-02-09 04:52:55 +03:00
|
|
|
"mozilla::detail::MutexImpl::~MutexImpl: pthread_mutex_destroy failed");
|
2014-01-20 05:57:00 +04:00
|
|
|
}
|
|
|
|
|
2018-05-02 17:11:53 +03:00
|
|
|
inline void mozilla::detail::MutexImpl::mutexLock() {
|
|
|
|
TRY_CALL_PTHREADS(
|
|
|
|
pthread_mutex_lock(&platformData()->ptMutex),
|
|
|
|
"mozilla::detail::MutexImpl::mutexLock: pthread_mutex_lock failed");
|
|
|
|
}
|
|
|
|
|
2019-01-09 18:09:24 +03:00
|
|
|
bool mozilla::detail::MutexImpl::tryLock() { return mutexTryLock(); }
|
|
|
|
|
|
|
|
bool mozilla::detail::MutexImpl::mutexTryLock() {
|
2018-05-02 17:11:53 +03:00
|
|
|
int result = pthread_mutex_trylock(&platformData()->ptMutex);
|
|
|
|
if (result == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == EBUSY) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORT_PTHREADS_ERROR(
|
|
|
|
result,
|
|
|
|
"mozilla::detail::MutexImpl::mutexTryLock: pthread_mutex_trylock failed");
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:10:29 +03:00
|
|
|
void mozilla::detail::MutexImpl::lock() { mutexLock(); }
|
2014-01-20 05:57:00 +04:00
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
void mozilla::detail::MutexImpl::unlock() {
|
2016-07-09 01:48:48 +03:00
|
|
|
TRY_CALL_PTHREADS(
|
|
|
|
pthread_mutex_unlock(&platformData()->ptMutex),
|
2017-02-09 04:52:55 +03:00
|
|
|
"mozilla::detail::MutexImpl::unlock: pthread_mutex_unlock failed");
|
2014-01-20 05:57:00 +04:00
|
|
|
}
|
2016-07-09 01:48:48 +03:00
|
|
|
|
|
|
|
#undef TRY_CALL_PTHREADS
|
2017-01-25 15:36:41 +03:00
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
mozilla::detail::MutexImpl::PlatformData*
|
|
|
|
mozilla::detail::MutexImpl::platformData() {
|
2017-01-25 15:36:41 +03:00
|
|
|
static_assert(sizeof(platformData_) >= sizeof(PlatformData),
|
|
|
|
"platformData_ is too small");
|
|
|
|
return reinterpret_cast<PlatformData*>(platformData_);
|
|
|
|
}
|