blob: 69b7b8126ab1cef22ad3c6880601cfaec4b81603 [file] [log] [blame]
Reid Spencerf4049812005-07-12 15:37:43 +00001//===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Spencerf4049812005-07-12 15:37:43 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the llvm::sys::Mutex class.
10//
11//===----------------------------------------------------------------------===//
12
Michael J. Spencer447762d2010-11-29 18:16:10 +000013#include "llvm/Support/Mutex.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/Config/config.h"
Nico Weber432a3882018-04-30 14:59:11 +000015#include "llvm/Support/ErrorHandling.h"
Reid Spencerf4049812005-07-12 15:37:43 +000016
Reid Spencerf4049812005-07-12 15:37:43 +000017//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only TRULY operating system
19//=== independent code.
20//===----------------------------------------------------------------------===//
21
Dylan Noblesmithefddf202011-11-28 00:48:58 +000022#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
Reid Spencerf85fabeb2005-08-24 10:07:20 +000023// Define all methods as no-ops if threading is explicitly disabled
Jeff Cohen3800a282005-07-13 02:15:18 +000024namespace llvm {
25using namespace sys;
Owen Anderson68f65982009-06-18 17:53:17 +000026MutexImpl::MutexImpl( bool recursive) { }
27MutexImpl::~MutexImpl() { }
28bool MutexImpl::acquire() { return true; }
29bool MutexImpl::release() { return true; }
30bool MutexImpl::tryacquire() { return true; }
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000031}
Reid Spencerf85fabeb2005-08-24 10:07:20 +000032#else
33
34#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
Jeff Cohen3800a282005-07-13 02:15:18 +000035
Reid Spencerf4049812005-07-12 15:37:43 +000036#include <cassert>
37#include <pthread.h>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000038#include <stdlib.h>
Reid Spencerf4049812005-07-12 15:37:43 +000039
Reid Spencerf85fabeb2005-08-24 10:07:20 +000040namespace llvm {
41using namespace sys;
42
Reid Spencerf4049812005-07-12 15:37:43 +000043// Construct a Mutex using pthread calls
Owen Anderson68f65982009-06-18 17:53:17 +000044MutexImpl::MutexImpl( bool recursive)
Craig Topperc10719f2014-04-07 04:17:22 +000045 : data_(nullptr)
Reid Spencerf4049812005-07-12 15:37:43 +000046{
David Blaikiefdcd6692012-01-15 01:09:13 +000047 // Declare the pthread_mutex data structures
48 pthread_mutex_t* mutex =
Serge Pavlov15681ad2018-06-09 05:19:45 +000049 static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t)));
Reid Klecknerada8c392017-07-11 16:45:30 +000050
David Blaikiefdcd6692012-01-15 01:09:13 +000051 pthread_mutexattr_t attr;
Reid Spencerf4049812005-07-12 15:37:43 +000052
David Blaikiefdcd6692012-01-15 01:09:13 +000053 // Initialize the mutex attributes
54 int errorcode = pthread_mutexattr_init(&attr);
Duncan Sandsae22c602012-02-05 14:20:11 +000055 assert(errorcode == 0); (void)errorcode;
Reid Spencerf4049812005-07-12 15:37:43 +000056
David Blaikiefdcd6692012-01-15 01:09:13 +000057 // 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 Spencerf4049812005-07-12 15:37:43 +000062
David Blaikiefdcd6692012-01-15 01:09:13 +000063 // Initialize the mutex
64 errorcode = pthread_mutex_init(mutex, &attr);
65 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000066
David Blaikiefdcd6692012-01-15 01:09:13 +000067 // Destroy the attributes
68 errorcode = pthread_mutexattr_destroy(&attr);
69 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000070
David Blaikiefdcd6692012-01-15 01:09:13 +000071 // Assign the data member
72 data_ = mutex;
Reid Spencerf4049812005-07-12 15:37:43 +000073}
74
75// Destruct a Mutex
Owen Anderson68f65982009-06-18 17:53:17 +000076MutexImpl::~MutexImpl()
Reid Spencerf4049812005-07-12 15:37:43 +000077{
David Blaikiefdcd6692012-01-15 01:09:13 +000078 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000079 assert(mutex != nullptr);
David Blaikiefdcd6692012-01-15 01:09:13 +000080 pthread_mutex_destroy(mutex);
81 free(mutex);
Reid Spencerf4049812005-07-12 15:37:43 +000082}
83
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000084bool
Owen Anderson68f65982009-06-18 17:53:17 +000085MutexImpl::acquire()
Reid Spencerf4049812005-07-12 15:37:43 +000086{
David Blaikiefdcd6692012-01-15 01:09:13 +000087 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000088 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +000089
David Blaikiefdcd6692012-01-15 01:09:13 +000090 int errorcode = pthread_mutex_lock(mutex);
91 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +000092}
93
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000094bool
Owen Anderson68f65982009-06-18 17:53:17 +000095MutexImpl::release()
Reid Spencerf4049812005-07-12 15:37:43 +000096{
David Blaikiefdcd6692012-01-15 01:09:13 +000097 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000098 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +000099
David Blaikiefdcd6692012-01-15 01:09:13 +0000100 int errorcode = pthread_mutex_unlock(mutex);
101 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000102}
103
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000104bool
Owen Anderson68f65982009-06-18 17:53:17 +0000105MutexImpl::tryacquire()
Reid Spencerf4049812005-07-12 15:37:43 +0000106{
David Blaikiefdcd6692012-01-15 01:09:13 +0000107 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000108 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +0000109
David Blaikiefdcd6692012-01-15 01:09:13 +0000110 int errorcode = pthread_mutex_trylock(mutex);
111 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000112}
113
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000114}
Jeff Cohen3800a282005-07-13 02:15:18 +0000115
Reid Spencerf4049812005-07-12 15:37:43 +0000116#elif defined(LLVM_ON_UNIX)
117#include "Unix/Mutex.inc"
Nico Weber712e8d22018-04-29 00:45:03 +0000118#elif defined( _WIN32)
Michael J. Spencer447762d2010-11-29 18:16:10 +0000119#include "Windows/Mutex.inc"
Reid Spencerf4049812005-07-12 15:37:43 +0000120#else
Nico Weber712e8d22018-04-29 00:45:03 +0000121#warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp
Reid Spencerf4049812005-07-12 15:37:43 +0000122#endif
Reid Spencerf85fabeb2005-08-24 10:07:20 +0000123#endif