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/System/RWMutex.h"
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only TRULY operating system
19 //=== independent code.
20 //===----------------------------------------------------------------------===//
22 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
23 // Define all methods as no-ops if threading is explicitly disabled
26 RWMutex::RWMutex( bool recursive) { }
27 RWMutex::~RWMutex() { }
28 bool RWMutex::reader_acquire() { return true; }
29 bool RWMutex::reader_release() { return true; }
30 bool RWMutex::writer_acquire() { return true; }
31 bool RWMutex::writer_release() { return true; }
35 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
45 // This variable is useful for situations where the pthread library has been
46 // compiled with weak linkage for its interface symbols. This allows the
47 // threading support to be turned off by simply not linking against -lpthread.
48 // In that situation, the value of pthread_mutex_init will be 0 and
49 // consequently pthread_enabled will be false. In such situations, all the
50 // pthread operations become no-ops and the functions all return false. If
51 // pthread_rwlock_init does have an address, then rwlock support is enabled.
52 // Note: all LLVM tools will link against -lpthread if its available since it
53 // is configured into the LIBS variable.
54 // Note: this line of code generates a warning if pthread_rwlock_init is not
55 // declared with weak linkage. It's safe to ignore the warning.
56 static const bool pthread_enabled = true;
58 // Construct a RWMutex using pthread calls
64 // Declare the pthread_rwlock data structures
65 pthread_rwlock_t* rwlock =
66 static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t)));
67 pthread_rwlockattr_t attr;
69 // Initialize the rwlock attributes
70 int errorcode = pthread_rwlockattr_init(&attr);
71 assert(errorcode == 0);
73 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__)
74 // Make it a process local rwlock
75 errorcode = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
78 // Initialize the rwlock
79 errorcode = pthread_rwlock_init(rwlock, &attr);
80 assert(errorcode == 0);
82 // Destroy the attributes
83 errorcode = pthread_rwlockattr_destroy(&attr);
84 assert(errorcode == 0);
86 // Assign the data member
96 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
98 pthread_rwlock_destroy(rwlock);
104 RWMutex::reader_acquire()
108 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
111 int errorcode = pthread_rwlock_rdlock(rwlock);
112 return errorcode == 0;
118 RWMutex::reader_release()
122 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
125 int errorcode = pthread_rwlock_unlock(rwlock);
126 return errorcode == 0;
132 RWMutex::writer_acquire()
136 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
139 int errorcode = pthread_rwlock_wrlock(rwlock);
140 return errorcode == 0;
146 RWMutex::writer_release()
150 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
153 int errorcode = pthread_rwlock_unlock(rwlock);
154 return errorcode == 0;
161 #elif defined(LLVM_ON_UNIX)
162 #include "Unix/RWMutex.inc"
163 #elif defined( LLVM_ON_WIN32)
164 #include "Win32/RWMutex.inc"
166 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp