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/Support/Mutex.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/ErrorHandling.h"
18 //===----------------------------------------------------------------------===//
19 //=== WARNING: Implementation here must contain only TRULY operating system
20 //=== independent code.
21 //===----------------------------------------------------------------------===//
23 #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
24 // Define all methods as no-ops if threading is explicitly disabled
27 MutexImpl::MutexImpl( bool recursive) { }
28 MutexImpl::~MutexImpl() { }
29 bool MutexImpl::acquire() { return true; }
30 bool MutexImpl::release() { return true; }
31 bool MutexImpl::tryacquire() { return true; }
35 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
44 // Construct a Mutex using pthread calls
45 MutexImpl::MutexImpl( bool recursive)
48 // Declare the pthread_mutex data structures
49 pthread_mutex_t* mutex =
50 static_cast<pthread_mutex_t*>(safe_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); (void)errorcode;
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 // Initialize the mutex
65 errorcode = pthread_mutex_init(mutex, &attr);
66 assert(errorcode == 0);
68 // Destroy the attributes
69 errorcode = pthread_mutexattr_destroy(&attr);
70 assert(errorcode == 0);
72 // Assign the data member
77 MutexImpl::~MutexImpl()
79 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
80 assert(mutex != nullptr);
81 pthread_mutex_destroy(mutex);
88 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
89 assert(mutex != nullptr);
91 int errorcode = pthread_mutex_lock(mutex);
92 return errorcode == 0;
98 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
99 assert(mutex != nullptr);
101 int errorcode = pthread_mutex_unlock(mutex);
102 return errorcode == 0;
106 MutexImpl::tryacquire()
108 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
109 assert(mutex != nullptr);
111 int errorcode = pthread_mutex_trylock(mutex);
112 return errorcode == 0;
117 #elif defined(LLVM_ON_UNIX)
118 #include "Unix/Mutex.inc"
119 #elif defined( _WIN32)
120 #include "Windows/Mutex.inc"
122 #warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp