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/System/Mutex.h"
15 #include "llvm/Config/config.h"
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only TRULY operating system
19 //=== independent code.
20 //===----------------------------------------------------------------------===//
22 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
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.
42 static const bool pthread_enabled = static_cast<bool>(pthread_mutex_init);
44 // Construct a Mutex using pthread calls
45 Mutex::Mutex( bool recursive)
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;
55 // Initialize the mutex attributes
56 int errorcode = pthread_mutexattr_init(&attr);
57 assert(errorcode == 0);
59 // Initialize the mutex as a recursive mutex, if requested, or normal
61 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
62 errorcode = pthread_mutexattr_settype(&attr, kind);
63 assert(errorcode == 0);
65 // Make it a process local mutex
66 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
68 // Initialize the mutex
69 errorcode = pthread_mutex_init(mutex, &attr);
70 assert(errorcode == 0);
72 // Destroy the attributes
73 errorcode = pthread_mutexattr_destroy(&attr);
74 assert(errorcode == 0);
76 // Assign the data member
86 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
88 int errorcode = pthread_mutex_destroy(mutex);
98 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
101 int errorcode = pthread_mutex_lock(mutex);
102 return errorcode == 0;
112 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
115 int errorcode = pthread_mutex_unlock(mutex);
116 return errorcode == 0;
126 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
129 int errorcode = pthread_mutex_trylock(mutex);
130 return errorcode == 0;
137 #elif defined(LLVM_ON_UNIX)
138 #include "Unix/Mutex.inc"
139 #elif defined( LLVM_ON_WIN32)
140 #include "Win32/Mutex.inc"
142 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp