LLVM API Documentation

RWMutex.cpp

Go to the documentation of this file.
00001 //===- RWMutex.cpp - Reader/Writer 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::RWMutex class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Config/config.h"
00015 #include "llvm/System/RWMutex.h"
00016 #include <cstring>
00017 
00018 //===----------------------------------------------------------------------===//
00019 //=== WARNING: Implementation here must contain only TRULY operating system
00020 //===          independent code.
00021 //===----------------------------------------------------------------------===//
00022 
00023 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
00024 // Define all methods as no-ops if threading is explicitly disabled
00025 namespace llvm {
00026 using namespace sys;
00027 RWMutexImpl::RWMutexImpl() { }
00028 RWMutexImpl::~RWMutexImpl() { }
00029 bool RWMutexImpl::reader_acquire() { return true; }
00030 bool RWMutexImpl::reader_release() { return true; }
00031 bool RWMutexImpl::writer_acquire() { return true; }
00032 bool RWMutexImpl::writer_release() { return true; }
00033 }
00034 #else
00035 
00036 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
00037 
00038 #include <cassert>
00039 #include <pthread.h>
00040 #include <stdlib.h>
00041 
00042 namespace llvm {
00043 using namespace sys;
00044 
00045 
00046 // This variable is useful for situations where the pthread library has been
00047 // compiled with weak linkage for its interface symbols. This allows the
00048 // threading support to be turned off by simply not linking against -lpthread.
00049 // In that situation, the value of pthread_mutex_init will be 0 and
00050 // consequently pthread_enabled will be false. In such situations, all the
00051 // pthread operations become no-ops and the functions all return false. If
00052 // pthread_rwlock_init does have an address, then rwlock support is enabled.
00053 // Note: all LLVM tools will link against -lpthread if its available since it
00054 //       is configured into the LIBS variable.
00055 // Note: this line of code generates a warning if pthread_rwlock_init is not
00056 //       declared with weak linkage. It's safe to ignore the warning.
00057 static const bool pthread_enabled = true;
00058 
00059 // Construct a RWMutex using pthread calls
00060 RWMutexImpl::RWMutexImpl()
00061   : data_(0)
00062 {
00063   if (pthread_enabled)
00064   {
00065     // Declare the pthread_rwlock data structures
00066     pthread_rwlock_t* rwlock =
00067       static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t)));
00068 
00069 #ifdef __APPLE__
00070     // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
00071     bzero(rwlock, sizeof(pthread_rwlock_t));
00072 #endif
00073 
00074     pthread_rwlockattr_t attr;
00075 
00076     // Initialize the rwlock attributes
00077     int errorcode = pthread_rwlockattr_init(&attr);
00078     assert(errorcode == 0);
00079 
00080 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__)
00081     // Make it a process local rwlock
00082     errorcode = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
00083 #endif
00084 
00085     // Initialize the rwlock
00086     errorcode = pthread_rwlock_init(rwlock, &attr);
00087     assert(errorcode == 0);
00088 
00089     // Destroy the attributes
00090     errorcode = pthread_rwlockattr_destroy(&attr);
00091     assert(errorcode == 0);
00092 
00093     // Assign the data member
00094     data_ = rwlock;
00095   }
00096 }
00097 
00098 // Destruct a RWMutex
00099 RWMutexImpl::~RWMutexImpl()
00100 {
00101   if (pthread_enabled)
00102   {
00103     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
00104     assert(rwlock != 0);
00105     pthread_rwlock_destroy(rwlock);
00106     free(rwlock);
00107   }
00108 }
00109 
00110 bool
00111 RWMutexImpl::reader_acquire()
00112 {
00113   if (pthread_enabled)
00114   {
00115     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
00116     assert(rwlock != 0);
00117 
00118     int errorcode = pthread_rwlock_rdlock(rwlock);
00119     return errorcode == 0;
00120   }
00121   return false;
00122 }
00123 
00124 bool
00125 RWMutexImpl::reader_release()
00126 {
00127   if (pthread_enabled)
00128   {
00129     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
00130     assert(rwlock != 0);
00131 
00132     int errorcode = pthread_rwlock_unlock(rwlock);
00133     return errorcode == 0;
00134   }
00135   return false;
00136 }
00137 
00138 bool
00139 RWMutexImpl::writer_acquire()
00140 {
00141   if (pthread_enabled)
00142   {
00143     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
00144     assert(rwlock != 0);
00145 
00146     int errorcode = pthread_rwlock_wrlock(rwlock);
00147     return errorcode == 0;
00148   }
00149   return false;
00150 }
00151 
00152 bool
00153 RWMutexImpl::writer_release()
00154 {
00155   if (pthread_enabled)
00156   {
00157     pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
00158     assert(rwlock != 0);
00159 
00160     int errorcode = pthread_rwlock_unlock(rwlock);
00161     return errorcode == 0;
00162   }
00163   return false;
00164 }
00165 
00166 }
00167 
00168 #elif defined(LLVM_ON_UNIX)
00169 #include "Unix/RWMutex.inc"
00170 #elif defined( LLVM_ON_WIN32)
00171 #include "Win32/RWMutex.inc"
00172 #else
00173 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp
00174 #endif
00175 #endif