Reid Spencer | f404981 | 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 | f3ebc3f | 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 | f404981 | 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 | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 14 | #include "llvm/Config/config.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Mutex.h" |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 16 | |
Reid Spencer | f404981 | 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 | efddf20 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 22 | #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0 |
Reid Spencer | f85fabeb | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 23 | // Define all methods as no-ops if threading is explicitly disabled |
Jeff Cohen | 3800a28 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | using namespace sys; |
Owen Anderson | 68f6598 | 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 | f85fabeb | 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 | 3800a28 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 35 | |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 36 | #include <cassert> |
| 37 | #include <pthread.h> |
| 38 | #include <stdlib.h> |
| 39 | |
Reid Spencer | f85fabeb | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 40 | namespace llvm { |
| 41 | using namespace sys; |
| 42 | |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 43 | // Construct a Mutex using pthread calls |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 44 | MutexImpl::MutexImpl( bool recursive) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 45 | : data_(nullptr) |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 46 | { |
David Blaikie | fdcd669 | 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 | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 51 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 52 | // Initialize the mutex attributes |
| 53 | int errorcode = pthread_mutexattr_init(&attr); |
Duncan Sands | ae22c60 | 2012-02-05 14:20:11 +0000 | [diff] [blame] | 54 | assert(errorcode == 0); (void)errorcode; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 55 | |
David Blaikie | fdcd669 | 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 | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 61 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 62 | // Initialize the mutex |
| 63 | errorcode = pthread_mutex_init(mutex, &attr); |
| 64 | assert(errorcode == 0); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 65 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 66 | // Destroy the attributes |
| 67 | errorcode = pthread_mutexattr_destroy(&attr); |
| 68 | assert(errorcode == 0); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 69 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 70 | // Assign the data member |
| 71 | data_ = mutex; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Destruct a Mutex |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 75 | MutexImpl::~MutexImpl() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 76 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 77 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 78 | assert(mutex != nullptr); |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 79 | pthread_mutex_destroy(mutex); |
| 80 | free(mutex); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 83 | bool |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 84 | MutexImpl::acquire() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 85 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 86 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 87 | assert(mutex != nullptr); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 88 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 89 | int errorcode = pthread_mutex_lock(mutex); |
| 90 | return errorcode == 0; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 93 | bool |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 94 | MutexImpl::release() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 95 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 96 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 97 | assert(mutex != nullptr); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 98 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 99 | int errorcode = pthread_mutex_unlock(mutex); |
| 100 | return errorcode == 0; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 103 | bool |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 104 | MutexImpl::tryacquire() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 105 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 106 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 107 | assert(mutex != nullptr); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 108 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 109 | int errorcode = pthread_mutex_trylock(mutex); |
| 110 | return errorcode == 0; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } |
Jeff Cohen | 3800a28 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 114 | |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 115 | #elif defined(LLVM_ON_UNIX) |
| 116 | #include "Unix/Mutex.inc" |
| 117 | #elif defined( LLVM_ON_WIN32) |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 118 | #include "Windows/Mutex.inc" |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 119 | #else |
Daniel Dunbar | 037fc93 | 2011-10-11 20:02:52 +0000 | [diff] [blame] | 120 | #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 121 | #endif |
Reid Spencer | f85fabeb | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 122 | #endif |