blob: a781a379ed418480d2fefcdfed24947e2e64f514 [file] [log] [blame]
Owen Anderson2a8cf9a2009-06-16 20:19:28 +00001//===- RWMutex.cpp - Reader/Writer Mutual Exclusion Lock --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the llvm::sys::RWMutex class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Config/config.h"
15#include "llvm/System/RWMutex.h"
16
17//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only TRULY operating system
19//=== independent code.
20//===----------------------------------------------------------------------===//
21
22#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
23// Define all methods as no-ops if threading is explicitly disabled
24namespace llvm {
25using namespace sys;
26RWMutex::RWMutex( bool recursive) { }
27RWMutex::~RWMutex() { }
28bool RWMutex::reader_acquire() { return true; }
29bool RWMutex::reader_release() { return true; }
30bool RWMutex::writer_acquire() { return true; }
31bool RWMutex::writer_release() { return true; }
32}
33#else
34
35#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
36
37#include <cassert>
38#include <pthread.h>
39#include <stdlib.h>
40
41namespace llvm {
42using namespace sys;
43
44
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.
56static const bool pthread_enabled = true;
57
58// Construct a RWMutex using pthread calls
59RWMutex::RWMutex()
60 : data_(0)
61{
62 if (pthread_enabled)
63 {
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;
68
69 // Initialize the rwlock attributes
70 int errorcode = pthread_rwlockattr_init(&attr);
71 assert(errorcode == 0);
72
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);
76#endif
77
78 // Initialize the rwlock
79 errorcode = pthread_rwlock_init(rwlock, &attr);
80 assert(errorcode == 0);
81
82 // Destroy the attributes
83 errorcode = pthread_rwlockattr_destroy(&attr);
84 assert(errorcode == 0);
85
86 // Assign the data member
87 data_ = rwlock;
88 }
89}
90
91// Destruct a RWMutex
92RWMutex::~RWMutex()
93{
94 if (pthread_enabled)
95 {
96 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
97 assert(rwlock != 0);
98 pthread_rwlock_destroy(rwlock);
99 free(rwlock);
100 }
101}
102
103bool
104RWMutex::reader_acquire()
105{
106 if (pthread_enabled)
107 {
108 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
109 assert(rwlock != 0);
110
111 int errorcode = pthread_rwlock_rdlock(rwlock);
112 return errorcode == 0;
113 }
114 return false;
115}
116
117bool
118RWMutex::reader_release()
119{
120 if (pthread_enabled)
121 {
122 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
123 assert(rwlock != 0);
124
125 int errorcode = pthread_rwlock_unlock(rwlock);
126 return errorcode == 0;
127 }
128 return false;
129}
130
131bool
132RWMutex::writer_acquire()
133{
134 if (pthread_enabled)
135 {
136 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
137 assert(rwlock != 0);
138
139 int errorcode = pthread_rwlock_wrlock(rwlock);
140 return errorcode == 0;
141 }
142 return false;
143}
144
145bool
146RWMutex::writer_release()
147{
148 if (pthread_enabled)
149 {
150 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
151 assert(rwlock != 0);
152
153 int errorcode = pthread_rwlock_unlock(rwlock);
154 return errorcode == 0;
155 }
156 return false;
157}
158
159}
160
161#elif defined(LLVM_ON_UNIX)
162#include "Unix/RWMutex.inc"
163#elif defined( LLVM_ON_WIN32)
164#include "Win32/RWMutex.inc"
165#else
166#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp
167#endif
168#endif
169