Mutex.cpp (226633) | Mutex.cpp (234353) |
---|---|
1//===- Mutex.cpp - 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//===----------------------------------------------------------------------===// --- 5 unchanged lines hidden (view full) --- 14#include "llvm/Config/config.h" 15#include "llvm/Support/Mutex.h" 16 17//===----------------------------------------------------------------------===// 18//=== WARNING: Implementation here must contain only TRULY operating system 19//=== independent code. 20//===----------------------------------------------------------------------===// 21 | 1//===- Mutex.cpp - 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//===----------------------------------------------------------------------===// --- 5 unchanged lines hidden (view full) --- 14#include "llvm/Config/config.h" 15#include "llvm/Support/Mutex.h" 16 17//===----------------------------------------------------------------------===// 18//=== WARNING: Implementation here must contain only TRULY operating system 19//=== independent code. 20//===----------------------------------------------------------------------===// 21 |
22#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0 | 22#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0 |
23// Define all methods as no-ops if threading is explicitly disabled 24namespace llvm { 25using namespace sys; 26MutexImpl::MutexImpl( bool recursive) { } 27MutexImpl::~MutexImpl() { } 28bool MutexImpl::acquire() { return true; } 29bool MutexImpl::release() { return true; } 30bool MutexImpl::tryacquire() { return true; } --- 4 unchanged lines hidden (view full) --- 35 36#include <cassert> 37#include <pthread.h> 38#include <stdlib.h> 39 40namespace llvm { 41using namespace sys; 42 | 23// Define all methods as no-ops if threading is explicitly disabled 24namespace llvm { 25using namespace sys; 26MutexImpl::MutexImpl( bool recursive) { } 27MutexImpl::~MutexImpl() { } 28bool MutexImpl::acquire() { return true; } 29bool MutexImpl::release() { return true; } 30bool MutexImpl::tryacquire() { return true; } --- 4 unchanged lines hidden (view full) --- 35 36#include <cassert> 37#include <pthread.h> 38#include <stdlib.h> 39 40namespace llvm { 41using namespace sys; 42 |
43 44// This variable is useful for situations where the pthread library has been 45// compiled with weak linkage for its interface symbols. This allows the 46// threading support to be turned off by simply not linking against -lpthread. 47// In that situation, the value of pthread_mutex_init will be 0 and 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 54// declared with weak linkage. It's safe to ignore the warning. 55static const bool pthread_enabled = true; 56 | |
57// Construct a Mutex using pthread calls 58MutexImpl::MutexImpl( bool recursive) 59 : data_(0) 60{ | 43// Construct a Mutex using pthread calls 44MutexImpl::MutexImpl( bool recursive) 45 : data_(0) 46{ |
61 if (pthread_enabled) 62 { 63 // Declare the pthread_mutex data structures 64 pthread_mutex_t* mutex = 65 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t))); 66 pthread_mutexattr_t attr; | 47 // Declare the pthread_mutex data structures 48 pthread_mutex_t* mutex = 49 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t))); 50 pthread_mutexattr_t attr; |
67 | 51 |
68 // Initialize the mutex attributes 69 int errorcode = pthread_mutexattr_init(&attr); 70 assert(errorcode == 0); | 52 // Initialize the mutex attributes 53 int errorcode = pthread_mutexattr_init(&attr); 54 assert(errorcode == 0); (void)errorcode; |
71 | 55 |
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); | 56 // Initialize the mutex as a recursive mutex, if requested, or normal 57 // otherwise. 58 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL ); 59 errorcode = pthread_mutexattr_settype(&attr, kind); 60 assert(errorcode == 0); |
77 78#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__) | 61 62#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__) |
79 // Make it a process local mutex 80 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); 81 assert(errorcode == 0); | 63 // Make it a process local mutex 64 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); 65 assert(errorcode == 0); |
82#endif 83 | 66#endif 67 |
84 // Initialize the mutex 85 errorcode = pthread_mutex_init(mutex, &attr); 86 assert(errorcode == 0); | 68 // Initialize the mutex 69 errorcode = pthread_mutex_init(mutex, &attr); 70 assert(errorcode == 0); |
87 | 71 |
88 // Destroy the attributes 89 errorcode = pthread_mutexattr_destroy(&attr); 90 assert(errorcode == 0); | 72 // Destroy the attributes 73 errorcode = pthread_mutexattr_destroy(&attr); 74 assert(errorcode == 0); |
91 | 75 |
92 // Assign the data member 93 data_ = mutex; 94 } | 76 // Assign the data member 77 data_ = mutex; |
95} 96 97// Destruct a Mutex 98MutexImpl::~MutexImpl() 99{ | 78} 79 80// Destruct a Mutex 81MutexImpl::~MutexImpl() 82{ |
100 if (pthread_enabled) 101 { 102 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 103 assert(mutex != 0); 104 pthread_mutex_destroy(mutex); 105 free(mutex); 106 } | 83 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 84 assert(mutex != 0); 85 pthread_mutex_destroy(mutex); 86 free(mutex); |
107} 108 109bool 110MutexImpl::acquire() 111{ | 87} 88 89bool 90MutexImpl::acquire() 91{ |
112 if (pthread_enabled) 113 { 114 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 115 assert(mutex != 0); | 92 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 93 assert(mutex != 0); |
116 | 94 |
117 int errorcode = pthread_mutex_lock(mutex); 118 return errorcode == 0; 119 } else return false; | 95 int errorcode = pthread_mutex_lock(mutex); 96 return errorcode == 0; |
120} 121 122bool 123MutexImpl::release() 124{ | 97} 98 99bool 100MutexImpl::release() 101{ |
125 if (pthread_enabled) 126 { 127 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 128 assert(mutex != 0); | 102 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 103 assert(mutex != 0); |
129 | 104 |
130 int errorcode = pthread_mutex_unlock(mutex); 131 return errorcode == 0; 132 } else return false; | 105 int errorcode = pthread_mutex_unlock(mutex); 106 return errorcode == 0; |
133} 134 135bool 136MutexImpl::tryacquire() 137{ | 107} 108 109bool 110MutexImpl::tryacquire() 111{ |
138 if (pthread_enabled) 139 { 140 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 141 assert(mutex != 0); | 112 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 113 assert(mutex != 0); |
142 | 114 |
143 int errorcode = pthread_mutex_trylock(mutex); 144 return errorcode == 0; 145 } else return false; | 115 int errorcode = pthread_mutex_trylock(mutex); 116 return errorcode == 0; |
146} 147 148} 149 150#elif defined(LLVM_ON_UNIX) 151#include "Unix/Mutex.inc" 152#elif defined( LLVM_ON_WIN32) 153#include "Windows/Mutex.inc" 154#else 155#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp 156#endif 157#endif | 117} 118 119} 120 121#elif defined(LLVM_ON_UNIX) 122#include "Unix/Mutex.inc" 123#elif defined( LLVM_ON_WIN32) 124#include "Windows/Mutex.inc" 125#else 126#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp 127#endif 128#endif |