Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 1 | //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the llvm::sys::Mutex class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 14 | #include "llvm/Config/config.h" |
Reid Spencer | 0a262ba | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 15 | #include "llvm/System/Mutex.h" |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 16 | |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 19 | //=== independent code. |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Reid Spencer | 0a262ba | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 22 | #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0 |
| 23 | // Define all methods as no-ops if threading is explicitly disabled |
Jeff Cohen | 6d23522 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | using namespace sys; |
Reid Spencer | 0a262ba | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 26 | Mutex::Mutex( bool recursive) { } |
| 27 | Mutex::~Mutex() { } |
| 28 | bool Mutex::acquire() { return true; } |
| 29 | bool Mutex::release() { return true; } |
| 30 | bool Mutex::tryacquire() { return true; } |
| 31 | } |
| 32 | #else |
| 33 | |
| 34 | #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK) |
Jeff Cohen | 6d23522 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 35 | |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 36 | #include <cassert> |
| 37 | #include <pthread.h> |
| 38 | #include <stdlib.h> |
| 39 | |
Reid Spencer | 0a262ba | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 40 | namespace llvm { |
| 41 | using namespace sys; |
| 42 | |
| 43 | |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 44 | // This variable is useful for situations where the pthread library has been |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 45 | // compiled with weak linkage for its interface symbols. This allows the |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 46 | // threading support to be turned off by simply not linking against -lpthread. |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 47 | // In that situation, the value of pthread_mutex_init will be 0 and |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 48 | // consequently pthread_enabled will be false. In such situations, all the |
| 49 | // pthread operations become no-ops and the functions all return false. If |
| 50 | // pthread_mutex_init does have an address, then mutex support is enabled. |
| 51 | // Note: all LLVM tools will link against -lpthread if its available since it |
| 52 | // is configured into the LIBS variable. |
| 53 | // Note: this line of code generates a warning if pthread_mutex_init is not |
Misha Brukman | 15d89cb | 2005-08-02 16:04:59 +0000 | [diff] [blame] | 54 | // declared with weak linkage. It's safe to ignore the warning. |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 55 | static const bool pthread_enabled = static_cast<bool>(pthread_mutex_init); |
| 56 | |
| 57 | // Construct a Mutex using pthread calls |
| 58 | Mutex::Mutex( bool recursive) |
| 59 | : data_(0) |
| 60 | { |
| 61 | if (pthread_enabled) |
| 62 | { |
| 63 | // Declare the pthread_mutex data structures |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 64 | pthread_mutex_t* mutex = |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 65 | static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t))); |
| 66 | pthread_mutexattr_t attr; |
| 67 | |
| 68 | // Initialize the mutex attributes |
| 69 | int errorcode = pthread_mutexattr_init(&attr); |
| 70 | assert(errorcode == 0); |
| 71 | |
| 72 | // Initialize the mutex as a recursive mutex, if requested, or normal |
| 73 | // otherwise. |
| 74 | int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL ); |
| 75 | errorcode = pthread_mutexattr_settype(&attr, kind); |
| 76 | assert(errorcode == 0); |
| 77 | |
Jeff Cohen | 3c280bf | 2006-04-17 17:55:41 +0000 | [diff] [blame^] | 78 | #if !defined(__FreeBSD__) && !defined(__OpenBSD__) |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 79 | // Make it a process local mutex |
| 80 | errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
Reid Spencer | 2499841 | 2005-07-13 03:02:06 +0000 | [diff] [blame] | 81 | #endif |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 82 | |
| 83 | // Initialize the mutex |
| 84 | errorcode = pthread_mutex_init(mutex, &attr); |
| 85 | assert(errorcode == 0); |
| 86 | |
| 87 | // Destroy the attributes |
| 88 | errorcode = pthread_mutexattr_destroy(&attr); |
| 89 | assert(errorcode == 0); |
| 90 | |
| 91 | // Assign the data member |
| 92 | data_ = mutex; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Destruct a Mutex |
| 97 | Mutex::~Mutex() |
| 98 | { |
| 99 | if (pthread_enabled) |
| 100 | { |
| 101 | pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_); |
| 102 | assert(mutex != 0); |
| 103 | int errorcode = pthread_mutex_destroy(mutex); |
| 104 | assert(mutex != 0); |
| 105 | } |
| 106 | } |
| 107 | |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 108 | bool |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 109 | Mutex::acquire() |
| 110 | { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 111 | if (pthread_enabled) |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 112 | { |
| 113 | pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_); |
| 114 | assert(mutex != 0); |
| 115 | |
| 116 | int errorcode = pthread_mutex_lock(mutex); |
| 117 | return errorcode == 0; |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 122 | bool |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 123 | Mutex::release() |
| 124 | { |
| 125 | if (pthread_enabled) |
| 126 | { |
| 127 | pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_); |
| 128 | assert(mutex != 0); |
| 129 | |
| 130 | int errorcode = pthread_mutex_unlock(mutex); |
| 131 | return errorcode == 0; |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 136 | bool |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 137 | Mutex::tryacquire() |
| 138 | { |
| 139 | if (pthread_enabled) |
| 140 | { |
| 141 | pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_); |
| 142 | assert(mutex != 0); |
| 143 | |
| 144 | int errorcode = pthread_mutex_trylock(mutex); |
| 145 | return errorcode == 0; |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | } |
Jeff Cohen | 6d23522 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 151 | |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 152 | #elif defined(LLVM_ON_UNIX) |
| 153 | #include "Unix/Mutex.inc" |
| 154 | #elif defined( LLVM_ON_WIN32) |
| 155 | #include "Win32/Mutex.inc" |
| 156 | #else |
| 157 | #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp |
| 158 | #endif |
Reid Spencer | 0a262ba | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 159 | #endif |