1 //===----------------------------------------------------------------------===//
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
8 // Abstract interface to shared reader/writer log, hiding platform and
9 // configuration differences.
11 //===----------------------------------------------------------------------===//
13 #ifndef __RWMUTEX_HPP__
14 #define __RWMUTEX_HPP__
18 #elif !defined(_LIBUNWIND_HAS_NO_THREADS)
20 #if defined(__ELF__) && defined(_LIBUNWIND_LINK_PTHREAD_LIB)
21 #pragma comment(lib, "pthread")
27 #if defined(_LIBUNWIND_HAS_NO_THREADS)
29 class _LIBUNWIND_HIDDEN RWMutex
{
31 bool lock_shared() { return true; }
32 bool unlock_shared() { return true; }
33 bool lock() { return true; }
34 bool unlock() { return true; }
39 class _LIBUNWIND_HIDDEN RWMutex
{
42 AcquireSRWLockShared(&_lock
);
45 bool unlock_shared() {
46 ReleaseSRWLockShared(&_lock
);
50 AcquireSRWLockExclusive(&_lock
);
54 ReleaseSRWLockExclusive(&_lock
);
59 SRWLOCK _lock
= SRWLOCK_INIT
;
62 #elif !defined(LIBUNWIND_USE_WEAK_PTHREAD)
64 class _LIBUNWIND_HIDDEN RWMutex
{
66 bool lock_shared() { return pthread_rwlock_rdlock(&_lock
) == 0; }
67 bool unlock_shared() { return pthread_rwlock_unlock(&_lock
) == 0; }
68 bool lock() { return pthread_rwlock_wrlock(&_lock
) == 0; }
69 bool unlock() { return pthread_rwlock_unlock(&_lock
) == 0; }
72 pthread_rwlock_t _lock
= PTHREAD_RWLOCK_INITIALIZER
;
77 extern "C" int __attribute__((weak
))
78 pthread_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
79 void *(*start_routine
)(void *), void *arg
);
80 extern "C" int __attribute__((weak
))
81 pthread_rwlock_rdlock(pthread_rwlock_t
*lock
);
82 extern "C" int __attribute__((weak
))
83 pthread_rwlock_wrlock(pthread_rwlock_t
*lock
);
84 extern "C" int __attribute__((weak
))
85 pthread_rwlock_unlock(pthread_rwlock_t
*lock
);
87 // Calls to the locking functions are gated on pthread_create, and not the
88 // functions themselves, because the data structure should only be locked if
89 // another thread has been created. This is what similar libraries do.
91 class _LIBUNWIND_HIDDEN RWMutex
{
94 return !pthread_create
|| (pthread_rwlock_rdlock(&_lock
) == 0);
96 bool unlock_shared() {
97 return !pthread_create
|| (pthread_rwlock_unlock(&_lock
) == 0);
100 return !pthread_create
|| (pthread_rwlock_wrlock(&_lock
) == 0);
103 return !pthread_create
|| (pthread_rwlock_unlock(&_lock
) == 0);
107 pthread_rwlock_t _lock
= PTHREAD_RWLOCK_INITIALIZER
;
112 } // namespace libunwind
114 #endif // __RWMUTEX_HPP__