Mutex.cpp revision 302408
11541Srgrimes//===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===// 21541Srgrimes// 31541Srgrimes// The LLVM Compiler Infrastructure 41541Srgrimes// 51541Srgrimes// This file is distributed under the University of Illinois Open Source 61541Srgrimes// License. See LICENSE.TXT for details. 71541Srgrimes// 81541Srgrimes//===----------------------------------------------------------------------===// 91541Srgrimes// 101541Srgrimes// This file implements the llvm::sys::Mutex class. 111541Srgrimes// 121541Srgrimes//===----------------------------------------------------------------------===// 131541Srgrimes 141541Srgrimes#include "llvm/Config/config.h" 151541Srgrimes#include "llvm/Support/Mutex.h" 161541Srgrimes 171541Srgrimes//===----------------------------------------------------------------------===// 181541Srgrimes//=== WARNING: Implementation here must contain only TRULY operating system 191541Srgrimes//=== independent code. 201541Srgrimes//===----------------------------------------------------------------------===// 211541Srgrimes 221541Srgrimes#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0 231541Srgrimes// Define all methods as no-ops if threading is explicitly disabled 241541Srgrimesnamespace llvm { 251541Srgrimesusing namespace sys; 261541SrgrimesMutexImpl::MutexImpl( bool recursive) { } 271541SrgrimesMutexImpl::~MutexImpl() { } 281541Srgrimesbool MutexImpl::acquire() { return true; } 291541Srgrimesbool MutexImpl::release() { return true; } 301541Srgrimesbool MutexImpl::tryacquire() { return true; } 311541Srgrimes} 321541Srgrimes#else 331541Srgrimes 3412662Sdg#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK) 351541Srgrimes 361541Srgrimes#include <cassert> 371541Srgrimes#include <pthread.h> 381541Srgrimes#include <stdlib.h> 391541Srgrimes 405455Sdgnamespace llvm { 4112662Sdgusing namespace sys; 421541Srgrimes 431541Srgrimes// Construct a Mutex using pthread calls 441541SrgrimesMutexImpl::MutexImpl( bool recursive) 451541Srgrimes : data_(nullptr) 461541Srgrimes{ 471541Srgrimes // Declare the pthread_mutex data structures 481541Srgrimes pthread_mutex_t* mutex = 491541Srgrimes static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t))); 501541Srgrimes pthread_mutexattr_t attr; 511541Srgrimes 521541Srgrimes // Initialize the mutex attributes 531541Srgrimes int errorcode = pthread_mutexattr_init(&attr); 541541Srgrimes assert(errorcode == 0); (void)errorcode; 5512642Sbde 5612642Sbde // Initialize the mutex as a recursive mutex, if requested, or normal 5712642Sbde // otherwise. 5812642Sbde int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL ); 5912642Sbde errorcode = pthread_mutexattr_settype(&attr, kind); 601541Srgrimes assert(errorcode == 0); 615455Sdg 6212642Sbde // Initialize the mutex 631541Srgrimes errorcode = pthread_mutex_init(mutex, &attr); 645455Sdg assert(errorcode == 0); 65 66 // Destroy the attributes 67 errorcode = pthread_mutexattr_destroy(&attr); 68 assert(errorcode == 0); 69 70 // Assign the data member 71 data_ = mutex; 72} 73 74// Destruct a Mutex 75MutexImpl::~MutexImpl() 76{ 77 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 78 assert(mutex != nullptr); 79 pthread_mutex_destroy(mutex); 80 free(mutex); 81} 82 83bool 84MutexImpl::acquire() 85{ 86 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 87 assert(mutex != nullptr); 88 89 int errorcode = pthread_mutex_lock(mutex); 90 return errorcode == 0; 91} 92 93bool 94MutexImpl::release() 95{ 96 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 97 assert(mutex != nullptr); 98 99 int errorcode = pthread_mutex_unlock(mutex); 100 return errorcode == 0; 101} 102 103bool 104MutexImpl::tryacquire() 105{ 106 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_); 107 assert(mutex != nullptr); 108 109 int errorcode = pthread_mutex_trylock(mutex); 110 return errorcode == 0; 111} 112 113} 114 115#elif defined(LLVM_ON_UNIX) 116#include "Unix/Mutex.inc" 117#elif defined( LLVM_ON_WIN32) 118#include "Windows/Mutex.inc" 119#else 120#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp 121#endif 122#endif 123