/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/Support/RWMutex.cpp
Line | Count | Source |
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/Support/RWMutex.h" |
15 | | #include "llvm/Config/config.h" |
16 | | |
17 | | //===----------------------------------------------------------------------===// |
18 | | //=== WARNING: Implementation here must contain only TRULY operating system |
19 | | //=== independent code. |
20 | | //===----------------------------------------------------------------------===// |
21 | | |
22 | | #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0 |
23 | | // Define all methods as no-ops if threading is explicitly disabled |
24 | | |
25 | | using namespace llvm; |
26 | | using namespace sys; |
27 | | |
28 | | RWMutexImpl::RWMutexImpl() = default; |
29 | | RWMutexImpl::~RWMutexImpl() = default; |
30 | | |
31 | | bool RWMutexImpl::reader_acquire() { return true; } |
32 | | bool RWMutexImpl::reader_release() { return true; } |
33 | | bool RWMutexImpl::writer_acquire() { return true; } |
34 | | bool RWMutexImpl::writer_release() { return true; } |
35 | | |
36 | | #else |
37 | | |
38 | | #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT) |
39 | | |
40 | | #include <cassert> |
41 | | #include <cstdlib> |
42 | | #include <pthread.h> |
43 | | |
44 | | using namespace llvm; |
45 | | using namespace sys; |
46 | | |
47 | | // Construct a RWMutex using pthread calls |
48 | | RWMutexImpl::RWMutexImpl() |
49 | 100k | { |
50 | 100k | // Declare the pthread_rwlock data structures |
51 | 100k | pthread_rwlock_t* rwlock = |
52 | 100k | static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t))); |
53 | 100k | |
54 | 100k | #ifdef __APPLE__ |
55 | 100k | // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init. |
56 | 100k | bzero(rwlock, sizeof(pthread_rwlock_t)); |
57 | 100k | #endif |
58 | 100k | |
59 | 100k | // Initialize the rwlock |
60 | 100k | int errorcode = pthread_rwlock_init(rwlock, nullptr); |
61 | 100k | (void)errorcode; |
62 | 100k | assert(errorcode == 0); |
63 | 100k | |
64 | 100k | // Assign the data member |
65 | 100k | data_ = rwlock; |
66 | 100k | } |
67 | | |
68 | | // Destruct a RWMutex |
69 | | RWMutexImpl::~RWMutexImpl() |
70 | 95.7k | { |
71 | 95.7k | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
72 | 95.7k | assert(rwlock != nullptr); |
73 | 95.7k | pthread_rwlock_destroy(rwlock); |
74 | 95.7k | free(rwlock); |
75 | 95.7k | } |
76 | | |
77 | | bool |
78 | | RWMutexImpl::reader_acquire() |
79 | 21.5M | { |
80 | 21.5M | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
81 | 21.5M | assert(rwlock != nullptr); |
82 | 21.5M | |
83 | 21.5M | int errorcode = pthread_rwlock_rdlock(rwlock); |
84 | 21.5M | return errorcode == 0; |
85 | 21.5M | } |
86 | | |
87 | | bool |
88 | | RWMutexImpl::reader_release() |
89 | 21.5M | { |
90 | 21.5M | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
91 | 21.5M | assert(rwlock != nullptr); |
92 | 21.5M | |
93 | 21.5M | int errorcode = pthread_rwlock_unlock(rwlock); |
94 | 21.5M | return errorcode == 0; |
95 | 21.5M | } |
96 | | |
97 | | bool |
98 | | RWMutexImpl::writer_acquire() |
99 | 19.7M | { |
100 | 19.7M | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
101 | 19.7M | assert(rwlock != nullptr); |
102 | 19.7M | |
103 | 19.7M | int errorcode = pthread_rwlock_wrlock(rwlock); |
104 | 19.7M | return errorcode == 0; |
105 | 19.7M | } |
106 | | |
107 | | bool |
108 | | RWMutexImpl::writer_release() |
109 | 19.7M | { |
110 | 19.7M | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
111 | 19.7M | assert(rwlock != nullptr); |
112 | 19.7M | |
113 | 19.7M | int errorcode = pthread_rwlock_unlock(rwlock); |
114 | 19.7M | return errorcode == 0; |
115 | 19.7M | } |
116 | | |
117 | | #elif defined(LLVM_ON_UNIX) |
118 | | #include "Unix/RWMutex.inc" |
119 | | #elif defined( LLVM_ON_WIN32) |
120 | | #include "Windows/RWMutex.inc" |
121 | | #else |
122 | | #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp |
123 | | #endif |
124 | | #endif |