Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1 | #ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED |
| 2 | #define NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 3 | |
| 4 | // MS compatible compilers support #pragma once |
| 5 | |
| 6 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
| 7 | # pragma once |
| 8 | #endif |
| 9 | |
| 10 | // |
| 11 | // Copyright (c) 2008 Peter Dimov |
| 12 | // |
| 13 | // Distributed under the Boost Software License, Version 1.0. |
| 14 | // See accompanying file LICENSE_1_0.txt or copy at |
| 15 | // http://www.boost.org/LICENSE_1_0.txt) |
| 16 | // |
| 17 | |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 18 | #include <ndnboost/detail/interlocked.hpp> |
| 19 | #include <ndnboost/smart_ptr/detail/yield_k.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 20 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 21 | // NDNBOOST_COMPILER_FENCE |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 22 | |
| 23 | #if defined(__INTEL_COMPILER) |
| 24 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 25 | #define NDNBOOST_COMPILER_FENCE __memory_barrier(); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 26 | |
| 27 | #elif defined( _MSC_VER ) && _MSC_VER >= 1310 |
| 28 | |
| 29 | extern "C" void _ReadWriteBarrier(); |
| 30 | #pragma intrinsic( _ReadWriteBarrier ) |
| 31 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 32 | #define NDNBOOST_COMPILER_FENCE _ReadWriteBarrier(); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 33 | |
| 34 | #elif defined(__GNUC__) |
| 35 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 36 | #define NDNBOOST_COMPILER_FENCE __asm__ __volatile__( "" : : : "memory" ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 37 | |
| 38 | #else |
| 39 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 40 | #define NDNBOOST_COMPILER_FENCE |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 41 | |
| 42 | #endif |
| 43 | |
| 44 | // |
| 45 | |
| 46 | namespace ndnboost |
| 47 | { |
| 48 | |
| 49 | namespace detail |
| 50 | { |
| 51 | |
| 52 | class spinlock |
| 53 | { |
| 54 | public: |
| 55 | |
| 56 | long v_; |
| 57 | |
| 58 | public: |
| 59 | |
| 60 | bool try_lock() |
| 61 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 62 | long r = NDNBOOST_INTERLOCKED_EXCHANGE( &v_, 1 ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 63 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 64 | NDNBOOST_COMPILER_FENCE |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 65 | |
| 66 | return r == 0; |
| 67 | } |
| 68 | |
| 69 | void lock() |
| 70 | { |
| 71 | for( unsigned k = 0; !try_lock(); ++k ) |
| 72 | { |
| 73 | ndnboost::detail::yield( k ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void unlock() |
| 78 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 79 | NDNBOOST_COMPILER_FENCE |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 80 | *const_cast< long volatile* >( &v_ ) = 0; |
| 81 | } |
| 82 | |
| 83 | public: |
| 84 | |
| 85 | class scoped_lock |
| 86 | { |
| 87 | private: |
| 88 | |
| 89 | spinlock & sp_; |
| 90 | |
| 91 | scoped_lock( scoped_lock const & ); |
| 92 | scoped_lock & operator=( scoped_lock const & ); |
| 93 | |
| 94 | public: |
| 95 | |
| 96 | explicit scoped_lock( spinlock & sp ): sp_( sp ) |
| 97 | { |
| 98 | sp.lock(); |
| 99 | } |
| 100 | |
| 101 | ~scoped_lock() |
| 102 | { |
| 103 | sp_.unlock(); |
| 104 | } |
| 105 | }; |
| 106 | }; |
| 107 | |
| 108 | } // namespace detail |
| 109 | } // namespace ndnboost |
| 110 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 111 | #define NDNBOOST_DETAIL_SPINLOCK_INIT {0} |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 112 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 113 | #endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED |