spinlock.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  21. * @file spinlock.h
  22. *
  23. * This file contains a declaration of the SpinLock class, and helper class SpinLockGuard.
  24. */
  25. #ifndef CXXUTIL_SPIN_LOCK_H_
  26. #define CXXUTIL_SPIN_LOCK_H_
  27. #include <pthread.h>
  28. #include <atomic>
  29. #include "cxxutil/noncopy.h"
  30. namespace edk {
  31. // FIXME(dmh): pthread return code is not handle
  32. /**
  33. * @brief Spin lock implementation using pthread
  34. */
  35. class SpinLock : public NonCopyable {
  36. public:
  37. /**
  38. * @brief Construct a new SpinLock object
  39. */
  40. SpinLock() { pthread_spin_init(&lock_, PTHREAD_PROCESS_PRIVATE); }
  41. /**
  42. * @brief Destroy the SpinLock object
  43. */
  44. ~SpinLock() { pthread_spin_destroy(&lock_); }
  45. /**
  46. * @brief Lock the spinlock
  47. */
  48. void Lock() { pthread_spin_lock(&lock_); }
  49. /**
  50. * @brief Unlock the spinlock
  51. */
  52. void Unlock() { pthread_spin_unlock(&lock_); }
  53. private:
  54. pthread_spinlock_t lock_;
  55. };
  56. /**
  57. * @brief Spin lock helper class, provide RAII management
  58. */
  59. class SpinLockGuard {
  60. public:
  61. /**
  62. * Constructor, lock the spinlock in construction
  63. * @param lock Spin lock instance.
  64. */
  65. explicit SpinLockGuard(SpinLock &lock) : lock_(lock) { // NOLINT
  66. lock_.Lock();
  67. is_locked.store(true, std::memory_order_release);
  68. }
  69. /**
  70. * @brief Lock the spinlock if have not been locked by this guard
  71. */
  72. void Lock() {
  73. if (!is_locked.load(std::memory_order_consume)) {
  74. lock_.Lock();
  75. is_locked.store(true, std::memory_order_release);
  76. }
  77. }
  78. /**
  79. * @brief Unlock the spinlock if have been locked by this guard
  80. */
  81. void Unlock() {
  82. if (is_locked.load(std::memory_order_consume)) {
  83. lock_.Unlock();
  84. is_locked.store(false, std::memory_order_release);
  85. }
  86. }
  87. /**
  88. * Destructor, unlock the spinlock in destruction
  89. */
  90. ~SpinLockGuard() {
  91. if (is_locked.load(std::memory_order_consume)) {
  92. lock_.Unlock();
  93. }
  94. }
  95. private:
  96. SpinLock &lock_;
  97. std::atomic<bool> is_locked{false};
  98. };
  99. } // namespace edk
  100. #endif // CXXUTIL_SPIN_LOCK_H_