spinlock.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //----------------------------------------------------------------------------
  2. /// @file spinlock_t.hpp
  3. /// @brief
  4. ///
  5. /// @author Copyright (c) 2010 2015 Francisco José Tapia ([email protected] )\n
  6. /// Distributed under the Boost Software License, Version 1.0.\n
  7. /// ( See accompanyingfile LICENSE_1_0.txt or copy at
  8. /// http://www.boost.org/LICENSE_1_0.txt )
  9. /// @version 0.1
  10. ///
  11. /// @remarks
  12. //-----------------------------------------------------------------------------
  13. #ifndef __BOOST_SORT_PARALLEL_DETAIL_UTIL_SPINLOCK_HPP
  14. #define __BOOST_SORT_PARALLEL_DETAIL_UTIL_SPINLOCK_HPP
  15. #include <ciso646>
  16. #include <atomic>
  17. #include <ctime>
  18. #include <functional>
  19. #include <memory>
  20. #include <mutex>
  21. #include <thread>
  22. namespace boost
  23. {
  24. namespace sort
  25. {
  26. namespace common
  27. {
  28. //
  29. //---------------------------------------------------------------------------
  30. /// @class spinlock_t
  31. /// @brief This class implement, from atomic variables, a spinlock
  32. /// @remarks This class meet the BasicLockable requirements ( lock, unlock )
  33. //---------------------------------------------------------------------------
  34. class spinlock_t
  35. {
  36. private:
  37. //------------------------------------------------------------------------
  38. // P R I V A T E V A R I A B L E S
  39. //------------------------------------------------------------------------
  40. std::atomic_flag af;
  41. public:
  42. //
  43. //-------------------------------------------------------------------------
  44. // function : spinlock_t
  45. /// @brief class constructor
  46. /// @param [in]
  47. //-------------------------------------------------------------------------
  48. explicit spinlock_t ( ) noexcept { af.clear ( ); };
  49. //
  50. //-------------------------------------------------------------------------
  51. // function : lock
  52. /// @brief Lock the spinlock_t
  53. //-------------------------------------------------------------------------
  54. void lock ( ) noexcept
  55. {
  56. while (af.test_and_set (std::memory_order_acquire))
  57. {
  58. std::this_thread::yield ( );
  59. };
  60. };
  61. //
  62. //-------------------------------------------------------------------------
  63. // function : try_lock
  64. /// @brief Try to lock the spinlock_t, if not, return false
  65. /// @return true : locked
  66. /// false: not previous locked
  67. //-------------------------------------------------------------------------
  68. bool try_lock ( ) noexcept
  69. {
  70. return !af.test_and_set (std::memory_order_acquire);
  71. };
  72. //
  73. //-------------------------------------------------------------------------
  74. // function : unlock
  75. /// @brief unlock the spinlock_t
  76. //-------------------------------------------------------------------------
  77. void unlock ( ) noexcept { af.clear (std::memory_order_release); };
  78. }; // E N D C L A S S S P I N L O C K
  79. //
  80. //***************************************************************************
  81. }; // end namespace common
  82. }; // end namespace sort
  83. }; // end namespace boost
  84. //***************************************************************************
  85. #endif