blob: 8ccd6e52c4d5bbdb7ec2ec55630d8a71274890bc [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the llvm::sys::Mutex class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Config/config.h"
15#include "llvm/System/Mutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016
17//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only TRULY operating system
19//=== independent code.
20//===----------------------------------------------------------------------===//
21
22#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
23// Define all methods as no-ops if threading is explicitly disabled
24namespace llvm {
25using namespace sys;
Owen Anderson052b3062009-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; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031}
32#else
33
34#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
35
36#include <cassert>
37#include <pthread.h>
38#include <stdlib.h>
39
40namespace llvm {
41using namespace sys;
42
43
44// This variable is useful for situations where the pthread library has been
45// compiled with weak linkage for its interface symbols. This allows the
46// threading support to be turned off by simply not linking against -lpthread.
47// In that situation, the value of pthread_mutex_init will be 0 and
48// 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
54// declared with weak linkage. It's safe to ignore the warning.
55static const bool pthread_enabled = true;
56
57// Construct a Mutex using pthread calls
Owen Anderson052b3062009-06-18 17:53:17 +000058MutexImpl::MutexImpl( bool recursive)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 : data_(0)
60{
61 if (pthread_enabled)
62 {
63 // Declare the pthread_mutex data structures
64 pthread_mutex_t* mutex =
65 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 Kooijman331217d2008-06-26 10:36:58 +000078#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 // Make it a process local mutex
80 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
81#endif
82
83 // Initialize the mutex
84 errorcode = pthread_mutex_init(mutex, &attr);
85 assert(errorcode == 0);
86
87 // Destroy the attributes
88 errorcode = pthread_mutexattr_destroy(&attr);
89 assert(errorcode == 0);
90
91 // Assign the data member
92 data_ = mutex;
93 }
94}
95
96// Destruct a Mutex
Owen Anderson052b3062009-06-18 17:53:17 +000097MutexImpl::~MutexImpl()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098{
99 if (pthread_enabled)
100 {
Dan Gohman1dcaeef2008-06-21 20:17:03 +0000101 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 assert(mutex != 0);
103 pthread_mutex_destroy(mutex);
Nuno Lopes0eeed742008-11-06 16:21:49 +0000104 free(mutex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 }
106}
107
108bool
Owen Anderson052b3062009-06-18 17:53:17 +0000109MutexImpl::acquire()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110{
111 if (pthread_enabled)
112 {
Dan Gohman1dcaeef2008-06-21 20:17:03 +0000113 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 assert(mutex != 0);
115
116 int errorcode = pthread_mutex_lock(mutex);
117 return errorcode == 0;
Duncan Sands3872b7e2009-09-06 10:53:22 +0000118 } else return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119}
120
121bool
Owen Anderson052b3062009-06-18 17:53:17 +0000122MutexImpl::release()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123{
124 if (pthread_enabled)
125 {
Dan Gohman1dcaeef2008-06-21 20:17:03 +0000126 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 assert(mutex != 0);
128
129 int errorcode = pthread_mutex_unlock(mutex);
130 return errorcode == 0;
Duncan Sands3872b7e2009-09-06 10:53:22 +0000131 } else return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132}
133
134bool
Owen Anderson052b3062009-06-18 17:53:17 +0000135MutexImpl::tryacquire()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136{
137 if (pthread_enabled)
138 {
Dan Gohman1dcaeef2008-06-21 20:17:03 +0000139 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 assert(mutex != 0);
141
142 int errorcode = pthread_mutex_trylock(mutex);
143 return errorcode == 0;
Duncan Sands3872b7e2009-09-06 10:53:22 +0000144 } else return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145}
146
147}
148
149#elif defined(LLVM_ON_UNIX)
150#include "Unix/Mutex.inc"
151#elif defined( LLVM_ON_WIN32)
152#include "Win32/Mutex.inc"
153#else
154#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp
155#endif
156#endif
157