1 //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/Support/Mutex.h"
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only TRULY operating system
19 //=== independent code.
20 //===----------------------------------------------------------------------===//
22 #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
23 // Define all methods as no-ops if threading is explicitly disabled
26 MutexImpl::MutexImpl( bool recursive) { }
27 MutexImpl::~MutexImpl() { }
28 bool MutexImpl::acquire() { return true; }
29 bool MutexImpl::release() { return true; }
30 bool MutexImpl::tryacquire() { return true; }
34 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
43 // Construct a Mutex using pthread calls
44 MutexImpl::MutexImpl( bool recursive)
47 // Declare the pthread_mutex data structures
48 pthread_mutex_t* mutex =
49 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
50 pthread_mutexattr_t attr;
52 // Initialize the mutex attributes
53 int errorcode = pthread_mutexattr_init(&attr);
54 assert(errorcode == 0); (void)errorcode;
56 // Initialize the mutex as a recursive mutex, if requested, or normal
58 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
59 errorcode = pthread_mutexattr_settype(&attr, kind);
60 assert(errorcode == 0);
62 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && \
63 !defined(__DragonFly__) && !defined(__Bitrig__)
64 // Make it a process local mutex
65 errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
66 assert(errorcode == 0);
69 // Initialize the mutex
70 errorcode = pthread_mutex_init(mutex, &attr);
71 assert(errorcode == 0);
73 // Destroy the attributes
74 errorcode = pthread_mutexattr_destroy(&attr);
75 assert(errorcode == 0);
77 // Assign the data member
82 MutexImpl::~MutexImpl()
84 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
86 pthread_mutex_destroy(mutex);
93 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
96 int errorcode = pthread_mutex_lock(mutex);
97 return errorcode == 0;
103 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
106 int errorcode = pthread_mutex_unlock(mutex);
107 return errorcode == 0;
111 MutexImpl::tryacquire()
113 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
116 int errorcode = pthread_mutex_trylock(mutex);
117 return errorcode == 0;
122 #elif defined(LLVM_ON_UNIX)
123 #include "Unix/Mutex.inc"
124 #elif defined( LLVM_ON_WIN32)
125 #include "Windows/Mutex.inc"
127 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp