rwlock.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef EDK_CXXUTIL_RWLOCK_H_
  21. #define EDK_CXXUTIL_RWLOCK_H_
  22. #include <pthread.h>
  23. #include "cxxutil/noncopy.h"
  24. namespace edk {
  25. // FIXME(dmh): pthread return code is not handle
  26. /**
  27. * @brief Read Write lock based on pthread
  28. */
  29. class RwLock : public NonCopyable {
  30. public:
  31. /**
  32. * @brief Construct a new RwLock object
  33. */
  34. RwLock() { pthread_rwlock_init(&rwlock_, NULL); }
  35. /**
  36. * @brief Destroy the RwLock object
  37. */
  38. ~RwLock() { pthread_rwlock_destroy(&rwlock_); }
  39. /**
  40. * @brief Lock with write access
  41. */
  42. void WriteLock() { pthread_rwlock_wrlock(&rwlock_); }
  43. /**
  44. * @brief Lock with read access
  45. */
  46. void ReadLock() { pthread_rwlock_rdlock(&rwlock_); }
  47. /**
  48. * @brief Unlock
  49. */
  50. void Unlock() { pthread_rwlock_unlock(&rwlock_); }
  51. private:
  52. pthread_rwlock_t rwlock_;
  53. };
  54. /**
  55. * @brief RAII guard of write lock
  56. */
  57. class WriteLockGuard {
  58. public:
  59. /**
  60. * @brief Construct a new Write Lock Guard object, and lock write
  61. *
  62. * @param lock Read write lock
  63. */
  64. explicit WriteLockGuard(RwLock& lock) : lock_(lock) { lock_.WriteLock(); }
  65. /**
  66. * @brief Destroy the Write Lock Guard object, and unlock
  67. */
  68. ~WriteLockGuard() { lock_.Unlock(); }
  69. private:
  70. RwLock& lock_;
  71. };
  72. /**
  73. * @brief RAII guard of read lock
  74. */
  75. class ReadLockGuard {
  76. public:
  77. /**
  78. * @brief Construct a new Read Lock Guard object, and lock read
  79. *
  80. * @param lock Read write lock
  81. */
  82. explicit ReadLockGuard(RwLock& lock) : lock_(lock) { lock_.ReadLock(); }
  83. /**
  84. * @brief Destroy the Read Lock Guard object, and unlock
  85. */
  86. ~ReadLockGuard() { lock_.Unlock(); }
  87. private:
  88. RwLock& lock_;
  89. };
  90. } // namespace edk
  91. #endif // EDK_CXXUTIL_RWLOCK_H_