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