blob: fa1df85f7604779950ae21750fbc3d52dfc22ea2 [file] [log] [blame]
Jeff Thompsonf7d49942013-08-01 16:47:40 -07001#ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED
2#define BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_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/smart_ptr/detail/yield_k.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070019
20#if defined( __ia64__ ) && defined( __INTEL_COMPILER )
21# include <ia64intrin.h>
22#endif
23
24namespace ndnboost
25{
26
27namespace detail
28{
29
30class spinlock
31{
32public:
33
34 int v_;
35
36public:
37
38 bool try_lock()
39 {
40 int r = __sync_lock_test_and_set( &v_, 1 );
41 return r == 0;
42 }
43
44 void lock()
45 {
46 for( unsigned k = 0; !try_lock(); ++k )
47 {
48 ndnboost::detail::yield( k );
49 }
50 }
51
52 void unlock()
53 {
54 __sync_lock_release( &v_ );
55 }
56
57public:
58
59 class scoped_lock
60 {
61 private:
62
63 spinlock & sp_;
64
65 scoped_lock( scoped_lock const & );
66 scoped_lock & operator=( scoped_lock const & );
67
68 public:
69
70 explicit scoped_lock( spinlock & sp ): sp_( sp )
71 {
72 sp.lock();
73 }
74
75 ~scoped_lock()
76 {
77 sp_.unlock();
78 }
79 };
80};
81
82} // namespace detail
83} // namespace ndnboost
84
85#define BOOST_DETAIL_SPINLOCK_INIT {0}
86
87#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED