fixing stuff
[dyninst.git] / dynutil / h / dyn_detail / boost / detail / spinlock_w32.hpp
1 #ifndef DYN_DETAIL_BOOST_DETAIL_SPINLOCK_W32_HPP_INCLUDED
2 #define DYN_DETAIL_BOOST_DETAIL_SPINLOCK_W32_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/interlocked.hpp>
19 #include <dyn_detail/boost/detail/yield_k.hpp>
20
21 // DYN_DETAIL_BOOST_COMPILER_FENCE
22
23 #if defined(__INTEL_COMPILER)
24
25 #define DYN_DETAIL_BOOST_COMPILER_FENCE __memory_barrier();
26
27 #elif defined( _MSC_VER ) && _MSC_VER >= 1310
28
29 extern "C" void _ReadWriteBarrier();
30 #pragma intrinsic( _ReadWriteBarrier )
31
32 #define DYN_DETAIL_BOOST_COMPILER_FENCE _ReadWriteBarrier();
33
34 #elif defined(__GNUC__)
35
36 #define DYN_DETAIL_BOOST_COMPILER_FENCE __asm__ __volatile__( "" : : : "memory" );
37
38 #else
39
40 #define DYN_DETAIL_BOOST_COMPILER_FENCE
41
42 #endif
43
44 //
45 namespace dyn_detail
46 {
47   
48
49 namespace boost
50 {
51
52 namespace detail
53 {
54
55 class spinlock
56 {
57 public:
58
59     long v_;
60
61 public:
62
63     bool try_lock()
64     {
65         long r = DYN_DETAIL_BOOST_INTERLOCKED_EXCHANGE( &v_, 1 );
66
67         DYN_DETAIL_BOOST_COMPILER_FENCE
68
69         return r == 0;
70     }
71
72     void lock()
73     {
74         for( unsigned k = 0; !try_lock(); ++k )
75         {
76             boost::detail::yield( k );
77         }
78     }
79
80     void unlock()
81     {
82         DYN_DETAIL_BOOST_COMPILER_FENCE
83         *const_cast< long volatile* >( &v_ ) = 0;
84     }
85
86 public:
87
88     class scoped_lock
89     {
90     private:
91
92         spinlock & sp_;
93
94         scoped_lock( scoped_lock const & );
95         scoped_lock & operator=( scoped_lock const & );
96
97     public:
98
99         explicit scoped_lock( spinlock & sp ): sp_( sp )
100         {
101             sp.lock();
102         }
103
104         ~scoped_lock()
105         {
106             sp_.unlock();
107         }
108     };
109 };
110
111 } // namespace detail
112 } // namespace boost
113 } // namespace dyn_detail
114
115 #define DYN_DETAIL_BOOST_DETAIL_SPINLOCK_INIT {0}
116
117 #endif // #ifndef DYN_DETAIL_BOOST_DETAIL_SPINLOCK_W32_HPP_INCLUDED