eraseFromDisk no longer throws.
[oota-llvm.git] / lib / System / RWMutex.cpp
1 //===- 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 #include <cstring>
17
18 //===----------------------------------------------------------------------===//
19 //=== WARNING: Implementation here must contain only TRULY operating system
20 //===          independent code.
21 //===----------------------------------------------------------------------===//
22
23 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
24 // Define all methods as no-ops if threading is explicitly disabled
25 namespace llvm {
26 using namespace sys;
27 RWMutexImpl::RWMutexImpl() { }
28 RWMutexImpl::~RWMutexImpl() { }
29 bool RWMutexImpl::reader_acquire() { return true; }
30 bool RWMutexImpl::reader_release() { return true; }
31 bool RWMutexImpl::writer_acquire() { return true; }
32 bool RWMutexImpl::writer_release() { return true; }
33 }
34 #else
35
36 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
37
38 #include <cassert>
39 #include <pthread.h>
40 #include <stdlib.h>
41
42 namespace llvm {
43 using namespace sys;
44
45
46 // This variable is useful for situations where the pthread library has been
47 // compiled with weak linkage for its interface symbols. This allows the
48 // threading support to be turned off by simply not linking against -lpthread.
49 // In that situation, the value of pthread_mutex_init will be 0 and
50 // consequently pthread_enabled will be false. In such situations, all the
51 // pthread operations become no-ops and the functions all return false. If
52 // pthread_rwlock_init does have an address, then rwlock support is enabled.
53 // Note: all LLVM tools will link against -lpthread if its available since it
54 //       is configured into the LIBS variable.
55 // Note: this line of code generates a warning if pthread_rwlock_init is not
56 //       declared with weak linkage. It's safe to ignore the warning.
57 static const bool pthread_enabled = true;
58
59 // Construct a RWMutex using pthread calls
60 RWMutexImpl::RWMutexImpl()
61   : data_(0)
62 {
63   if (pthread_enabled)
64   {
65     // Declare the pthread_rwlock data structures
66     pthread_rwlock_t* rwlock =
67       static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t)));
68
69 #ifdef __APPLE__
70     // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
71     bzero(rwlock, sizeof(pthread_rwlock_t));
72 #endif
73
74     pthread_rwlockattr_t attr;
75
76     // Initialize the rwlock attributes
77     int errorcode = pthread_rwlockattr_init(&attr);
78     assert(errorcode == 0);
79
80 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__)
81     // Make it a process local rwlock
82     errorcode = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
83 #endif
84
85     // Initialize the rwlock
86     errorcode = pthread_rwlock_init(rwlock, &attr);
87     assert(errorcode == 0);
88
89     // Destroy the attributes
90     errorcode = pthread_rwlockattr_destroy(&attr);
91     assert(errorcode == 0);
92
93     // Assign the data member
94     data_ = rwlock;
95   }
96 }
97
98 // Destruct a RWMutex
99 RWMutexImpl::~RWMutexImpl()
100 {
101   if (pthread_enabled)
102   {
103     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
104     assert(rwlock != 0);
105     pthread_rwlock_destroy(rwlock);
106     free(rwlock);
107   }
108 }
109
110 bool
111 RWMutexImpl::reader_acquire()
112 {
113   if (pthread_enabled)
114   {
115     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
116     assert(rwlock != 0);
117
118     int errorcode = pthread_rwlock_rdlock(rwlock);
119     return errorcode == 0;
120   }
121   return false;
122 }
123
124 bool
125 RWMutexImpl::reader_release()
126 {
127   if (pthread_enabled)
128   {
129     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
130     assert(rwlock != 0);
131
132     int errorcode = pthread_rwlock_unlock(rwlock);
133     return errorcode == 0;
134   }
135   return false;
136 }
137
138 bool
139 RWMutexImpl::writer_acquire()
140 {
141   if (pthread_enabled)
142   {
143     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
144     assert(rwlock != 0);
145
146     int errorcode = pthread_rwlock_wrlock(rwlock);
147     return errorcode == 0;
148   }
149   return false;
150 }
151
152 bool
153 RWMutexImpl::writer_release()
154 {
155   if (pthread_enabled)
156   {
157     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
158     assert(rwlock != 0);
159
160     int errorcode = pthread_rwlock_unlock(rwlock);
161     return errorcode == 0;
162   }
163   return false;
164 }
165
166 }
167
168 #elif defined(LLVM_ON_UNIX)
169 #include "Unix/RWMutex.inc"
170 #elif defined( LLVM_ON_WIN32)
171 #include "Win32/RWMutex.inc"
172 #else
173 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp
174 #endif
175 #endif