blob: 59cc715ccaeb8ff42c625252f5aa3a81e679c6ba [file] [log] [blame]
Jeff Thompsonf7d49942013-08-01 16:47:40 -07001#ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED
2#define BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED
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 Thompson2277ce52013-08-01 17:34:11 -070018#include <ndnboost/assert.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070019
20namespace ndnboost
21{
22
23namespace detail
24{
25
26class spinlock
27{
28public:
29
30 bool locked_;
31
32public:
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 {
49 BOOST_ASSERT( !locked_ );
50 locked_ = true;
51 }
52
53 inline void unlock()
54 {
55 BOOST_ASSERT( locked_ );
56 locked_ = false;
57 }
58
59public:
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
87#define BOOST_DETAIL_SPINLOCK_INIT { false }
88
89#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED