blob: 5b514c80c6b1e419cbfdc4b0fd019a57f13e4431 [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED
Jeff Thompsonf7d49942013-08-01 16:47:40 -07003
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 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070049 NDNBOOST_ASSERT( !locked_ );
Jeff Thompsonf7d49942013-08-01 16:47:40 -070050 locked_ = true;
51 }
52
53 inline void unlock()
54 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070055 NDNBOOST_ASSERT( locked_ );
Jeff Thompsonf7d49942013-08-01 16:47:40 -070056 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070087#define NDNBOOST_DETAIL_SPINLOCK_INIT { false }
Jeff Thompsonf7d49942013-08-01 16:47:40 -070088
Jeff Thompson3d613fd2013-10-15 15:39:04 -070089#endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED