LLVM API Documentation

Mutex.cpp
Go to the documentation of this file.
00001 //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the llvm::sys::Mutex class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Config/config.h"
00015 #include "llvm/Support/Mutex.h"
00016 
00017 //===----------------------------------------------------------------------===//
00018 //=== WARNING: Implementation here must contain only TRULY operating system
00019 //===          independent code.
00020 //===----------------------------------------------------------------------===//
00021 
00022 #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
00023 // Define all methods as no-ops if threading is explicitly disabled
00024 namespace llvm {
00025 using namespace sys;
00026 MutexImpl::MutexImpl( bool recursive) { }
00027 MutexImpl::~MutexImpl() { }
00028 bool MutexImpl::acquire() { return true; }
00029 bool MutexImpl::release() { return true; }
00030 bool MutexImpl::tryacquire() { return true; }
00031 }
00032 #else
00033 
00034 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
00035 
00036 #include <cassert>
00037 #include <pthread.h>
00038 #include <stdlib.h>
00039 
00040 namespace llvm {
00041 using namespace sys;
00042 
00043 // Construct a Mutex using pthread calls
00044 MutexImpl::MutexImpl( bool recursive)
00045   : data_(nullptr)
00046 {
00047   // Declare the pthread_mutex data structures
00048   pthread_mutex_t* mutex =
00049     static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
00050   pthread_mutexattr_t attr;
00051 
00052   // Initialize the mutex attributes
00053   int errorcode = pthread_mutexattr_init(&attr);
00054   assert(errorcode == 0); (void)errorcode;
00055 
00056   // Initialize the mutex as a recursive mutex, if requested, or normal
00057   // otherwise.
00058   int kind = ( recursive  ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
00059   errorcode = pthread_mutexattr_settype(&attr, kind);
00060   assert(errorcode == 0);
00061 
00062   // Initialize the mutex
00063   errorcode = pthread_mutex_init(mutex, &attr);
00064   assert(errorcode == 0);
00065 
00066   // Destroy the attributes
00067   errorcode = pthread_mutexattr_destroy(&attr);
00068   assert(errorcode == 0);
00069 
00070   // Assign the data member
00071   data_ = mutex;
00072 }
00073 
00074 // Destruct a Mutex
00075 MutexImpl::~MutexImpl()
00076 {
00077   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
00078   assert(mutex != nullptr);
00079   pthread_mutex_destroy(mutex);
00080   free(mutex);
00081 }
00082 
00083 bool
00084 MutexImpl::acquire()
00085 {
00086   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
00087   assert(mutex != nullptr);
00088 
00089   int errorcode = pthread_mutex_lock(mutex);
00090   return errorcode == 0;
00091 }
00092 
00093 bool
00094 MutexImpl::release()
00095 {
00096   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
00097   assert(mutex != nullptr);
00098 
00099   int errorcode = pthread_mutex_unlock(mutex);
00100   return errorcode == 0;
00101 }
00102 
00103 bool
00104 MutexImpl::tryacquire()
00105 {
00106   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
00107   assert(mutex != nullptr);
00108 
00109   int errorcode = pthread_mutex_trylock(mutex);
00110   return errorcode == 0;
00111 }
00112 
00113 }
00114 
00115 #elif defined(LLVM_ON_UNIX)
00116 #include "Unix/Mutex.inc"
00117 #elif defined( LLVM_ON_WIN32)
00118 #include "Windows/Mutex.inc"
00119 #else
00120 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
00121 #endif
00122 #endif