1 //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the llvm::sys::Mutex class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Config/config.h"
15 #include "llvm/System/Mutex.h"
16 #include "llvm/System/IncludeFile.h"
18 //===----------------------------------------------------------------------===//
19 //=== WARNING: Implementation here must contain only TRULY operating system
20 //=== independent code.
21 //===----------------------------------------------------------------------===//
23 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
24 // Define all methods as no-ops if threading is explicitly disabled
27 Mutex::Mutex( bool recursive
) { }
29 bool Mutex::acquire() { return true; }
30 bool Mutex::release() { return true; }
31 bool Mutex::tryacquire() { return true; }
35 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
45 // This variable is useful for situations where the pthread library has been
46 // compiled with weak linkage for its interface symbols. This allows the
47 // threading support to be turned off by simply not linking against -lpthread.
48 // In that situation, the value of pthread_mutex_init will be 0 and
49 // consequently pthread_enabled will be false. In such situations, all the
50 // pthread operations become no-ops and the functions all return false. If
51 // pthread_mutex_init does have an address, then mutex support is enabled.
52 // Note: all LLVM tools will link against -lpthread if its available since it
53 // is configured into the LIBS variable.
54 // Note: this line of code generates a warning if pthread_mutex_init is not
55 // declared with weak linkage. It's safe to ignore the warning.
56 static const bool pthread_enabled
= true;
58 // Construct a Mutex using pthread calls
59 Mutex::Mutex( bool recursive
)
64 // Declare the pthread_mutex data structures
65 pthread_mutex_t
* mutex
=
66 static_cast<pthread_mutex_t
*>(malloc(sizeof(pthread_mutex_t
)));
67 pthread_mutexattr_t attr
;
69 // Initialize the mutex attributes
70 int errorcode
= pthread_mutexattr_init(&attr
);
71 assert(errorcode
== 0);
73 // Initialize the mutex as a recursive mutex, if requested, or normal
75 int kind
= ( recursive
? PTHREAD_MUTEX_RECURSIVE
: PTHREAD_MUTEX_NORMAL
);
76 errorcode
= pthread_mutexattr_settype(&attr
, kind
);
77 assert(errorcode
== 0);
79 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
80 // Make it a process local mutex
81 errorcode
= pthread_mutexattr_setpshared(&attr
, PTHREAD_PROCESS_PRIVATE
);
84 // Initialize the mutex
85 errorcode
= pthread_mutex_init(mutex
, &attr
);
86 assert(errorcode
== 0);
88 // Destroy the attributes
89 errorcode
= pthread_mutexattr_destroy(&attr
);
90 assert(errorcode
== 0);
92 // Assign the data member
102 pthread_mutex_t
* mutex
= reinterpret_cast<pthread_mutex_t
*>(data_
);
104 pthread_mutex_destroy(mutex
);
114 pthread_mutex_t
* mutex
= reinterpret_cast<pthread_mutex_t
*>(data_
);
117 int errorcode
= pthread_mutex_lock(mutex
);
118 return errorcode
== 0;
128 pthread_mutex_t
* mutex
= reinterpret_cast<pthread_mutex_t
*>(data_
);
131 int errorcode
= pthread_mutex_unlock(mutex
);
132 return errorcode
== 0;
142 pthread_mutex_t
* mutex
= reinterpret_cast<pthread_mutex_t
*>(data_
);
145 int errorcode
= pthread_mutex_trylock(mutex
);
146 return errorcode
== 0;
153 #elif defined(LLVM_ON_UNIX)
154 #include "Unix/Mutex.inc"
155 #elif defined( LLVM_ON_WIN32)
156 #include "Win32/Mutex.inc"
158 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp
162 DEFINING_FILE_FOR(SystemMutex
)