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"
20 //===----------------------------------------------------------------------===//
21 //=== WARNING: Implementation here must contain only TRULY operating system
22 //=== independent code.
23 //===----------------------------------------------------------------------===//
25 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
30 // This variable is useful for situations where the pthread library has been
31 // compiled with weak linkage for its interface symbols. This allows the
32 // threading support to be turned off by simply not linking against -lpthread.
33 // In that situation, the value of pthread_mutex_init will be 0 and
34 // consequently pthread_enabled will be false. In such situations, all the
35 // pthread operations become no-ops and the functions all return false. If
36 // pthread_mutex_init does have an address, then mutex support is enabled.
37 // Note: all LLVM tools will link against -lpthread if its available since it
38 // is configured into the LIBS variable.
39 // Note: this line of code generates a warning if pthread_mutex_init is not
40 // declared with weak linkage. Its safe to ignore the warning.
41 static const bool pthread_enabled = static_cast<bool>(pthread_mutex_init);
43 // Construct a Mutex using pthread calls
44 Mutex::Mutex( bool recursive)
49 // Declare the pthread_mutex data structures
50 pthread_mutex_t* mutex =
51 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
52 pthread_mutexattr_t attr;
54 // Initialize the mutex attributes
55 int errorcode = pthread_mutexattr_init(&attr);
56 assert(errorcode == 0);
58 // Initialize the mutex as a recursive mutex, if requested, or normal
60 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
61 errorcode = pthread_mutexattr_settype(&attr, kind);
62 assert(errorcode == 0);
64 // Make it a process local mutex
65 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
67 // Initialize the mutex
68 errorcode = pthread_mutex_init(mutex, &attr);
69 assert(errorcode == 0);
71 // Destroy the attributes
72 errorcode = pthread_mutexattr_destroy(&attr);
73 assert(errorcode == 0);
75 // Assign the data member
85 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
87 int errorcode = pthread_mutex_destroy(mutex);
97 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
100 int errorcode = pthread_mutex_lock(mutex);
101 return errorcode == 0;
111 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
114 int errorcode = pthread_mutex_unlock(mutex);
115 return errorcode == 0;
125 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
128 int errorcode = pthread_mutex_trylock(mutex);
129 return errorcode == 0;
135 #elif defined(LLVM_ON_UNIX)
136 #include "Unix/Mutex.inc"
137 #elif defined( LLVM_ON_WIN32)
138 #include "Win32/Mutex.inc"
140 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp