Mutex_posix.cpp (4200B)
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/. */ 6 7 #include "mozilla/Assertions.h" 8 9 #include <errno.h> 10 #include <pthread.h> 11 #include <stdio.h> 12 13 #if defined(XP_DARWIN) 14 # include <pthread_spis.h> 15 #endif 16 17 #include "mozilla/PlatformMutex.h" 18 #include "MutexPlatformData_posix.h" 19 20 #define REPORT_PTHREADS_ERROR(result, msg) \ 21 { \ 22 errno = result; \ 23 perror(msg); \ 24 MOZ_CRASH(msg); \ 25 } 26 27 #define TRY_CALL_PTHREADS(call, msg) \ 28 { \ 29 int result = (call); \ 30 if (result != 0) { \ 31 REPORT_PTHREADS_ERROR(result, msg); \ 32 } \ 33 } 34 35 mozilla::detail::MutexImpl::MutexImpl() { 36 pthread_mutexattr_t* attrp = nullptr; 37 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 52 53 #if defined(MUTEX_KIND) || defined(POLICY_KIND) 54 # define ATTR_REQUIRED 55 #endif 56 57 #if defined(ATTR_REQUIRED) 58 pthread_mutexattr_t attr; 59 60 TRY_CALL_PTHREADS( 61 pthread_mutexattr_init(&attr), 62 "mozilla::detail::MutexImpl::MutexImpl: pthread_mutexattr_init failed"); 63 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"); 72 # endif 73 attrp = &attr; 74 #endif 75 76 TRY_CALL_PTHREADS( 77 pthread_mutex_init(&platformData()->ptMutex, attrp), 78 "mozilla::detail::MutexImpl::MutexImpl: pthread_mutex_init failed"); 79 80 #if defined(ATTR_REQUIRED) 81 TRY_CALL_PTHREADS(pthread_mutexattr_destroy(&attr), 82 "mozilla::detail::MutexImpl::MutexImpl: " 83 "pthread_mutexattr_destroy failed"); 84 #endif 85 } 86 87 mozilla::detail::MutexImpl::~MutexImpl() { 88 TRY_CALL_PTHREADS( 89 pthread_mutex_destroy(&platformData()->ptMutex), 90 "mozilla::detail::MutexImpl::~MutexImpl: pthread_mutex_destroy failed"); 91 } 92 93 inline void mozilla::detail::MutexImpl::mutexLock() { 94 TRY_CALL_PTHREADS( 95 pthread_mutex_lock(&platformData()->ptMutex), 96 "mozilla::detail::MutexImpl::mutexLock: pthread_mutex_lock failed"); 97 } 98 99 bool mozilla::detail::MutexImpl::tryLock() { return mutexTryLock(); } 100 101 bool mozilla::detail::MutexImpl::mutexTryLock() { 102 int result = pthread_mutex_trylock(&platformData()->ptMutex); 103 if (result == 0) { 104 return true; 105 } 106 107 if (result == EBUSY) { 108 return false; 109 } 110 111 REPORT_PTHREADS_ERROR( 112 result, 113 "mozilla::detail::MutexImpl::mutexTryLock: pthread_mutex_trylock failed"); 114 } 115 116 void mozilla::detail::MutexImpl::lock() { mutexLock(); } 117 118 void mozilla::detail::MutexImpl::unlock() { 119 TRY_CALL_PTHREADS( 120 pthread_mutex_unlock(&platformData()->ptMutex), 121 "mozilla::detail::MutexImpl::unlock: pthread_mutex_unlock failed"); 122 } 123 124 #undef TRY_CALL_PTHREADS 125 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_); 131 }