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