1 //===- RWMutex.cpp - Reader/Writer Mutual Exclusion Lock --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the llvm::sys::RWMutex class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/RWMutex.h"
16 #include "llvm/Config/config.h"
18 //===----------------------------------------------------------------------===//
19 //=== WARNING: Implementation here must contain only TRULY operating system
20 //=== independent code.
21 //===----------------------------------------------------------------------===//
23 #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
24 // Define all methods as no-ops if threading is explicitly disabled
29 RWMutexImpl::RWMutexImpl() = default;
30 RWMutexImpl::~RWMutexImpl() = default;
32 bool RWMutexImpl::reader_acquire() { return true; }
33 bool RWMutexImpl::reader_release() { return true; }
34 bool RWMutexImpl::writer_acquire() { return true; }
35 bool RWMutexImpl::writer_release() { return true; }
39 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
48 // Construct a RWMutex using pthread calls
49 RWMutexImpl::RWMutexImpl()
51 // Declare the pthread_rwlock data structures
52 pthread_rwlock_t
* rwlock
=
53 static_cast<pthread_rwlock_t
*>(safe_malloc(sizeof(pthread_rwlock_t
)));
56 // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
57 bzero(rwlock
, sizeof(pthread_rwlock_t
));
60 // Initialize the rwlock
61 int errorcode
= pthread_rwlock_init(rwlock
, nullptr);
63 assert(errorcode
== 0);
65 // Assign the data member
70 RWMutexImpl::~RWMutexImpl()
72 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
73 assert(rwlock
!= nullptr);
74 pthread_rwlock_destroy(rwlock
);
79 RWMutexImpl::reader_acquire()
81 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
82 assert(rwlock
!= nullptr);
84 int errorcode
= pthread_rwlock_rdlock(rwlock
);
85 return errorcode
== 0;
89 RWMutexImpl::reader_release()
91 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
92 assert(rwlock
!= nullptr);
94 int errorcode
= pthread_rwlock_unlock(rwlock
);
95 return errorcode
== 0;
99 RWMutexImpl::writer_acquire()
101 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
102 assert(rwlock
!= nullptr);
104 int errorcode
= pthread_rwlock_wrlock(rwlock
);
105 return errorcode
== 0;
109 RWMutexImpl::writer_release()
111 pthread_rwlock_t
* rwlock
= static_cast<pthread_rwlock_t
*>(data_
);
112 assert(rwlock
!= nullptr);
114 int errorcode
= pthread_rwlock_unlock(rwlock
);
115 return errorcode
== 0;
118 #elif defined(LLVM_ON_UNIX)
119 #include "Unix/RWMutex.inc"
120 #elif defined( _WIN32)
121 #include "Windows/RWMutex.inc"
123 #warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp