1 //===----------------------------- Registers.hpp --------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 // 8 // Abstract interface to shared reader/writer log, hiding platform and 9 // configuration differences. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef __RWMUTEX_HPP__ 14 #define __RWMUTEX_HPP__ 15 16 #if defined(_WIN32) 17 #include <windows.h> 18 #elif !defined(_LIBUNWIND_HAS_NO_THREADS) 19 #include <pthread.h> 20 #endif 21 22 namespace libunwind { 23 24 #if defined(_LIBUNWIND_HAS_NO_THREADS) 25 26 class _LIBUNWIND_HIDDEN RWMutex { 27 public: lock_shared()28 bool lock_shared() { return true; } unlock_shared()29 bool unlock_shared() { return true; } lock()30 bool lock() { return true; } unlock()31 bool unlock() { return true; } 32 }; 33 34 #elif defined(_WIN32) 35 36 class _LIBUNWIND_HIDDEN RWMutex { 37 public: 38 bool lock_shared() { 39 AcquireSRWLockShared(&_lock); 40 return true; 41 } 42 bool unlock_shared() { 43 ReleaseSRWLockShared(&_lock); 44 return true; 45 } 46 bool lock() { 47 AcquireSRWLockExclusive(&_lock); 48 return true; 49 } 50 bool unlock() { 51 ReleaseSRWLockExclusive(&_lock); 52 return true; 53 } 54 55 private: 56 SRWLOCK _lock = SRWLOCK_INIT; 57 }; 58 59 #else 60 61 class _LIBUNWIND_HIDDEN RWMutex { 62 public: 63 bool lock_shared() { return pthread_rwlock_rdlock(&_lock) == 0; } 64 bool unlock_shared() { return pthread_rwlock_unlock(&_lock) == 0; } 65 bool lock() { return pthread_rwlock_wrlock(&_lock) == 0; } 66 bool unlock() { return pthread_rwlock_unlock(&_lock) == 0; } 67 68 private: 69 pthread_rwlock_t _lock = PTHREAD_RWLOCK_INITIALIZER; 70 }; 71 72 #endif 73 74 } // namespace libunwind 75 76 #endif // __RWMUTEX_HPP__ 77