blob: 799beee6806657f3b11356f24c7d2e2882e14f1c [file] [log] [blame]
Reid Spencerf4049812005-07-12 15:37:43 +00001//===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the llvm::sys::Mutex class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/Mutex.h"
15#include "llvm/Config/config.h"
16
Reid Spencerf4049812005-07-12 15:37:43 +000017//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only TRULY operating system
19//=== independent code.
20//===----------------------------------------------------------------------===//
21
22#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
Jeff Cohen3800a282005-07-13 02:15:18 +000023
24namespace llvm {
25using namespace sys;
26
Reid Spencerf4049812005-07-12 15:37:43 +000027#include <cassert>
28#include <pthread.h>
29#include <stdlib.h>
30
31// This variable is useful for situations where the pthread library has been
32// compiled with weak linkage for its interface symbols. This allows the
33// threading support to be turned off by simply not linking against -lpthread.
34// In that situation, the value of pthread_mutex_init will be 0 and
35// consequently pthread_enabled will be false. In such situations, all the
36// pthread operations become no-ops and the functions all return false. If
37// pthread_mutex_init does have an address, then mutex support is enabled.
38// Note: all LLVM tools will link against -lpthread if its available since it
39// is configured into the LIBS variable.
40// Note: this line of code generates a warning if pthread_mutex_init is not
41// declared with weak linkage. Its safe to ignore the warning.
42static const bool pthread_enabled = static_cast<bool>(pthread_mutex_init);
43
44// Construct a Mutex using pthread calls
45Mutex::Mutex( bool recursive)
46 : data_(0)
47{
48 if (pthread_enabled)
49 {
50 // Declare the pthread_mutex data structures
51 pthread_mutex_t* mutex =
52 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
53 pthread_mutexattr_t attr;
54
55 // Initialize the mutex attributes
56 int errorcode = pthread_mutexattr_init(&attr);
57 assert(errorcode == 0);
58
59 // Initialize the mutex as a recursive mutex, if requested, or normal
60 // otherwise.
61 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
62 errorcode = pthread_mutexattr_settype(&attr, kind);
63 assert(errorcode == 0);
64
Reid Spencer86df93a2005-07-13 03:02:06 +000065#ifndef __FreeBSD__
Reid Spencerf4049812005-07-12 15:37:43 +000066 // Make it a process local mutex
67 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
Reid Spencer86df93a2005-07-13 03:02:06 +000068#endif
Reid Spencerf4049812005-07-12 15:37:43 +000069
70 // Initialize the mutex
71 errorcode = pthread_mutex_init(mutex, &attr);
72 assert(errorcode == 0);
73
74 // Destroy the attributes
75 errorcode = pthread_mutexattr_destroy(&attr);
76 assert(errorcode == 0);
77
78 // Assign the data member
79 data_ = mutex;
80 }
81}
82
83// Destruct a Mutex
84Mutex::~Mutex()
85{
86 if (pthread_enabled)
87 {
88 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
89 assert(mutex != 0);
90 int errorcode = pthread_mutex_destroy(mutex);
91 assert(mutex != 0);
92 }
93}
94
95bool
96Mutex::acquire()
97{
98 if (pthread_enabled)
99 {
100 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
101 assert(mutex != 0);
102
103 int errorcode = pthread_mutex_lock(mutex);
104 return errorcode == 0;
105 }
106 return false;
107}
108
109bool
110Mutex::release()
111{
112 if (pthread_enabled)
113 {
114 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
115 assert(mutex != 0);
116
117 int errorcode = pthread_mutex_unlock(mutex);
118 return errorcode == 0;
119 }
120 return false;
121}
122
123bool
124Mutex::tryacquire()
125{
126 if (pthread_enabled)
127 {
128 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
129 assert(mutex != 0);
130
131 int errorcode = pthread_mutex_trylock(mutex);
132 return errorcode == 0;
133 }
134 return false;
135}
136
137}
Jeff Cohen3800a282005-07-13 02:15:18 +0000138
Reid Spencerf4049812005-07-12 15:37:43 +0000139#elif defined(LLVM_ON_UNIX)
140#include "Unix/Mutex.inc"
141#elif defined( LLVM_ON_WIN32)
142#include "Win32/Mutex.inc"
143#else
144#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp
145#endif