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/Config/config.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/System/RWMutex.h"
19 //===----------------------------------------------------------------------===//
20 //=== WARNING: Implementation here must contain only TRULY operating system
21 //=== independent code.
22 //===----------------------------------------------------------------------===//
24 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
25 // Define all methods as no-ops if threading is explicitly disabled
28 RWMutexImpl::RWMutexImpl() { }
29 RWMutexImpl::~RWMutexImpl() { }
30 bool RWMutexImpl::reader_acquire() { return true; }
31 bool RWMutexImpl::reader_release() { return true; }
32 bool RWMutexImpl::writer_acquire() { return true; }
33 bool RWMutexImpl::writer_release() { return true; }
37 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
47 // This variable is useful for situations where the pthread library has been
48 // compiled with weak linkage for its interface symbols. This allows the
49 // threading support to be turned off by simply not linking against -lpthread.
50 // In that situation, the value of pthread_mutex_init will be 0 and
51 // consequently pthread_enabled will be false. In such situations, all the
52 // pthread operations become no-ops and the functions all return false. If
53 // pthread_rwlock_init does have an address, then rwlock support is enabled.
54 // Note: all LLVM tools will link against -lpthread if its available since it
55 // is configured into the LIBS variable.
56 // Note: this line of code generates a warning if pthread_rwlock_init is not
57 // declared with weak linkage. It's safe to ignore the warning.
58 static const bool pthread_enabled = true;
60 // Construct a RWMutex using pthread calls
61 RWMutexImpl::RWMutexImpl()
66 // Declare the pthread_rwlock data structures
67 pthread_rwlock_t* rwlock =
68 static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t)));
71 // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
72 bzero(rwlock, sizeof(pthread_rwlock_t));
75 // Initialize the rwlock
76 int ATTRIBUTE_UNUSED errorcode = pthread_rwlock_init(rwlock, NULL);
77 assert(errorcode == 0);
79 // Assign the data member
85 RWMutexImpl::~RWMutexImpl()
89 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
91 pthread_rwlock_destroy(rwlock);
97 RWMutexImpl::reader_acquire()
101 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
104 int errorcode = pthread_rwlock_rdlock(rwlock);
105 return errorcode == 0;
110 RWMutexImpl::reader_release()
114 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
117 int errorcode = pthread_rwlock_unlock(rwlock);
118 return errorcode == 0;
123 RWMutexImpl::writer_acquire()
127 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
130 int errorcode = pthread_rwlock_wrlock(rwlock);
131 return errorcode == 0;
136 RWMutexImpl::writer_release()
140 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
143 int errorcode = pthread_rwlock_unlock(rwlock);
144 return errorcode == 0;
150 #elif defined(LLVM_ON_UNIX)
151 #include "Unix/RWMutex.inc"
152 #elif defined( LLVM_ON_WIN32)
153 #include "Win32/RWMutex.inc"
155 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp