5 #include "src/base/platform/mutex.h" 14 static V8_INLINE
void InitializeNativeHandle(pthread_mutex_t* mutex) {
18 pthread_mutexattr_t attr;
19 result = pthread_mutexattr_init(&attr);
21 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
23 result = pthread_mutex_init(mutex, &attr);
25 result = pthread_mutexattr_destroy(&attr);
28 result = pthread_mutex_init(mutex,
nullptr);
29 #endif // defined(DEBUG) 35 static V8_INLINE
void InitializeRecursiveNativeHandle(pthread_mutex_t* mutex) {
36 pthread_mutexattr_t attr;
37 int result = pthread_mutexattr_init(&attr);
39 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
41 result = pthread_mutex_init(mutex, &attr);
43 result = pthread_mutexattr_destroy(&attr);
49 static V8_INLINE
void DestroyNativeHandle(pthread_mutex_t* mutex) {
50 int result = pthread_mutex_destroy(mutex);
56 static V8_INLINE
void LockNativeHandle(pthread_mutex_t* mutex) {
57 int result = pthread_mutex_lock(mutex);
63 static V8_INLINE
void UnlockNativeHandle(pthread_mutex_t* mutex) {
64 int result = pthread_mutex_unlock(mutex);
70 static V8_INLINE
bool TryLockNativeHandle(pthread_mutex_t* mutex) {
71 int result = pthread_mutex_trylock(mutex);
72 if (result == EBUSY) {
81 InitializeNativeHandle(&native_handle_);
89 DestroyNativeHandle(&native_handle_);
95 LockNativeHandle(&native_handle_);
96 AssertUnheldAndMark();
100 void Mutex::Unlock() {
101 AssertHeldAndUnmark();
102 UnlockNativeHandle(&native_handle_);
106 bool Mutex::TryLock() {
107 if (!TryLockNativeHandle(&native_handle_)) {
110 AssertUnheldAndMark();
115 RecursiveMutex::RecursiveMutex() {
116 InitializeRecursiveNativeHandle(&native_handle_);
123 RecursiveMutex::~RecursiveMutex() {
124 DestroyNativeHandle(&native_handle_);
125 DCHECK_EQ(0, level_);
129 void RecursiveMutex::Lock() {
130 LockNativeHandle(&native_handle_);
132 DCHECK_LE(0, level_);
138 void RecursiveMutex::Unlock() {
140 DCHECK_LT(0, level_);
143 UnlockNativeHandle(&native_handle_);
147 bool RecursiveMutex::TryLock() {
148 if (!TryLockNativeHandle(&native_handle_)) {
152 DCHECK_LE(0, level_);
160 Mutex::Mutex() : native_handle_(SRWLOCK_INIT) {
168 DCHECK_EQ(0, level_);
173 AcquireSRWLockExclusive(&native_handle_);
174 AssertUnheldAndMark();
178 void Mutex::Unlock() {
179 AssertHeldAndUnmark();
180 ReleaseSRWLockExclusive(&native_handle_);
184 bool Mutex::TryLock() {
185 if (!TryAcquireSRWLockExclusive(&native_handle_)) {
188 AssertUnheldAndMark();
193 RecursiveMutex::RecursiveMutex() {
194 InitializeCriticalSection(&native_handle_);
201 RecursiveMutex::~RecursiveMutex() {
202 DeleteCriticalSection(&native_handle_);
203 DCHECK_EQ(0, level_);
207 void RecursiveMutex::Lock() {
208 EnterCriticalSection(&native_handle_);
210 DCHECK_LE(0, level_);
216 void RecursiveMutex::Unlock() {
218 DCHECK_LT(0, level_);
221 LeaveCriticalSection(&native_handle_);
225 bool RecursiveMutex::TryLock() {
226 if (!TryEnterCriticalSection(&native_handle_)) {
230 DCHECK_LE(0, level_);
236 #endif // V8_OS_POSIX