blob: da5baab4be468bc73c1cce1d70b6f1b1aaeed71c [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
Matthijs Kooijmanf5122812008-06-26 10:36:58 +000062#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__)
David Blaikie49c0a9a2012-01-15 01:09:13 +000063 // Make it a process local mutex
64 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
65 assert(errorcode == 0);
Reid Spencer24998412005-07-13 03:02:06 +000066#endif
Reid Spencerb2164e52005-07-12 15:37:43 +000067
David Blaikie49c0a9a2012-01-15 01:09:13 +000068 // Initialize the mutex
69 errorcode = pthread_mutex_init(mutex, &attr);
70 assert(errorcode == 0);
Reid Spencerb2164e52005-07-12 15:37:43 +000071
David Blaikie49c0a9a2012-01-15 01:09:13 +000072 // Destroy the attributes
73 errorcode = pthread_mutexattr_destroy(&attr);
74 assert(errorcode == 0);
Reid Spencerb2164e52005-07-12 15:37:43 +000075
David Blaikie49c0a9a2012-01-15 01:09:13 +000076 // Assign the data member
77 data_ = mutex;
Reid Spencerb2164e52005-07-12 15:37:43 +000078}
79
80// Destruct a Mutex
Owen Andersonb849a4d2009-06-18 17:53:17 +000081MutexImpl::~MutexImpl()
Reid Spencerb2164e52005-07-12 15:37:43 +000082{
David Blaikie49c0a9a2012-01-15 01:09:13 +000083 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
84 assert(mutex != 0);
85 pthread_mutex_destroy(mutex);
86 free(mutex);
Reid Spencerb2164e52005-07-12 15:37:43 +000087}
88
Jeff Cohen00b168892005-07-27 06:12:32 +000089bool
Owen Andersonb849a4d2009-06-18 17:53:17 +000090MutexImpl::acquire()
Reid Spencerb2164e52005-07-12 15:37:43 +000091{
David Blaikie49c0a9a2012-01-15 01:09:13 +000092 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
93 assert(mutex != 0);
Reid Spencerb2164e52005-07-12 15:37:43 +000094
David Blaikie49c0a9a2012-01-15 01:09:13 +000095 int errorcode = pthread_mutex_lock(mutex);
96 return errorcode == 0;
Reid Spencerb2164e52005-07-12 15:37:43 +000097}
98
Jeff Cohen00b168892005-07-27 06:12:32 +000099bool
Owen Andersonb849a4d2009-06-18 17:53:17 +0000100MutexImpl::release()
Reid Spencerb2164e52005-07-12 15:37:43 +0000101{
David Blaikie49c0a9a2012-01-15 01:09:13 +0000102 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
103 assert(mutex != 0);
Reid Spencerb2164e52005-07-12 15:37:43 +0000104
David Blaikie49c0a9a2012-01-15 01:09:13 +0000105 int errorcode = pthread_mutex_unlock(mutex);
106 return errorcode == 0;
Reid Spencerb2164e52005-07-12 15:37:43 +0000107}
108
Jeff Cohen00b168892005-07-27 06:12:32 +0000109bool
Owen Andersonb849a4d2009-06-18 17:53:17 +0000110MutexImpl::tryacquire()
Reid Spencerb2164e52005-07-12 15:37:43 +0000111{
David Blaikie49c0a9a2012-01-15 01:09:13 +0000112 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
113 assert(mutex != 0);
Reid Spencerb2164e52005-07-12 15:37:43 +0000114
David Blaikie49c0a9a2012-01-15 01:09:13 +0000115 int errorcode = pthread_mutex_trylock(mutex);
116 return errorcode == 0;
Reid Spencerb2164e52005-07-12 15:37:43 +0000117}
118
119}
Jeff Cohen6d235222005-07-13 02:15:18 +0000120
Reid Spencerb2164e52005-07-12 15:37:43 +0000121#elif defined(LLVM_ON_UNIX)
122#include "Unix/Mutex.inc"
123#elif defined( LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000124#include "Windows/Mutex.inc"
Reid Spencerb2164e52005-07-12 15:37:43 +0000125#else
Daniel Dunbar2c607b62011-10-11 20:02:52 +0000126#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
Reid Spencerb2164e52005-07-12 15:37:43 +0000127#endif
Reid Spencer0a262ba2005-08-24 10:07:20 +0000128#endif