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);
66 // Make it a process local mutex
67 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
70 // Initialize the mutex
71 errorcode = pthread_mutex_init(mutex, &attr);
72 assert(errorcode == 0);
74 // Destroy the attributes
75 errorcode = pthread_mutexattr_destroy(&attr);
76 assert(errorcode == 0);
78 // Assign the data member
88 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
90 int errorcode = pthread_mutex_destroy(mutex);
100 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
103 int errorcode = pthread_mutex_lock(mutex);
104 return errorcode == 0;
114 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
117 int errorcode = pthread_mutex_unlock(mutex);
118 return errorcode == 0;
128 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data_);
131 int errorcode = pthread_mutex_trylock(mutex);
132 return errorcode == 0;
139 #elif defined(LLVM_ON_UNIX)
140 #include "Unix/Mutex.inc"
141 #elif defined( LLVM_ON_WIN32)
142 #include "Win32/Mutex.inc"
144 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp