1 //===- RWMutex.cpp - Reader/Writer Mutual Exclusion Lock --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the llvm::sys::RWMutex class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Allocator.h"
14 #include "llvm/Support/RWMutex.h"
15 #include "llvm/Config/config.h"
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only TRULY operating system
19 //=== independent code.
20 //===----------------------------------------------------------------------===//
22 #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
23 // Define all methods as no-ops if threading is explicitly disabled
28 RWMutexImpl::RWMutexImpl() = default;
29 RWMutexImpl::~RWMutexImpl() = default;
31 bool RWMutexImpl::reader_acquire() { return true; }
32 bool RWMutexImpl::reader_release() { return true; }
33 bool RWMutexImpl::writer_acquire() { return true; }
34 bool RWMutexImpl::writer_release() { return true; }
38 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
47 // Construct a RWMutex using pthread calls
48 RWMutexImpl::RWMutexImpl()
50 // Declare the pthread_rwlock data structures
51 pthread_rwlock_t
* rwlock
=
52 static_cast<pthread_rwlock_t
*>(safe_malloc(sizeof(pthread_rwlock_t
)));
55 // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
56 bzero(rwlock
, sizeof(pthread_rwlock_t
));
59 // Initialize the rwlock
60 int errorcode
= pthread_rwlock_init(rwlock
, nullptr);
62 assert(errorcode
== 0);
64 // Assign the data member
69 RWMutexImpl::~RWMutexImpl()
71 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
72 assert(rwlock
!= nullptr);
73 pthread_rwlock_destroy(rwlock
);
78 RWMutexImpl::reader_acquire()
80 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
81 assert(rwlock
!= nullptr);
83 int errorcode
= pthread_rwlock_rdlock(rwlock
);
84 return errorcode
== 0;
88 RWMutexImpl::reader_release()
90 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
91 assert(rwlock
!= nullptr);
93 int errorcode
= pthread_rwlock_unlock(rwlock
);
94 return errorcode
== 0;
98 RWMutexImpl::writer_acquire()
100 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
101 assert(rwlock
!= nullptr);
103 int errorcode
= pthread_rwlock_wrlock(rwlock
);
104 return errorcode
== 0;
108 RWMutexImpl::writer_release()
110 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
111 assert(rwlock
!= nullptr);
113 int errorcode
= pthread_rwlock_unlock(rwlock
);
114 return errorcode
== 0;
117 #elif defined(LLVM_ON_UNIX)
118 #include "Unix/RWMutex.inc"
119 #elif defined( _WIN32)
120 #include "Windows/RWMutex.inc"
122 #warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp