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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 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" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 15 | #include "llvm/Support/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 | |
Dylan Noblesmith | 08b73a3 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 22 | #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0 |
Reid Spencer | 0a262ba | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 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; |
Owen Anderson | b849a4d | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 26 | MutexImpl::MutexImpl( bool recursive) { } |
| 27 | MutexImpl::~MutexImpl() { } |
| 28 | bool MutexImpl::acquire() { return true; } |
| 29 | bool MutexImpl::release() { return true; } |
| 30 | bool MutexImpl::tryacquire() { return true; } |
Reid Spencer | 0a262ba | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 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 | |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 43 | // Construct a Mutex using pthread calls |
Owen Anderson | b849a4d | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 44 | MutexImpl::MutexImpl( bool recursive) |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 45 | : data_(0) |
| 46 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 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; |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 51 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 52 | // Initialize the mutex attributes |
| 53 | int errorcode = pthread_mutexattr_init(&attr); |
Duncan Sands | 5b8a1db | 2012-02-05 14:20:11 +0000 | [diff] [blame] | 54 | assert(errorcode == 0); (void)errorcode; |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 55 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 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); |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 61 | |
Matthijs Kooijman | f512281 | 2008-06-26 10:36:58 +0000 | [diff] [blame] | 62 | #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__) |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 63 | // Make it a process local mutex |
| 64 | errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
| 65 | assert(errorcode == 0); |
Reid Spencer | 2499841 | 2005-07-13 03:02:06 +0000 | [diff] [blame] | 66 | #endif |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 67 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 68 | // Initialize the mutex |
| 69 | errorcode = pthread_mutex_init(mutex, &attr); |
| 70 | assert(errorcode == 0); |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 71 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 72 | // Destroy the attributes |
| 73 | errorcode = pthread_mutexattr_destroy(&attr); |
| 74 | assert(errorcode == 0); |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 75 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 76 | // Assign the data member |
| 77 | data_ = mutex; |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Destruct a Mutex |
Owen Anderson | b849a4d | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 81 | MutexImpl::~MutexImpl() |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 82 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 83 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
| 84 | assert(mutex != 0); |
| 85 | pthread_mutex_destroy(mutex); |
| 86 | free(mutex); |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 89 | bool |
Owen Anderson | b849a4d | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 90 | MutexImpl::acquire() |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 91 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 92 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
| 93 | assert(mutex != 0); |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 94 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 95 | int errorcode = pthread_mutex_lock(mutex); |
| 96 | return errorcode == 0; |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 99 | bool |
Owen Anderson | b849a4d | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 100 | MutexImpl::release() |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 101 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 102 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
| 103 | assert(mutex != 0); |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 104 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 105 | int errorcode = pthread_mutex_unlock(mutex); |
| 106 | return errorcode == 0; |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 109 | bool |
Owen Anderson | b849a4d | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 110 | MutexImpl::tryacquire() |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 111 | { |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 112 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
| 113 | assert(mutex != 0); |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 114 | |
David Blaikie | 49c0a9a | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 115 | int errorcode = pthread_mutex_trylock(mutex); |
| 116 | return errorcode == 0; |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } |
Jeff Cohen | 6d23522 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 120 | |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 121 | #elif defined(LLVM_ON_UNIX) |
| 122 | #include "Unix/Mutex.inc" |
| 123 | #elif defined( LLVM_ON_WIN32) |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 124 | #include "Windows/Mutex.inc" |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 125 | #else |
Daniel Dunbar | 2c607b6 | 2011-10-11 20:02:52 +0000 | [diff] [blame] | 126 | #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp |
Reid Spencer | b2164e5 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 127 | #endif |
Reid Spencer | 0a262ba | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 128 | #endif |