blob: b1d5e7c0d9912ab8b983dde68436448587482170 [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"
Reid Klecknerada8c392017-07-11 16:45:30 +000015#include "llvm/Support/ErrorHandling.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/Config/config.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 =
50 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
Reid Klecknerada8c392017-07-11 16:45:30 +000051
52 if (mutex == nullptr)
53 report_bad_alloc_error("Mutex allocation failed");
54
David Blaikiefdcd6692012-01-15 01:09:13 +000055 pthread_mutexattr_t attr;
Reid Spencerf4049812005-07-12 15:37:43 +000056
David Blaikiefdcd6692012-01-15 01:09:13 +000057 // Initialize the mutex attributes
58 int errorcode = pthread_mutexattr_init(&attr);
Duncan Sandsae22c602012-02-05 14:20:11 +000059 assert(errorcode == 0); (void)errorcode;
Reid Spencerf4049812005-07-12 15:37:43 +000060
David Blaikiefdcd6692012-01-15 01:09:13 +000061 // Initialize the mutex as a recursive mutex, if requested, or normal
62 // otherwise.
63 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
64 errorcode = pthread_mutexattr_settype(&attr, kind);
65 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000066
David Blaikiefdcd6692012-01-15 01:09:13 +000067 // Initialize the mutex
68 errorcode = pthread_mutex_init(mutex, &attr);
69 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000070
David Blaikiefdcd6692012-01-15 01:09:13 +000071 // Destroy the attributes
72 errorcode = pthread_mutexattr_destroy(&attr);
73 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000074
David Blaikiefdcd6692012-01-15 01:09:13 +000075 // Assign the data member
76 data_ = mutex;
Reid Spencerf4049812005-07-12 15:37:43 +000077}
78
79// Destruct a Mutex
Owen Anderson68f65982009-06-18 17:53:17 +000080MutexImpl::~MutexImpl()
Reid Spencerf4049812005-07-12 15:37:43 +000081{
David Blaikiefdcd6692012-01-15 01:09:13 +000082 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000083 assert(mutex != nullptr);
David Blaikiefdcd6692012-01-15 01:09:13 +000084 pthread_mutex_destroy(mutex);
85 free(mutex);
Reid Spencerf4049812005-07-12 15:37:43 +000086}
87
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000088bool
Owen Anderson68f65982009-06-18 17:53:17 +000089MutexImpl::acquire()
Reid Spencerf4049812005-07-12 15:37:43 +000090{
David Blaikiefdcd6692012-01-15 01:09:13 +000091 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000092 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +000093
David Blaikiefdcd6692012-01-15 01:09:13 +000094 int errorcode = pthread_mutex_lock(mutex);
95 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +000096}
97
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000098bool
Owen Anderson68f65982009-06-18 17:53:17 +000099MutexImpl::release()
Reid Spencerf4049812005-07-12 15:37:43 +0000100{
David Blaikiefdcd6692012-01-15 01:09:13 +0000101 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000102 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +0000103
David Blaikiefdcd6692012-01-15 01:09:13 +0000104 int errorcode = pthread_mutex_unlock(mutex);
105 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000106}
107
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000108bool
Owen Anderson68f65982009-06-18 17:53:17 +0000109MutexImpl::tryacquire()
Reid Spencerf4049812005-07-12 15:37:43 +0000110{
David Blaikiefdcd6692012-01-15 01:09:13 +0000111 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000112 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +0000113
David Blaikiefdcd6692012-01-15 01:09:13 +0000114 int errorcode = pthread_mutex_trylock(mutex);
115 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000116}
117
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000118}
Jeff Cohen3800a282005-07-13 02:15:18 +0000119
Reid Spencerf4049812005-07-12 15:37:43 +0000120#elif defined(LLVM_ON_UNIX)
121#include "Unix/Mutex.inc"
122#elif defined( LLVM_ON_WIN32)
Michael J. Spencer447762d2010-11-29 18:16:10 +0000123#include "Windows/Mutex.inc"
Reid Spencerf4049812005-07-12 15:37:43 +0000124#else
Daniel Dunbar037fc932011-10-11 20:02:52 +0000125#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
Reid Spencerf4049812005-07-12 15:37:43 +0000126#endif
Reid Spencerf85fabeb2005-08-24 10:07:20 +0000127#endif