Mercurial > hg > vamp-build-and-test
annotate DEPENDENCIES/generic/include/boost/smart_ptr/detail/spinlock_std_atomic.hpp @ 125:34e428693f5d vext
Vext -> Repoint
author | Chris Cannam |
---|---|
date | Thu, 14 Jun 2018 11:15:39 +0100 |
parents | f46d142149f5 |
children |
rev | line source |
---|---|
Chris@102 | 1 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED |
Chris@102 | 2 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED |
Chris@102 | 3 |
Chris@102 | 4 // MS compatible compilers support #pragma once |
Chris@102 | 5 |
Chris@102 | 6 #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
Chris@102 | 7 # pragma once |
Chris@102 | 8 #endif |
Chris@102 | 9 |
Chris@102 | 10 // |
Chris@102 | 11 // Copyright (c) 2014 Peter Dimov |
Chris@102 | 12 // |
Chris@102 | 13 // Distributed under the Boost Software License, Version 1.0. |
Chris@102 | 14 // See accompanying file LICENSE_1_0.txt or copy at |
Chris@102 | 15 // http://www.boost.org/LICENSE_1_0.txt) |
Chris@102 | 16 // |
Chris@102 | 17 |
Chris@102 | 18 #include <boost/smart_ptr/detail/yield_k.hpp> |
Chris@102 | 19 #include <atomic> |
Chris@102 | 20 |
Chris@102 | 21 namespace boost |
Chris@102 | 22 { |
Chris@102 | 23 |
Chris@102 | 24 namespace detail |
Chris@102 | 25 { |
Chris@102 | 26 |
Chris@102 | 27 class spinlock |
Chris@102 | 28 { |
Chris@102 | 29 public: |
Chris@102 | 30 |
Chris@102 | 31 std::atomic_flag v_; |
Chris@102 | 32 |
Chris@102 | 33 public: |
Chris@102 | 34 |
Chris@102 | 35 bool try_lock() |
Chris@102 | 36 { |
Chris@102 | 37 return !v_.test_and_set( std::memory_order_acquire ); |
Chris@102 | 38 } |
Chris@102 | 39 |
Chris@102 | 40 void lock() |
Chris@102 | 41 { |
Chris@102 | 42 for( unsigned k = 0; !try_lock(); ++k ) |
Chris@102 | 43 { |
Chris@102 | 44 boost::detail::yield( k ); |
Chris@102 | 45 } |
Chris@102 | 46 } |
Chris@102 | 47 |
Chris@102 | 48 void unlock() |
Chris@102 | 49 { |
Chris@102 | 50 v_ .clear( std::memory_order_release ); |
Chris@102 | 51 } |
Chris@102 | 52 |
Chris@102 | 53 public: |
Chris@102 | 54 |
Chris@102 | 55 class scoped_lock |
Chris@102 | 56 { |
Chris@102 | 57 private: |
Chris@102 | 58 |
Chris@102 | 59 spinlock & sp_; |
Chris@102 | 60 |
Chris@102 | 61 scoped_lock( scoped_lock const & ); |
Chris@102 | 62 scoped_lock & operator=( scoped_lock const & ); |
Chris@102 | 63 |
Chris@102 | 64 public: |
Chris@102 | 65 |
Chris@102 | 66 explicit scoped_lock( spinlock & sp ): sp_( sp ) |
Chris@102 | 67 { |
Chris@102 | 68 sp.lock(); |
Chris@102 | 69 } |
Chris@102 | 70 |
Chris@102 | 71 ~scoped_lock() |
Chris@102 | 72 { |
Chris@102 | 73 sp_.unlock(); |
Chris@102 | 74 } |
Chris@102 | 75 }; |
Chris@102 | 76 }; |
Chris@102 | 77 |
Chris@102 | 78 } // namespace detail |
Chris@102 | 79 } // namespace boost |
Chris@102 | 80 |
Chris@102 | 81 #define BOOST_DETAIL_SPINLOCK_INIT { ATOMIC_FLAG_INIT } |
Chris@102 | 82 |
Chris@102 | 83 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED |