blob: b408973bbad1557b922c2a5bc97f081f1fa2e519 [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
Reid Spencer0a262ba2005-08-24 10:07:20 +000022#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
23// 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
43
Reid Spencerb2164e52005-07-12 15:37:43 +000044// This variable is useful for situations where the pthread library has been
Jeff Cohen00b168892005-07-27 06:12:32 +000045// compiled with weak linkage for its interface symbols. This allows the
Reid Spencerb2164e52005-07-12 15:37:43 +000046// threading support to be turned off by simply not linking against -lpthread.
Jeff Cohen00b168892005-07-27 06:12:32 +000047// In that situation, the value of pthread_mutex_init will be 0 and
Reid Spencerb2164e52005-07-12 15:37:43 +000048// consequently pthread_enabled will be false. In such situations, all the
49// pthread operations become no-ops and the functions all return false. If
50// pthread_mutex_init does have an address, then mutex support is enabled.
51// Note: all LLVM tools will link against -lpthread if its available since it
52// is configured into the LIBS variable.
53// Note: this line of code generates a warning if pthread_mutex_init is not
Misha Brukman15d89cb2005-08-02 16:04:59 +000054// declared with weak linkage. It's safe to ignore the warning.
Chris Lattnera5370172006-08-28 00:42:29 +000055static const bool pthread_enabled = true;
Reid Spencerb2164e52005-07-12 15:37:43 +000056
57// Construct a Mutex using pthread calls
Owen Andersonb849a4d2009-06-18 17:53:17 +000058MutexImpl::MutexImpl( bool recursive)
Reid Spencerb2164e52005-07-12 15:37:43 +000059 : data_(0)
60{
61 if (pthread_enabled)
62 {
63 // Declare the pthread_mutex data structures
Jeff Cohen00b168892005-07-27 06:12:32 +000064 pthread_mutex_t* mutex =
Reid Spencerb2164e52005-07-12 15:37:43 +000065 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
66 pthread_mutexattr_t attr;
67
68 // Initialize the mutex attributes
69 int errorcode = pthread_mutexattr_init(&attr);
70 assert(errorcode == 0);
71
72 // Initialize the mutex as a recursive mutex, if requested, or normal
73 // otherwise.
74 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
75 errorcode = pthread_mutexattr_settype(&attr, kind);
76 assert(errorcode == 0);
77
Matthijs Kooijmanf5122812008-06-26 10:36:58 +000078#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__)
Reid Spencerb2164e52005-07-12 15:37:43 +000079 // Make it a process local mutex
80 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
Argyrios Kyrtzidisc788f442011-02-18 00:47:07 +000081 assert(errorcode == 0);
Reid Spencer24998412005-07-13 03:02:06 +000082#endif
Reid Spencerb2164e52005-07-12 15:37:43 +000083
84 // Initialize the mutex
85 errorcode = pthread_mutex_init(mutex, &attr);
86 assert(errorcode == 0);
87
88 // Destroy the attributes
89 errorcode = pthread_mutexattr_destroy(&attr);
90 assert(errorcode == 0);
91
92 // Assign the data member
93 data_ = mutex;
94 }
95}
96
97// Destruct a Mutex
Owen Andersonb849a4d2009-06-18 17:53:17 +000098MutexImpl::~MutexImpl()
Reid Spencerb2164e52005-07-12 15:37:43 +000099{
100 if (pthread_enabled)
101 {
Dan Gohman933e51c2008-06-21 20:17:03 +0000102 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Reid Spencerb2164e52005-07-12 15:37:43 +0000103 assert(mutex != 0);
Reid Spencer2b86b0e2006-11-02 07:59:59 +0000104 pthread_mutex_destroy(mutex);
Nuno Lopes274c6a62008-11-06 16:21:49 +0000105 free(mutex);
Reid Spencerb2164e52005-07-12 15:37:43 +0000106 }
107}
108
Jeff Cohen00b168892005-07-27 06:12:32 +0000109bool
Owen Andersonb849a4d2009-06-18 17:53:17 +0000110MutexImpl::acquire()
Reid Spencerb2164e52005-07-12 15:37:43 +0000111{
Jeff Cohen00b168892005-07-27 06:12:32 +0000112 if (pthread_enabled)
Reid Spencerb2164e52005-07-12 15:37:43 +0000113 {
Dan Gohman933e51c2008-06-21 20:17:03 +0000114 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Reid Spencerb2164e52005-07-12 15:37:43 +0000115 assert(mutex != 0);
116
117 int errorcode = pthread_mutex_lock(mutex);
118 return errorcode == 0;
Duncan Sands740eb532009-09-06 10:53:22 +0000119 } else return false;
Reid Spencerb2164e52005-07-12 15:37:43 +0000120}
121
Jeff Cohen00b168892005-07-27 06:12:32 +0000122bool
Owen Andersonb849a4d2009-06-18 17:53:17 +0000123MutexImpl::release()
Reid Spencerb2164e52005-07-12 15:37:43 +0000124{
125 if (pthread_enabled)
126 {
Dan Gohman933e51c2008-06-21 20:17:03 +0000127 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Reid Spencerb2164e52005-07-12 15:37:43 +0000128 assert(mutex != 0);
129
130 int errorcode = pthread_mutex_unlock(mutex);
131 return errorcode == 0;
Duncan Sands740eb532009-09-06 10:53:22 +0000132 } else return false;
Reid Spencerb2164e52005-07-12 15:37:43 +0000133}
134
Jeff Cohen00b168892005-07-27 06:12:32 +0000135bool
Owen Andersonb849a4d2009-06-18 17:53:17 +0000136MutexImpl::tryacquire()
Reid Spencerb2164e52005-07-12 15:37:43 +0000137{
138 if (pthread_enabled)
139 {
Dan Gohman933e51c2008-06-21 20:17:03 +0000140 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Reid Spencerb2164e52005-07-12 15:37:43 +0000141 assert(mutex != 0);
142
143 int errorcode = pthread_mutex_trylock(mutex);
144 return errorcode == 0;
Duncan Sands740eb532009-09-06 10:53:22 +0000145 } else return false;
Reid Spencerb2164e52005-07-12 15:37:43 +0000146}
147
148}
Jeff Cohen6d235222005-07-13 02:15:18 +0000149
Reid Spencerb2164e52005-07-12 15:37:43 +0000150#elif defined(LLVM_ON_UNIX)
151#include "Unix/Mutex.inc"
152#elif defined( LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000153#include "Windows/Mutex.inc"
Reid Spencerb2164e52005-07-12 15:37:43 +0000154#else
155#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp
156#endif
Reid Spencer0a262ba2005-08-24 10:07:20 +0000157#endif