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 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Mutex.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 17 | |
Reid Spencer | f404981 | 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 | |
Dylan Noblesmith | efddf20 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 23 | #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0 |
Reid Spencer | f85fabeb | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 24 | // Define all methods as no-ops if threading is explicitly disabled |
Jeff Cohen | 3800a28 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 25 | namespace llvm { |
| 26 | using namespace sys; |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 27 | MutexImpl::MutexImpl( bool recursive) { } |
| 28 | MutexImpl::~MutexImpl() { } |
| 29 | bool MutexImpl::acquire() { return true; } |
| 30 | bool MutexImpl::release() { return true; } |
| 31 | bool MutexImpl::tryacquire() { return true; } |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 32 | } |
Reid Spencer | f85fabeb | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 33 | #else |
| 34 | |
| 35 | #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK) |
Jeff Cohen | 3800a28 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 36 | |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 37 | #include <cassert> |
| 38 | #include <pthread.h> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 40 | |
Reid Spencer | f85fabeb | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 41 | namespace llvm { |
| 42 | using namespace sys; |
| 43 | |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 44 | // Construct a Mutex using pthread calls |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 45 | MutexImpl::MutexImpl( bool recursive) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 46 | : data_(nullptr) |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 47 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 48 | // Declare the pthread_mutex data structures |
| 49 | pthread_mutex_t* mutex = |
Serge Pavlov | 15681ad | 2018-06-09 05:19:45 +0000 | [diff] [blame] | 50 | static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t))); |
Reid Kleckner | ada8c39 | 2017-07-11 16:45:30 +0000 | [diff] [blame] | 51 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 52 | pthread_mutexattr_t attr; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 53 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 54 | // Initialize the mutex attributes |
| 55 | int errorcode = pthread_mutexattr_init(&attr); |
Duncan Sands | ae22c60 | 2012-02-05 14:20:11 +0000 | [diff] [blame] | 56 | assert(errorcode == 0); (void)errorcode; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 57 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 58 | // Initialize the mutex as a recursive mutex, if requested, or normal |
| 59 | // otherwise. |
| 60 | int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL ); |
| 61 | errorcode = pthread_mutexattr_settype(&attr, kind); |
| 62 | assert(errorcode == 0); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 63 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 64 | // Initialize the mutex |
| 65 | errorcode = pthread_mutex_init(mutex, &attr); |
| 66 | assert(errorcode == 0); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 67 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 68 | // Destroy the attributes |
| 69 | errorcode = pthread_mutexattr_destroy(&attr); |
| 70 | assert(errorcode == 0); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 71 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 72 | // Assign the data member |
| 73 | data_ = mutex; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // Destruct a Mutex |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 77 | MutexImpl::~MutexImpl() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 78 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 79 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 80 | assert(mutex != nullptr); |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 81 | pthread_mutex_destroy(mutex); |
| 82 | free(mutex); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 85 | bool |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 86 | MutexImpl::acquire() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 87 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 88 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 89 | assert(mutex != nullptr); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 90 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 91 | int errorcode = pthread_mutex_lock(mutex); |
| 92 | return errorcode == 0; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 95 | bool |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 96 | MutexImpl::release() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 97 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 98 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 99 | assert(mutex != nullptr); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 100 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 101 | int errorcode = pthread_mutex_unlock(mutex); |
| 102 | return errorcode == 0; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 105 | bool |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 106 | MutexImpl::tryacquire() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 107 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 108 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 109 | assert(mutex != nullptr); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 110 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 111 | int errorcode = pthread_mutex_trylock(mutex); |
| 112 | return errorcode == 0; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 115 | } |
Jeff Cohen | 3800a28 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 116 | |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 117 | #elif defined(LLVM_ON_UNIX) |
| 118 | #include "Unix/Mutex.inc" |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 119 | #elif defined( _WIN32) |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 120 | #include "Windows/Mutex.inc" |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 121 | #else |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 122 | #warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 123 | #endif |
Reid Spencer | f85fabeb | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 124 | #endif |