Martin Storsjo | 12339e4 | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 1 | //===----------------------------- Registers.hpp --------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | // |
| 9 | // Abstract interface to shared reader/writer log, hiding platform and |
| 10 | // configuration differences. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef __RWMUTEX_HPP__ |
| 15 | #define __RWMUTEX_HPP__ |
| 16 | |
| 17 | #if defined(_WIN32) |
| 18 | #include <windows.h> |
| 19 | #elif !defined(_LIBUNWIND_HAS_NO_THREADS) |
| 20 | #include <pthread.h> |
| 21 | #endif |
| 22 | |
| 23 | namespace libunwind { |
| 24 | |
| 25 | #if defined(_LIBUNWIND_HAS_NO_THREADS) |
| 26 | |
| 27 | class _LIBUNWIND_HIDDEN RWMutex { |
| 28 | public: |
| 29 | bool lock_shared() { return true; } |
| 30 | bool unlock_shared() { return true; } |
| 31 | bool lock() { return true; } |
| 32 | bool unlock() { return true; } |
| 33 | }; |
| 34 | |
| 35 | #elif defined(_WIN32) |
| 36 | |
| 37 | class _LIBUNWIND_HIDDEN RWMutex { |
| 38 | public: |
| 39 | bool lock_shared() { |
| 40 | AcquireSRWLockShared(&_lock); |
| 41 | return true; |
| 42 | } |
| 43 | bool unlock_shared() { |
| 44 | ReleaseSRWLockShared(&_lock); |
| 45 | return true; |
| 46 | } |
| 47 | bool lock() { |
| 48 | AcquireSRWLockExclusive(&_lock); |
| 49 | return true; |
| 50 | } |
| 51 | bool unlock() { |
| 52 | ReleaseSRWLockExclusive(&_lock); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | SRWLOCK _lock = SRWLOCK_INIT; |
| 58 | }; |
| 59 | |
| 60 | #else |
| 61 | |
| 62 | class _LIBUNWIND_HIDDEN RWMutex { |
| 63 | public: |
| 64 | bool lock_shared() { return pthread_rwlock_rdlock(&_lock) == 0; } |
| 65 | bool unlock_shared() { return pthread_rwlock_unlock(&_lock) == 0; } |
| 66 | bool lock() { return pthread_rwlock_wrlock(&_lock) == 0; } |
| 67 | bool unlock() { return pthread_rwlock_unlock(&_lock) == 0; } |
| 68 | |
| 69 | private: |
| 70 | pthread_rwlock_t _lock = PTHREAD_RWLOCK_INITIALIZER; |
| 71 | }; |
| 72 | |
| 73 | #endif |
| 74 | |
| 75 | } // namespace libunwind |
| 76 | |
| 77 | #endif // __RWMUTEX_HPP__ |