blob: c8d3844d0c9618a19133836c06aabac831f42e35 [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 {
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; }
Reid Spencerf85fabeb2005-08-24 10:07:20 +000031}
32#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>
38#include <stdlib.h>
39
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 =
49 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
50 pthread_mutexattr_t attr;
Reid Spencerf4049812005-07-12 15:37:43 +000051
David Blaikiefdcd6692012-01-15 01:09:13 +000052 // Initialize the mutex attributes
53 int errorcode = pthread_mutexattr_init(&attr);
Duncan Sandsae22c602012-02-05 14:20:11 +000054 assert(errorcode == 0); (void)errorcode;
Reid Spencerf4049812005-07-12 15:37:43 +000055
David Blaikiefdcd6692012-01-15 01:09:13 +000056 // Initialize the mutex as a recursive mutex, if requested, or normal
57 // otherwise.
58 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
59 errorcode = pthread_mutexattr_settype(&attr, kind);
60 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000061
David Blaikiefdcd6692012-01-15 01:09:13 +000062 // Initialize the mutex
63 errorcode = pthread_mutex_init(mutex, &attr);
64 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000065
David Blaikiefdcd6692012-01-15 01:09:13 +000066 // Destroy the attributes
67 errorcode = pthread_mutexattr_destroy(&attr);
68 assert(errorcode == 0);
Reid Spencerf4049812005-07-12 15:37:43 +000069
David Blaikiefdcd6692012-01-15 01:09:13 +000070 // Assign the data member
71 data_ = mutex;
Reid Spencerf4049812005-07-12 15:37:43 +000072}
73
74// Destruct a Mutex
Owen Anderson68f65982009-06-18 17:53:17 +000075MutexImpl::~MutexImpl()
Reid Spencerf4049812005-07-12 15:37:43 +000076{
David Blaikiefdcd6692012-01-15 01:09:13 +000077 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000078 assert(mutex != nullptr);
David Blaikiefdcd6692012-01-15 01:09:13 +000079 pthread_mutex_destroy(mutex);
80 free(mutex);
Reid Spencerf4049812005-07-12 15:37:43 +000081}
82
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000083bool
Owen Anderson68f65982009-06-18 17:53:17 +000084MutexImpl::acquire()
Reid Spencerf4049812005-07-12 15:37:43 +000085{
David Blaikiefdcd6692012-01-15 01:09:13 +000086 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000087 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +000088
David Blaikiefdcd6692012-01-15 01:09:13 +000089 int errorcode = pthread_mutex_lock(mutex);
90 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +000091}
92
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000093bool
Owen Anderson68f65982009-06-18 17:53:17 +000094MutexImpl::release()
Reid Spencerf4049812005-07-12 15:37:43 +000095{
David Blaikiefdcd6692012-01-15 01:09:13 +000096 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000097 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +000098
David Blaikiefdcd6692012-01-15 01:09:13 +000099 int errorcode = pthread_mutex_unlock(mutex);
100 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000101}
102
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000103bool
Owen Anderson68f65982009-06-18 17:53:17 +0000104MutexImpl::tryacquire()
Reid Spencerf4049812005-07-12 15:37:43 +0000105{
David Blaikiefdcd6692012-01-15 01:09:13 +0000106 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000107 assert(mutex != nullptr);
Reid Spencerf4049812005-07-12 15:37:43 +0000108
David Blaikiefdcd6692012-01-15 01:09:13 +0000109 int errorcode = pthread_mutex_trylock(mutex);
110 return errorcode == 0;
Reid Spencerf4049812005-07-12 15:37:43 +0000111}
112
113}
Jeff Cohen3800a282005-07-13 02:15:18 +0000114
Reid Spencerf4049812005-07-12 15:37:43 +0000115#elif defined(LLVM_ON_UNIX)
116#include "Unix/Mutex.inc"
117#elif defined( LLVM_ON_WIN32)
Michael J. Spencer447762d2010-11-29 18:16:10 +0000118#include "Windows/Mutex.inc"
Reid Spencerf4049812005-07-12 15:37:43 +0000119#else
Daniel Dunbar037fc932011-10-11 20:02:52 +0000120#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
Reid Spencerf4049812005-07-12 15:37:43 +0000121#endif
Reid Spencerf85fabeb2005-08-24 10:07:20 +0000122#endif