blob: 7138c7a4b984c3de7d49aec5627242df6ffb495f [file] [log] [blame]
Reid Spencerf4049812005-07-12 15:37:43 +00001//===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencerf4049812005-07-12 15:37:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the llvm::sys::Mutex class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "llvm/Support/Mutex.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/Config/config.h"
Nico Weber432a3882018-04-30 14:59:11 +000016#include "llvm/Support/ErrorHandling.h"
Reid Spencerf4049812005-07-12 15:37:43 +000017
Reid Spencerf4049812005-07-12 15:37:43 +000018//===----------------------------------------------------------------------===//
19//=== WARNING: Implementation here must contain only TRULY operating system
20//=== independent code.
21//===----------------------------------------------------------------------===//
22
Dylan Noblesmithefddf202011-11-28 00:48:58 +000023#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
Reid Spencerf85fabeb2005-08-24 10:07:20 +000024// Define all methods as no-ops if threading is explicitly disabled
Jeff Cohen3800a282005-07-13 02:15:18 +000025namespace llvm {
26using namespace sys;
Owen Anderson68f65982009-06-18 17:53:17 +000027MutexImpl::MutexImpl( bool recursive) { }
28MutexImpl::~MutexImpl() { }
29bool MutexImpl::acquire() { return true; }
30bool MutexImpl::release() { return true; }
31bool MutexImpl::tryacquire() { return true; }
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000032}
Reid Spencerf85fabeb2005-08-24 10:07:20 +000033#else
34
35#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
Jeff Cohen3800a282005-07-13 02:15:18 +000036
Reid Spencerf4049812005-07-12 15:37:43 +000037#include <cassert>
38#include <pthread.h>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000039#include <stdlib.h>
Reid Spencerf4049812005-07-12 15:37:43 +000040
Reid Spencerf85fabeb2005-08-24 10:07:20 +000041namespace llvm {
42using namespace sys;
43
Reid Spencerf4049812005-07-12 15:37:43 +000044// Construct a Mutex using pthread calls
Owen Anderson68f65982009-06-18 17:53:17 +000045MutexImpl::MutexImpl( bool recursive)
Craig Topperc10719f2014-04-07 04:17:22 +000046 : data_(nullptr)
Reid Spencerf4049812005-07-12 15:37:43 +000047{
David Blaikiefdcd6692012-01-15 01:09:13 +000048 // Declare the pthread_mutex data structures
49 pthread_mutex_t* mutex =
Serge Pavlov15681ad2018-06-09 05:19:45 +000050 static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t)));
Reid Klecknerada8c392017-07-11 16:45:30 +000051
David Blaikiefdcd6692012-01-15 01:09:13 +000052 pthread_mutexattr_t attr;
Reid Spencerf4049812005-07-12 15:37:43 +000053
David Blaikiefdcd6692012-01-15 01:09:13 +000054 // Initialize the mutex attributes
55 int errorcode = pthread_mutexattr_init(&attr);
Duncan Sandsae22c602012-02-05 14:20:11 +000056 assert(errorcode == 0); (void)errorcode;
Reid Spencerf4049812005-07-12 15:37:43 +000057
David Blaikiefdcd6692012-01-15 01:09:13 +000058 // 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 Spencerf4049812005-07-12 15:37:43 +000063
David Blaikiefdcd6692012-01-15 01:09:13 +000064 // Initialize the mutex
65 errorcode = pthread_mutex_init(mutex, &attr);
66 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000067
David Blaikiefdcd6692012-01-15 01:09:13 +000068 // Destroy the attributes
69 errorcode = pthread_mutexattr_destroy(&attr);
70 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000071
David Blaikiefdcd6692012-01-15 01:09:13 +000072 // Assign the data member
73 data_ = mutex;
Reid Spencerf4049812005-07-12 15:37:43 +000074}
75
76// Destruct a Mutex
Owen Anderson68f65982009-06-18 17:53:17 +000077MutexImpl::~MutexImpl()
Reid Spencerf4049812005-07-12 15:37:43 +000078{
David Blaikiefdcd6692012-01-15 01:09:13 +000079 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000080 assert(mutex != nullptr);
David Blaikiefdcd6692012-01-15 01:09:13 +000081 pthread_mutex_destroy(mutex);
82 free(mutex);
Reid Spencerf4049812005-07-12 15:37:43 +000083}
84
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000085bool
Owen Anderson68f65982009-06-18 17:53:17 +000086MutexImpl::acquire()
Reid Spencerf4049812005-07-12 15:37:43 +000087{
David Blaikiefdcd6692012-01-15 01:09:13 +000088 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000089 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +000090
David Blaikiefdcd6692012-01-15 01:09:13 +000091 int errorcode = pthread_mutex_lock(mutex);
92 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +000093}
94
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000095bool
Owen Anderson68f65982009-06-18 17:53:17 +000096MutexImpl::release()
Reid Spencerf4049812005-07-12 15:37:43 +000097{
David Blaikiefdcd6692012-01-15 01:09:13 +000098 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000099 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +0000100
David Blaikiefdcd6692012-01-15 01:09:13 +0000101 int errorcode = pthread_mutex_unlock(mutex);
102 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000103}
104
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000105bool
Owen Anderson68f65982009-06-18 17:53:17 +0000106MutexImpl::tryacquire()
Reid Spencerf4049812005-07-12 15:37:43 +0000107{
David Blaikiefdcd6692012-01-15 01:09:13 +0000108 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000109 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +0000110
David Blaikiefdcd6692012-01-15 01:09:13 +0000111 int errorcode = pthread_mutex_trylock(mutex);
112 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000113}
114
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000115}
Jeff Cohen3800a282005-07-13 02:15:18 +0000116
Reid Spencerf4049812005-07-12 15:37:43 +0000117#elif defined(LLVM_ON_UNIX)
118#include "Unix/Mutex.inc"
Nico Weber712e8d22018-04-29 00:45:03 +0000119#elif defined( _WIN32)
Michael J. Spencer447762d2010-11-29 18:16:10 +0000120#include "Windows/Mutex.inc"
Reid Spencerf4049812005-07-12 15:37:43 +0000121#else
Nico Weber712e8d22018-04-29 00:45:03 +0000122#warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp
Reid Spencerf4049812005-07-12 15:37:43 +0000123#endif
Reid Spencerf85fabeb2005-08-24 10:07:20 +0000124#endif