Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 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" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 15 | #include "llvm/Support/RWMutex.h" |
Owen Anderson | 107a537 | 2009-06-20 00:32:27 +0000 | [diff] [blame] | 16 | #include <cstring> |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 17 | |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 20 | //=== independent code. |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Dylan Noblesmith | 08b73a3 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 23 | #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0 |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 24 | // Define all methods as no-ops if threading is explicitly disabled |
| 25 | namespace llvm { |
| 26 | using namespace sys; |
Owen Anderson | b65e9ed | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 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; } |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 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 | |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 45 | // Construct a RWMutex using pthread calls |
Owen Anderson | b65e9ed | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 46 | RWMutexImpl::RWMutexImpl() |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 47 | : data_(0) |
| 48 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 49 | // Declare the pthread_rwlock data structures |
| 50 | pthread_rwlock_t* rwlock = |
| 51 | static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t))); |
Owen Anderson | 107a537 | 2009-06-20 00:32:27 +0000 | [diff] [blame] | 52 | |
| 53 | #ifdef __APPLE__ |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 54 | // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init. |
| 55 | bzero(rwlock, sizeof(pthread_rwlock_t)); |
Owen Anderson | 107a537 | 2009-06-20 00:32:27 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 58 | // Initialize the rwlock |
| 59 | int errorcode = pthread_rwlock_init(rwlock, NULL); |
| 60 | (void)errorcode; |
| 61 | assert(errorcode == 0); |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 62 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 63 | // Assign the data member |
| 64 | data_ = rwlock; |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Destruct a RWMutex |
Owen Anderson | b65e9ed | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 68 | RWMutexImpl::~RWMutexImpl() |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 69 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 70 | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
| 71 | assert(rwlock != 0); |
| 72 | pthread_rwlock_destroy(rwlock); |
| 73 | free(rwlock); |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool |
Owen Anderson | b65e9ed | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 77 | RWMutexImpl::reader_acquire() |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 78 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 79 | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
| 80 | assert(rwlock != 0); |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 81 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 82 | int errorcode = pthread_rwlock_rdlock(rwlock); |
| 83 | return errorcode == 0; |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool |
Owen Anderson | b65e9ed | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 87 | RWMutexImpl::reader_release() |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 88 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 89 | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
| 90 | assert(rwlock != 0); |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 91 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 92 | int errorcode = pthread_rwlock_unlock(rwlock); |
| 93 | return errorcode == 0; |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool |
Owen Anderson | b65e9ed | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 97 | RWMutexImpl::writer_acquire() |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 98 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 99 | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
| 100 | assert(rwlock != 0); |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 101 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 102 | int errorcode = pthread_rwlock_wrlock(rwlock); |
| 103 | return errorcode == 0; |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool |
Owen Anderson | b65e9ed | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 107 | RWMutexImpl::writer_release() |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 108 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 109 | pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_); |
| 110 | assert(rwlock != 0); |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 111 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 112 | int errorcode = pthread_rwlock_unlock(rwlock); |
| 113 | return errorcode == 0; |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } |
| 117 | |
| 118 | #elif defined(LLVM_ON_UNIX) |
| 119 | #include "Unix/RWMutex.inc" |
| 120 | #elif defined( LLVM_ON_WIN32) |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 121 | #include "Windows/RWMutex.inc" |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 122 | #else |
Daniel Dunbar | 2c607b6 | 2011-10-11 20:02:52 +0000 | [diff] [blame] | 123 | #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp |
Owen Anderson | 2a8cf9a | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 124 | #endif |
| 125 | #endif |