RWMutex.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //===- RWMutex.cpp - Reader/Writer Mutual Exclusion Lock --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the llvm::sys::RWMutex class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Config/config.h"
  14. #include "llvm/Support/RWMutex.h"
  15. #include <cstring>
  16. //===----------------------------------------------------------------------===//
  17. //=== WARNING: Implementation here must contain only TRULY operating system
  18. //=== independent code.
  19. //===----------------------------------------------------------------------===//
  20. #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
  21. // Define all methods as no-ops if threading is explicitly disabled
  22. namespace llvm {
  23. using namespace sys;
  24. RWMutexImpl::RWMutexImpl() { }
  25. RWMutexImpl::~RWMutexImpl() { }
  26. bool RWMutexImpl::reader_acquire() { return true; }
  27. bool RWMutexImpl::reader_release() { return true; }
  28. bool RWMutexImpl::writer_acquire() { return true; }
  29. bool RWMutexImpl::writer_release() { return true; }
  30. }
  31. #else
  32. #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
  33. #include <cassert>
  34. #include <pthread.h>
  35. #include <stdlib.h>
  36. namespace llvm {
  37. using namespace sys;
  38. // Construct a RWMutex using pthread calls
  39. RWMutexImpl::RWMutexImpl()
  40. : data_(nullptr)
  41. {
  42. // Declare the pthread_rwlock data structures
  43. pthread_rwlock_t* rwlock =
  44. static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t)));
  45. #ifdef __APPLE__
  46. // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
  47. bzero(rwlock, sizeof(pthread_rwlock_t));
  48. #endif
  49. // Initialize the rwlock
  50. int errorcode = pthread_rwlock_init(rwlock, nullptr);
  51. (void)errorcode;
  52. assert(errorcode == 0);
  53. // Assign the data member
  54. data_ = rwlock;
  55. }
  56. // Destruct a RWMutex
  57. RWMutexImpl::~RWMutexImpl()
  58. {
  59. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  60. assert(rwlock != nullptr);
  61. pthread_rwlock_destroy(rwlock);
  62. free(rwlock);
  63. }
  64. bool
  65. RWMutexImpl::reader_acquire()
  66. {
  67. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  68. assert(rwlock != nullptr);
  69. int errorcode = pthread_rwlock_rdlock(rwlock);
  70. return errorcode == 0;
  71. }
  72. bool
  73. RWMutexImpl::reader_release()
  74. {
  75. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  76. assert(rwlock != nullptr);
  77. int errorcode = pthread_rwlock_unlock(rwlock);
  78. return errorcode == 0;
  79. }
  80. bool
  81. RWMutexImpl::writer_acquire()
  82. {
  83. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  84. assert(rwlock != nullptr);
  85. int errorcode = pthread_rwlock_wrlock(rwlock);
  86. return errorcode == 0;
  87. }
  88. bool
  89. RWMutexImpl::writer_release()
  90. {
  91. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  92. assert(rwlock != nullptr);
  93. int errorcode = pthread_rwlock_unlock(rwlock);
  94. return errorcode == 0;
  95. }
  96. }
  97. #elif defined(LLVM_ON_UNIX)
  98. #include "Unix/RWMutex.inc"
  99. #elif defined( LLVM_ON_WIN32)
  100. #include "Windows/RWMutex.inc"
  101. #else
  102. #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
  103. #endif
  104. #endif