Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:59:55

0001 //----------------------------------------------------------------------------
0002 /// @file spinlock_t.hpp
0003 /// @brief
0004 ///
0005 /// @author Copyright (c) 2010 2015 Francisco José Tapia (fjtapia@gmail.com )\n
0006 ///         Distributed under the Boost Software License, Version 1.0.\n
0007 ///         ( See accompanyingfile LICENSE_1_0.txt or copy at
0008 ///           http://www.boost.org/LICENSE_1_0.txt  )
0009 /// @version 0.1
0010 ///
0011 /// @remarks
0012 //-----------------------------------------------------------------------------
0013 #ifndef __BOOST_SORT_PARALLEL_DETAIL_UTIL_SPINLOCK_HPP
0014 #define __BOOST_SORT_PARALLEL_DETAIL_UTIL_SPINLOCK_HPP
0015 
0016 #include <ciso646>
0017 #include <atomic>
0018 #include <ctime>
0019 #include <functional>
0020 #include <memory>
0021 #include <mutex>
0022 #include <thread>
0023 
0024 namespace boost
0025 {
0026 namespace sort
0027 {
0028 namespace common
0029 {
0030 //
0031 //---------------------------------------------------------------------------
0032 /// @class spinlock_t
0033 /// @brief This class implement, from atomic variables, a spinlock
0034 /// @remarks This class meet the BasicLockable requirements ( lock, unlock )
0035 //---------------------------------------------------------------------------
0036 class spinlock_t
0037 {
0038   private:
0039     //------------------------------------------------------------------------
0040     //             P R I V A T E      V A R I A B L E S
0041     //------------------------------------------------------------------------
0042     std::atomic_flag af;
0043 
0044   public:
0045     //
0046     //-------------------------------------------------------------------------
0047     //  function : spinlock_t
0048     /// @brief  class constructor
0049     /// @param [in]
0050     //-------------------------------------------------------------------------
0051     explicit spinlock_t ( ) noexcept { af.clear ( ); };
0052     //
0053     //-------------------------------------------------------------------------
0054     //  function : lock
0055     /// @brief  Lock the spinlock_t
0056     //-------------------------------------------------------------------------
0057     void lock ( ) noexcept
0058     {
0059         while (af.test_and_set (std::memory_order_acquire))
0060         {
0061             std::this_thread::yield ( );
0062         };
0063     };
0064     //
0065     //-------------------------------------------------------------------------
0066     //  function : try_lock
0067     /// @brief Try to lock the spinlock_t, if not, return false
0068     /// @return true : locked
0069     ///         false: not previous locked
0070     //-------------------------------------------------------------------------
0071     bool try_lock ( ) noexcept
0072     {
0073         return !af.test_and_set (std::memory_order_acquire);
0074     };
0075     //
0076     //-------------------------------------------------------------------------
0077     //  function : unlock
0078     /// @brief  unlock the spinlock_t
0079     //-------------------------------------------------------------------------
0080     void unlock ( ) noexcept { af.clear (std::memory_order_release); };
0081 
0082 }; // E N D    C L A S S     S P I N L O C K
0083 //
0084 //***************************************************************************
0085 }; // end namespace common
0086 }; // end namespace sort
0087 }; // end namespace boost
0088 //***************************************************************************
0089 #endif