Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1 | #ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED |
| 2 | #define NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_NT_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/assert.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 19 | |
| 20 | namespace ndnboost |
| 21 | { |
| 22 | |
| 23 | namespace detail |
| 24 | { |
| 25 | |
| 26 | class spinlock |
| 27 | { |
| 28 | public: |
| 29 | |
| 30 | bool locked_; |
| 31 | |
| 32 | public: |
| 33 | |
| 34 | inline bool try_lock() |
| 35 | { |
| 36 | if( locked_ ) |
| 37 | { |
| 38 | return false; |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | locked_ = true; |
| 43 | return true; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | inline void lock() |
| 48 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 49 | NDNBOOST_ASSERT( !locked_ ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 50 | locked_ = true; |
| 51 | } |
| 52 | |
| 53 | inline void unlock() |
| 54 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 55 | NDNBOOST_ASSERT( locked_ ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 56 | locked_ = false; |
| 57 | } |
| 58 | |
| 59 | public: |
| 60 | |
| 61 | class scoped_lock |
| 62 | { |
| 63 | private: |
| 64 | |
| 65 | spinlock & sp_; |
| 66 | |
| 67 | scoped_lock( scoped_lock const & ); |
| 68 | scoped_lock & operator=( scoped_lock const & ); |
| 69 | |
| 70 | public: |
| 71 | |
| 72 | explicit scoped_lock( spinlock & sp ): sp_( sp ) |
| 73 | { |
| 74 | sp.lock(); |
| 75 | } |
| 76 | |
| 77 | ~scoped_lock() |
| 78 | { |
| 79 | sp_.unlock(); |
| 80 | } |
| 81 | }; |
| 82 | }; |
| 83 | |
| 84 | } // namespace detail |
| 85 | } // namespace ndnboost |
| 86 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 87 | #define NDNBOOST_DETAIL_SPINLOCK_INIT { false } |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 88 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 89 | #endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED |