RWMutex.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //===- RWMutex.cpp - Reader/Writer Mutual Exclusion Lock --------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the llvm::sys::RWMutex class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/Allocator.h"
  13. #include "llvm/Support/RWMutex.h"
  14. #include "llvm/Config/config.h"
  15. #if defined(LLVM_USE_RW_MUTEX_IMPL)
  16. using namespace llvm;
  17. using namespace sys;
  18. #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
  19. // Define all methods as no-ops if threading is explicitly disabled
  20. RWMutexImpl::RWMutexImpl() = default;
  21. RWMutexImpl::~RWMutexImpl() = default;
  22. bool RWMutexImpl::lock_shared() { return true; }
  23. bool RWMutexImpl::unlock_shared() { return true; }
  24. bool RWMutexImpl::lock() { return true; }
  25. bool RWMutexImpl::unlock() { return true; }
  26. #else
  27. #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
  28. #include <cassert>
  29. #include <cstdlib>
  30. #include <pthread.h>
  31. // Construct a RWMutex using pthread calls
  32. RWMutexImpl::RWMutexImpl()
  33. {
  34. // Declare the pthread_rwlock data structures
  35. pthread_rwlock_t* rwlock =
  36. static_cast<pthread_rwlock_t*>(safe_malloc(sizeof(pthread_rwlock_t)));
  37. #ifdef __APPLE__
  38. // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
  39. bzero(rwlock, sizeof(pthread_rwlock_t));
  40. #endif
  41. // Initialize the rwlock
  42. int errorcode = pthread_rwlock_init(rwlock, nullptr);
  43. (void)errorcode;
  44. assert(errorcode == 0);
  45. // Assign the data member
  46. data_ = rwlock;
  47. }
  48. // Destruct a RWMutex
  49. RWMutexImpl::~RWMutexImpl()
  50. {
  51. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  52. assert(rwlock != nullptr);
  53. pthread_rwlock_destroy(rwlock);
  54. free(rwlock);
  55. }
  56. bool
  57. RWMutexImpl::lock_shared()
  58. {
  59. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  60. assert(rwlock != nullptr);
  61. int errorcode = pthread_rwlock_rdlock(rwlock);
  62. return errorcode == 0;
  63. }
  64. bool
  65. RWMutexImpl::unlock_shared()
  66. {
  67. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  68. assert(rwlock != nullptr);
  69. int errorcode = pthread_rwlock_unlock(rwlock);
  70. return errorcode == 0;
  71. }
  72. bool
  73. RWMutexImpl::lock()
  74. {
  75. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  76. assert(rwlock != nullptr);
  77. int errorcode = pthread_rwlock_wrlock(rwlock);
  78. return errorcode == 0;
  79. }
  80. bool
  81. RWMutexImpl::unlock()
  82. {
  83. pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
  84. assert(rwlock != nullptr);
  85. int errorcode = pthread_rwlock_unlock(rwlock);
  86. return errorcode == 0;
  87. }
  88. #else
  89. RWMutexImpl::RWMutexImpl() : data_(new MutexImpl(false)) { }
  90. RWMutexImpl::~RWMutexImpl() {
  91. delete static_cast<MutexImpl *>(data_);
  92. }
  93. bool RWMutexImpl::lock_shared() {
  94. return static_cast<MutexImpl *>(data_)->acquire();
  95. }
  96. bool RWMutexImpl::unlock_shared() {
  97. return static_cast<MutexImpl *>(data_)->release();
  98. }
  99. bool RWMutexImpl::lock() {
  100. return static_cast<MutexImpl *>(data_)->acquire();
  101. }
  102. bool RWMutexImpl::unlock() {
  103. return static_cast<MutexImpl *>(data_)->release();
  104. }
  105. #endif
  106. #endif
  107. #endif