[llvm-exegesis] [NFC] Fixing typo.
[llvm-complete.git] / lib / Support / RWMutex.cpp
blob7ce856b716c6081957ea34df45bf9398afa74cc8
1 //===- RWMutex.cpp - Reader/Writer Mutual Exclusion Lock --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the llvm::sys::RWMutex class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Allocator.h"
14 #include "llvm/Support/RWMutex.h"
15 #include "llvm/Config/config.h"
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only TRULY operating system
19 //=== independent code.
20 //===----------------------------------------------------------------------===//
22 #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
23 // Define all methods as no-ops if threading is explicitly disabled
25 using namespace llvm;
26 using namespace sys;
28 RWMutexImpl::RWMutexImpl() = default;
29 RWMutexImpl::~RWMutexImpl() = default;
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; }
36 #else
38 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
40 #include <cassert>
41 #include <cstdlib>
42 #include <pthread.h>
44 using namespace llvm;
45 using namespace sys;
47 // Construct a RWMutex using pthread calls
48 RWMutexImpl::RWMutexImpl()
50 // Declare the pthread_rwlock data structures
51 pthread_rwlock_t* rwlock =
52 static_cast<pthread_rwlock_t*>(safe_malloc(sizeof(pthread_rwlock_t)));
54 #ifdef __APPLE__
55 // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
56 bzero(rwlock, sizeof(pthread_rwlock_t));
57 #endif
59 // Initialize the rwlock
60 int errorcode = pthread_rwlock_init(rwlock, nullptr);
61 (void)errorcode;
62 assert(errorcode == 0);
64 // Assign the data member
65 data_ = rwlock;
68 // Destruct a RWMutex
69 RWMutexImpl::~RWMutexImpl()
71 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
72 assert(rwlock != nullptr);
73 pthread_rwlock_destroy(rwlock);
74 free(rwlock);
77 bool
78 RWMutexImpl::reader_acquire()
80 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
81 assert(rwlock != nullptr);
83 int errorcode = pthread_rwlock_rdlock(rwlock);
84 return errorcode == 0;
87 bool
88 RWMutexImpl::reader_release()
90 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
91 assert(rwlock != nullptr);
93 int errorcode = pthread_rwlock_unlock(rwlock);
94 return errorcode == 0;
97 bool
98 RWMutexImpl::writer_acquire()
100 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
101 assert(rwlock != nullptr);
103 int errorcode = pthread_rwlock_wrlock(rwlock);
104 return errorcode == 0;
107 bool
108 RWMutexImpl::writer_release()
110 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
111 assert(rwlock != nullptr);
113 int errorcode = pthread_rwlock_unlock(rwlock);
114 return errorcode == 0;
117 #elif defined(LLVM_ON_UNIX)
118 #include "Unix/RWMutex.inc"
119 #elif defined( _WIN32)
120 #include "Windows/RWMutex.inc"
121 #else
122 #warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp
123 #endif
124 #endif