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