Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 1 | //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the llvm::sys::Mutex class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Mutex.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "llvm/Config/config.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ErrorHandling.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; } |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 31 | } |
Reid Spencer | f85fabeb | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 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> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 38 | #include <stdlib.h> |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 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 = |
Serge Pavlov | 15681ad | 2018-06-09 05:19:45 +0000 | [diff] [blame] | 49 | static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t))); |
Reid Kleckner | ada8c39 | 2017-07-11 16:45:30 +0000 | [diff] [blame] | 50 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 51 | pthread_mutexattr_t attr; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 52 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 53 | // Initialize the mutex attributes |
| 54 | int errorcode = pthread_mutexattr_init(&attr); |
Duncan Sands | ae22c60 | 2012-02-05 14:20:11 +0000 | [diff] [blame] | 55 | assert(errorcode == 0); (void)errorcode; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 56 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 57 | // Initialize the mutex as a recursive mutex, if requested, or normal |
| 58 | // otherwise. |
| 59 | int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL ); |
| 60 | errorcode = pthread_mutexattr_settype(&attr, kind); |
| 61 | assert(errorcode == 0); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 62 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 63 | // Initialize the mutex |
| 64 | errorcode = pthread_mutex_init(mutex, &attr); |
| 65 | assert(errorcode == 0); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 66 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 67 | // Destroy the attributes |
| 68 | errorcode = pthread_mutexattr_destroy(&attr); |
| 69 | assert(errorcode == 0); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 70 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 71 | // Assign the data member |
| 72 | data_ = mutex; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Destruct a Mutex |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 76 | MutexImpl::~MutexImpl() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 77 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 78 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 79 | assert(mutex != nullptr); |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 80 | pthread_mutex_destroy(mutex); |
| 81 | free(mutex); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 84 | bool |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 85 | MutexImpl::acquire() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 86 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 87 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 88 | assert(mutex != nullptr); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 89 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 90 | int errorcode = pthread_mutex_lock(mutex); |
| 91 | return errorcode == 0; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 94 | bool |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 95 | MutexImpl::release() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 96 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 97 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 98 | assert(mutex != nullptr); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 99 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 100 | int errorcode = pthread_mutex_unlock(mutex); |
| 101 | return errorcode == 0; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 104 | bool |
Owen Anderson | 68f6598 | 2009-06-18 17:53:17 +0000 | [diff] [blame] | 105 | MutexImpl::tryacquire() |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 106 | { |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 107 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 108 | assert(mutex != nullptr); |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 109 | |
David Blaikie | fdcd669 | 2012-01-15 01:09:13 +0000 | [diff] [blame] | 110 | int errorcode = pthread_mutex_trylock(mutex); |
| 111 | return errorcode == 0; |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 114 | } |
Jeff Cohen | 3800a28 | 2005-07-13 02:15:18 +0000 | [diff] [blame] | 115 | |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 116 | #elif defined(LLVM_ON_UNIX) |
| 117 | #include "Unix/Mutex.inc" |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 118 | #elif defined( _WIN32) |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 119 | #include "Windows/Mutex.inc" |
Reid Spencer | f404981 | 2005-07-12 15:37:43 +0000 | [diff] [blame] | 120 | #else |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 121 | #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] | 122 | #endif |
Reid Spencer | f85fabeb | 2005-08-24 10:07:20 +0000 | [diff] [blame] | 123 | #endif |