blob: 4e4a026b2f0d970acec9aeba48eefaf83c9ae68a [file] [log] [blame]
Reid Spencerb2164e52005-07-12 15:37:43 +00001//===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Spencerb2164e52005-07-12 15:37:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the llvm::sys::Mutex class.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencerb2164e52005-07-12 15:37:43 +000014#include "llvm/Config/config.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000015#include "llvm/Support/Mutex.h"
Reid Spencerb2164e52005-07-12 15:37:43 +000016
Reid Spencerb2164e52005-07-12 15:37:43 +000017//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only TRULY operating system
19//=== independent code.
20//===----------------------------------------------------------------------===//
21
Dylan Noblesmith08b73a32011-11-28 00:48:58 +000022#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
Reid Spencer0a262ba2005-08-24 10:07:20 +000023// Define all methods as no-ops if threading is explicitly disabled
Jeff Cohen6d235222005-07-13 02:15:18 +000024namespace llvm {
25using namespace sys;
Owen Andersonb849a4d2009-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 Spencer0a262ba2005-08-24 10:07:20 +000031}
32#else
33
34#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
Jeff Cohen6d235222005-07-13 02:15:18 +000035
Reid Spencerb2164e52005-07-12 15:37:43 +000036#include <cassert>
37#include <pthread.h>
38#include <stdlib.h>
39
Reid Spencer0a262ba2005-08-24 10:07:20 +000040namespace llvm {
41using namespace sys;
42
Reid Spencerb2164e52005-07-12 15:37:43 +000043// Construct a Mutex using pthread calls
Owen Andersonb849a4d2009-06-18 17:53:17 +000044MutexImpl::MutexImpl( bool recursive)
Reid Spencerb2164e52005-07-12 15:37:43 +000045 : data_(0)
46{
David Blaikie49c0a9a2012-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 Spencerb2164e52005-07-12 15:37:43 +000051
David Blaikie49c0a9a2012-01-15 01:09:13 +000052 // Initialize the mutex attributes
53 int errorcode = pthread_mutexattr_init(&attr);
Duncan Sands5b8a1db2012-02-05 14:20:11 +000054 assert(errorcode == 0); (void)errorcode;
Reid Spencerb2164e52005-07-12 15:37:43 +000055
David Blaikie49c0a9a2012-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 Spencerb2164e52005-07-12 15:37:43 +000061
Eric Christopherb0f67592012-08-06 20:52:18 +000062#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && \
63 !defined(__DragonFly__) && !defined(__Bitrig__)
David Blaikie49c0a9a2012-01-15 01:09:13 +000064 // Make it a process local mutex
65 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
66 assert(errorcode == 0);
Reid Spencer24998412005-07-13 03:02:06 +000067#endif
Reid Spencerb2164e52005-07-12 15:37:43 +000068
David Blaikie49c0a9a2012-01-15 01:09:13 +000069 // Initialize the mutex
70 errorcode = pthread_mutex_init(mutex, &attr);
71 assert(errorcode == 0);
Reid Spencerb2164e52005-07-12 15:37:43 +000072
David Blaikie49c0a9a2012-01-15 01:09:13 +000073 // Destroy the attributes
74 errorcode = pthread_mutexattr_destroy(&attr);
75 assert(errorcode == 0);
Reid Spencerb2164e52005-07-12 15:37:43 +000076
David Blaikie49c0a9a2012-01-15 01:09:13 +000077 // Assign the data member
78 data_ = mutex;
Reid Spencerb2164e52005-07-12 15:37:43 +000079}
80
81// Destruct a Mutex
Owen Andersonb849a4d2009-06-18 17:53:17 +000082MutexImpl::~MutexImpl()
Reid Spencerb2164e52005-07-12 15:37:43 +000083{
David Blaikie49c0a9a2012-01-15 01:09:13 +000084 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
85 assert(mutex != 0);
86 pthread_mutex_destroy(mutex);
87 free(mutex);
Reid Spencerb2164e52005-07-12 15:37:43 +000088}
89
Jeff Cohen00b168892005-07-27 06:12:32 +000090bool
Owen Andersonb849a4d2009-06-18 17:53:17 +000091MutexImpl::acquire()
Reid Spencerb2164e52005-07-12 15:37:43 +000092{
David Blaikie49c0a9a2012-01-15 01:09:13 +000093 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
94 assert(mutex != 0);
Reid Spencerb2164e52005-07-12 15:37:43 +000095
David Blaikie49c0a9a2012-01-15 01:09:13 +000096 int errorcode = pthread_mutex_lock(mutex);
97 return errorcode == 0;
Reid Spencerb2164e52005-07-12 15:37:43 +000098}
99
Jeff Cohen00b168892005-07-27 06:12:32 +0000100bool
Owen Andersonb849a4d2009-06-18 17:53:17 +0000101MutexImpl::release()
Reid Spencerb2164e52005-07-12 15:37:43 +0000102{
David Blaikie49c0a9a2012-01-15 01:09:13 +0000103 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
104 assert(mutex != 0);
Reid Spencerb2164e52005-07-12 15:37:43 +0000105
David Blaikie49c0a9a2012-01-15 01:09:13 +0000106 int errorcode = pthread_mutex_unlock(mutex);
107 return errorcode == 0;
Reid Spencerb2164e52005-07-12 15:37:43 +0000108}
109
Jeff Cohen00b168892005-07-27 06:12:32 +0000110bool
Owen Andersonb849a4d2009-06-18 17:53:17 +0000111MutexImpl::tryacquire()
Reid Spencerb2164e52005-07-12 15:37:43 +0000112{
David Blaikie49c0a9a2012-01-15 01:09:13 +0000113 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
114 assert(mutex != 0);
Reid Spencerb2164e52005-07-12 15:37:43 +0000115
David Blaikie49c0a9a2012-01-15 01:09:13 +0000116 int errorcode = pthread_mutex_trylock(mutex);
117 return errorcode == 0;
Reid Spencerb2164e52005-07-12 15:37:43 +0000118}
119
120}
Jeff Cohen6d235222005-07-13 02:15:18 +0000121
Reid Spencerb2164e52005-07-12 15:37:43 +0000122#elif defined(LLVM_ON_UNIX)
123#include "Unix/Mutex.inc"
124#elif defined( LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000125#include "Windows/Mutex.inc"
Reid Spencerb2164e52005-07-12 15:37:43 +0000126#else
Daniel Dunbar2c607b62011-10-11 20:02:52 +0000127#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
Reid Spencerb2164e52005-07-12 15:37:43 +0000128#endif
Reid Spencer0a262ba2005-08-24 10:07:20 +0000129#endif