RWMutex.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/Support/RWMutex.h"
  14. #include "llvm/Config/config.h"
  15. //===----------------------------------------------------------------------===//
  16. //=== WARNING: Implementation here must contain only TRULY operating system
  17. //=== independent code.
  18. //===----------------------------------------------------------------------===//
  19. #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
  20. // Define all methods as no-ops if threading is explicitly disabled
  21. using namespace llvm;
  22. using namespace sys;
  23. RWMutexImpl::RWMutexImpl() = default;
  24. RWMutexImpl::~RWMutexImpl() = default;
  25. bool RWMutexImpl::reader_acquire() { return true; }
  26. bool RWMutexImpl::reader_release() { return true; }
  27. bool RWMutexImpl::writer_acquire() { return true; }
  28. bool RWMutexImpl::writer_release() { return true; }
  29. #else
  30. #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
  31. #include <cassert>
  32. #include <cstdlib>
  33. #include <pthread.h>
  34. using namespace llvm;
  35. using namespace sys;
  36. // Construct a RWMutex using pthread calls
  37. RWMutexImpl::RWMutexImpl()
  38. {
  39. // Declare the pthread_rwlock data structures
  40. pthread_rwlock_t* rwlock =
  41. static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t)));
  42. #ifdef __APPLE__
  43. // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
  44. bzero(rwlock, sizeof(pthread_rwlock_t));
  45. #endif
  46. // Initialize the rwlock
  47. int errorcode = pthread_rwlock_init(rwlock, nullptr);
  48. (void)errorcode;
  49. assert(errorcode == 0);
  50. // Assign the data member
  51. data_ = rwlock;
  52. }
  53. // Destruct a RWMutex
  54. RWMutexImpl::~RWMutexImpl()
  55. {
  56. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  57. assert(rwlock != nullptr);
  58. pthread_rwlock_destroy(rwlock);
  59. free(rwlock);
  60. }
  61. bool
  62. RWMutexImpl::reader_acquire()
  63. {
  64. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  65. assert(rwlock != nullptr);
  66. int errorcode = pthread_rwlock_rdlock(rwlock);
  67. return errorcode == 0;
  68. }
  69. bool
  70. RWMutexImpl::reader_release()
  71. {
  72. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  73. assert(rwlock != nullptr);
  74. int errorcode = pthread_rwlock_unlock(rwlock);
  75. return errorcode == 0;
  76. }
  77. bool
  78. RWMutexImpl::writer_acquire()
  79. {
  80. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  81. assert(rwlock != nullptr);
  82. int errorcode = pthread_rwlock_wrlock(rwlock);
  83. return errorcode == 0;
  84. }
  85. bool
  86. RWMutexImpl::writer_release()
  87. {
  88. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  89. assert(rwlock != nullptr);
  90. int errorcode = pthread_rwlock_unlock(rwlock);
  91. return errorcode == 0;
  92. }
  93. #elif defined(LLVM_ON_UNIX)
  94. #include "Unix/RWMutex.inc"
  95. #elif defined( LLVM_ON_WIN32)
  96. #include "Windows/RWMutex.inc"
  97. #else
  98. #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
  99. #endif
  100. #endif