blob: db30c73de510090ecaf256df8d00e1dc71cc064e [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
Reid Spencerf4049812005-07-12 15:37:43 +000014#include "llvm/Config/config.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/Mutex.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 {
Eugene Zelenko1760dc22016-04-05 20:19:49 +000025
Jeff Cohen3800a282005-07-13 02:15:18 +000026using namespace sys;
Eugene Zelenko1760dc22016-04-05 20:19:49 +000027
Owen Anderson68f65982009-06-18 17:53:17 +000028MutexImpl::MutexImpl( bool recursive) { }
29MutexImpl::~MutexImpl() { }
30bool MutexImpl::acquire() { return true; }
31bool MutexImpl::release() { return true; }
32bool MutexImpl::tryacquire() { return true; }
Eugene Zelenko1760dc22016-04-05 20:19:49 +000033
34} // end namespace llvm
Reid Spencerf85fabeb2005-08-24 10:07:20 +000035#else
36
37#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
Jeff Cohen3800a282005-07-13 02:15:18 +000038
Reid Spencerf4049812005-07-12 15:37:43 +000039#include <cassert>
Eugene Zelenko1760dc22016-04-05 20:19:49 +000040#include <cstdlib>
Reid Spencerf4049812005-07-12 15:37:43 +000041#include <pthread.h>
Reid Spencerf4049812005-07-12 15:37:43 +000042
Reid Spencerf85fabeb2005-08-24 10:07:20 +000043namespace llvm {
Eugene Zelenko1760dc22016-04-05 20:19:49 +000044
Reid Spencerf85fabeb2005-08-24 10:07:20 +000045using namespace sys;
46
Reid Spencerf4049812005-07-12 15:37:43 +000047// Construct a Mutex using pthread calls
Owen Anderson68f65982009-06-18 17:53:17 +000048MutexImpl::MutexImpl( bool recursive)
Craig Topperc10719f2014-04-07 04:17:22 +000049 : data_(nullptr)
Reid Spencerf4049812005-07-12 15:37:43 +000050{
David Blaikiefdcd6692012-01-15 01:09:13 +000051 // Declare the pthread_mutex data structures
52 pthread_mutex_t* mutex =
53 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
54 pthread_mutexattr_t attr;
Reid Spencerf4049812005-07-12 15:37:43 +000055
David Blaikiefdcd6692012-01-15 01:09:13 +000056 // Initialize the mutex attributes
57 int errorcode = pthread_mutexattr_init(&attr);
Duncan Sandsae22c602012-02-05 14:20:11 +000058 assert(errorcode == 0); (void)errorcode;
Reid Spencerf4049812005-07-12 15:37:43 +000059
David Blaikiefdcd6692012-01-15 01:09:13 +000060 // Initialize the mutex as a recursive mutex, if requested, or normal
61 // otherwise.
62 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
63 errorcode = pthread_mutexattr_settype(&attr, kind);
64 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000065
David Blaikiefdcd6692012-01-15 01:09:13 +000066 // Initialize the mutex
67 errorcode = pthread_mutex_init(mutex, &attr);
68 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000069
David Blaikiefdcd6692012-01-15 01:09:13 +000070 // Destroy the attributes
71 errorcode = pthread_mutexattr_destroy(&attr);
72 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000073
David Blaikiefdcd6692012-01-15 01:09:13 +000074 // Assign the data member
75 data_ = mutex;
Reid Spencerf4049812005-07-12 15:37:43 +000076}
77
78// Destruct a Mutex
Owen Anderson68f65982009-06-18 17:53:17 +000079MutexImpl::~MutexImpl()
Reid Spencerf4049812005-07-12 15:37:43 +000080{
David Blaikiefdcd6692012-01-15 01:09:13 +000081 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000082 assert(mutex != nullptr);
David Blaikiefdcd6692012-01-15 01:09:13 +000083 pthread_mutex_destroy(mutex);
84 free(mutex);
Reid Spencerf4049812005-07-12 15:37:43 +000085}
86
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000087bool
Owen Anderson68f65982009-06-18 17:53:17 +000088MutexImpl::acquire()
Reid Spencerf4049812005-07-12 15:37:43 +000089{
David Blaikiefdcd6692012-01-15 01:09:13 +000090 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000091 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +000092
David Blaikiefdcd6692012-01-15 01:09:13 +000093 int errorcode = pthread_mutex_lock(mutex);
94 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +000095}
96
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000097bool
Owen Anderson68f65982009-06-18 17:53:17 +000098MutexImpl::release()
Reid Spencerf4049812005-07-12 15:37:43 +000099{
David Blaikiefdcd6692012-01-15 01:09:13 +0000100 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000101 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +0000102
David Blaikiefdcd6692012-01-15 01:09:13 +0000103 int errorcode = pthread_mutex_unlock(mutex);
104 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000105}
106
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000107bool
Owen Anderson68f65982009-06-18 17:53:17 +0000108MutexImpl::tryacquire()
Reid Spencerf4049812005-07-12 15:37:43 +0000109{
David Blaikiefdcd6692012-01-15 01:09:13 +0000110 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000111 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +0000112
David Blaikiefdcd6692012-01-15 01:09:13 +0000113 int errorcode = pthread_mutex_trylock(mutex);
114 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000115}
116
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000117} // end namespace llvm
Jeff Cohen3800a282005-07-13 02:15:18 +0000118
Reid Spencerf4049812005-07-12 15:37:43 +0000119#elif defined(LLVM_ON_UNIX)
120#include "Unix/Mutex.inc"
121#elif defined( LLVM_ON_WIN32)
Michael J. Spencer447762d2010-11-29 18:16:10 +0000122#include "Windows/Mutex.inc"
Reid Spencerf4049812005-07-12 15:37:43 +0000123#else
Daniel Dunbar037fc932011-10-11 20:02:52 +0000124#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
Reid Spencerf4049812005-07-12 15:37:43 +0000125#endif
Reid Spencerf85fabeb2005-08-24 10:07:20 +0000126#endif