spinlock.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //----------------------------------------------------------------------------
  2. /// @file spinlock_t.hpp
  3. /// @brief
  4. ///
  5. /// @author Copyright (c) 2010 2015 Francisco José Tapia (fjtapia@gmail.com )\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 <atomic>
  16. #include <ctime>
  17. #include <functional>
  18. #include <memory>
  19. #include <mutex>
  20. #include <thread>
  21. namespace boost
  22. {
  23. namespace sort
  24. {
  25. namespace common
  26. {
  27. //
  28. //---------------------------------------------------------------------------
  29. /// @class spinlock_t
  30. /// @brief This class implement, from atomic variables, a spinlock
  31. /// @remarks This class meet the BasicLockable requirements ( lock, unlock )
  32. //---------------------------------------------------------------------------
  33. class spinlock_t
  34. {
  35. private:
  36. //------------------------------------------------------------------------
  37. // P R I V A T E V A R I A B L E S
  38. //------------------------------------------------------------------------
  39. std::atomic_flag af;
  40. public:
  41. //
  42. //-------------------------------------------------------------------------
  43. // function : spinlock_t
  44. /// @brief class constructor
  45. /// @param [in]
  46. //-------------------------------------------------------------------------
  47. explicit spinlock_t ( ) noexcept { af.clear ( ); };
  48. //
  49. //-------------------------------------------------------------------------
  50. // function : lock
  51. /// @brief Lock the spinlock_t
  52. //-------------------------------------------------------------------------
  53. void lock ( ) noexcept
  54. {
  55. while (af.test_and_set (std::memory_order_acquire))
  56. {
  57. std::this_thread::yield ( );
  58. };
  59. };
  60. //
  61. //-------------------------------------------------------------------------
  62. // function : try_lock
  63. /// @brief Try to lock the spinlock_t, if not, return false
  64. /// @return true : locked
  65. /// false: not previous locked
  66. //-------------------------------------------------------------------------
  67. bool try_lock ( ) noexcept
  68. {
  69. return not af.test_and_set (std::memory_order_acquire);
  70. };
  71. //
  72. //-------------------------------------------------------------------------
  73. // function : unlock
  74. /// @brief unlock the spinlock_t
  75. //-------------------------------------------------------------------------
  76. void unlock ( ) noexcept { af.clear (std::memory_order_release); };
  77. }; // E N D C L A S S S P I N L O C K
  78. //
  79. //***************************************************************************
  80. }; // end namespace common
  81. }; // end namespace sort
  82. }; // end namespace boost
  83. //***************************************************************************
  84. #endif