Path: blob/main/contrib/llvm-project/libunwind/src/RWMutex.hpp
35154 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//7// Abstract interface to shared reader/writer log, hiding platform and8// configuration differences.9//10//===----------------------------------------------------------------------===//1112#ifndef __RWMUTEX_HPP__13#define __RWMUTEX_HPP__1415#if defined(_WIN32)16#include <windows.h>17#elif !defined(_LIBUNWIND_HAS_NO_THREADS)18#include <pthread.h>19#if defined(__ELF__) && defined(_LIBUNWIND_LINK_PTHREAD_LIB)20#pragma comment(lib, "pthread")21#endif22#endif2324namespace libunwind {2526#if defined(_LIBUNWIND_HAS_NO_THREADS)2728class _LIBUNWIND_HIDDEN RWMutex {29public:30bool lock_shared() { return true; }31bool unlock_shared() { return true; }32bool lock() { return true; }33bool unlock() { return true; }34};3536#elif defined(_WIN32)3738class _LIBUNWIND_HIDDEN RWMutex {39public:40bool lock_shared() {41AcquireSRWLockShared(&_lock);42return true;43}44bool unlock_shared() {45ReleaseSRWLockShared(&_lock);46return true;47}48bool lock() {49AcquireSRWLockExclusive(&_lock);50return true;51}52bool unlock() {53ReleaseSRWLockExclusive(&_lock);54return true;55}5657private:58SRWLOCK _lock = SRWLOCK_INIT;59};6061#elif !defined(LIBUNWIND_USE_WEAK_PTHREAD)6263class _LIBUNWIND_HIDDEN RWMutex {64public:65bool lock_shared() { return pthread_rwlock_rdlock(&_lock) == 0; }66bool unlock_shared() { return pthread_rwlock_unlock(&_lock) == 0; }67bool lock() { return pthread_rwlock_wrlock(&_lock) == 0; }68bool unlock() { return pthread_rwlock_unlock(&_lock) == 0; }6970private:71pthread_rwlock_t _lock = PTHREAD_RWLOCK_INITIALIZER;72};7374#else7576extern "C" int __attribute__((weak))77pthread_create(pthread_t *thread, const pthread_attr_t *attr,78void *(*start_routine)(void *), void *arg);79extern "C" int __attribute__((weak))80pthread_rwlock_rdlock(pthread_rwlock_t *lock);81extern "C" int __attribute__((weak))82pthread_rwlock_wrlock(pthread_rwlock_t *lock);83extern "C" int __attribute__((weak))84pthread_rwlock_unlock(pthread_rwlock_t *lock);8586// Calls to the locking functions are gated on pthread_create, and not the87// functions themselves, because the data structure should only be locked if88// another thread has been created. This is what similar libraries do.8990class _LIBUNWIND_HIDDEN RWMutex {91public:92bool lock_shared() {93return !pthread_create || (pthread_rwlock_rdlock(&_lock) == 0);94}95bool unlock_shared() {96return !pthread_create || (pthread_rwlock_unlock(&_lock) == 0);97}98bool lock() {99return !pthread_create || (pthread_rwlock_wrlock(&_lock) == 0);100}101bool unlock() {102return !pthread_create || (pthread_rwlock_unlock(&_lock) == 0);103}104105private:106pthread_rwlock_t _lock = PTHREAD_RWLOCK_INITIALIZER;107};108109#endif110111} // namespace libunwind112113#endif // __RWMUTEX_HPP__114115116