fixing stuff
[dyninst.git] / dynutil / h / dyn_detail / boost / detail / spinlock_sync.hpp
1 #ifndef DYN_DETAIL_BOOST_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED
2 #define DYN_DETAIL_BOOST_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
18 #include <dyn_detail/boost/detail/yield_k.hpp>
19
20 #if defined( __ia64__ ) && defined( __INTEL_COMPILER )
21 # include <ia64intrin.h>
22 #endif
23 namespace dyn_detail
24 {
25   
26 namespace boost
27 {
28
29 namespace detail
30 {
31
32 class spinlock
33 {
34 public:
35
36     int v_;
37
38 public:
39
40     bool try_lock()
41     {
42         int r = __sync_lock_test_and_set( &v_, 1 );
43         return r == 0;
44     }
45
46     void lock()
47     {
48         for( unsigned k = 0; !try_lock(); ++k )
49         {
50             boost::detail::yield( k );
51         }
52     }
53
54     void unlock()
55     {
56         __sync_lock_release( &v_ );
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 boost
86 } // namespace dyn_detail
87
88 #define DYN_DETAIL_BOOST_DETAIL_SPINLOCK_INIT {0}
89
90 #endif // #ifndef DYN_DETAIL_BOOST_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED