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/Support/RWMutex.h"
18
//===----------------------------------------------------------------------===//
19
//=== WARNING: Implementation here must contain only TRULY operating system
20
//=== independent code.
21
//===----------------------------------------------------------------------===//
23
#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
24
// Define all methods as no-ops if threading is explicitly disabled
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; }
36
#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
45
// Construct a RWMutex using pthread calls
46
RWMutexImpl::RWMutexImpl()
49
// Declare the pthread_rwlock data structures
50
pthread_rwlock_t* rwlock =
51
static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t)));
54
// Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
55
bzero(rwlock, sizeof(pthread_rwlock_t));
58
// Initialize the rwlock
59
int errorcode = pthread_rwlock_init(rwlock, nullptr);
61
assert(errorcode == 0);
63
// Assign the data member
68
RWMutexImpl::~RWMutexImpl()
70
pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
71
assert(rwlock != nullptr);
72
pthread_rwlock_destroy(rwlock);
77
RWMutexImpl::reader_acquire()
79
pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
80
assert(rwlock != nullptr);
82
int errorcode = pthread_rwlock_rdlock(rwlock);
83
return errorcode == 0;
87
RWMutexImpl::reader_release()
89
pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
90
assert(rwlock != nullptr);
92
int errorcode = pthread_rwlock_unlock(rwlock);
93
return errorcode == 0;
97
RWMutexImpl::writer_acquire()
99
pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
100
assert(rwlock != nullptr);
102
int errorcode = pthread_rwlock_wrlock(rwlock);
103
return errorcode == 0;
107
RWMutexImpl::writer_release()
109
pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
110
assert(rwlock != nullptr);
112
int errorcode = pthread_rwlock_unlock(rwlock);
113
return errorcode == 0;
118
#elif defined(LLVM_ON_UNIX)
119
#include "Unix/RWMutex.inc"
120
#elif defined( LLVM_ON_WIN32)
121
#include "Windows/RWMutex.inc"
123
#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp