fixing stuff
[dyninst.git] / dynutil / h / dyn_detail / boost / detail / spinlock_gcc_arm.hpp
1 #ifndef DYN_DETAIL_BOOST_DETAIL_SPINLOCK_GCC_ARM_HPP_INCLUDED
2 #define DYN_DETAIL_BOOST_DETAIL_SPINLOCK_GCC_ARM_HPP_INCLUDED
3
4 //
5 //  Copyright (c) 2008 Peter Dimov
6 //
7 //  Distributed under the Boost Software License, Version 1.0.
8 //  See accompanying file LICENSE_1_0.txt or copy at
9 //  http://www.boost.org/LICENSE_1_0.txt)
10 //
11
12 #include <dyn_detail/boost/detail/yield_k.hpp>
13
14 namespace dyn_detail
15 {
16   
17 namespace boost
18 {
19
20 namespace detail
21 {
22
23 class spinlock
24 {
25 public:
26
27     int v_;
28
29 public:
30
31     bool try_lock()
32     {
33         int r;
34
35         __asm__ __volatile__(
36             "swp %0, %1, [%2]":
37             "=&r"( r ): // outputs
38             "r"( 1 ), "r"( &v_ ): // inputs
39             "memory", "cc" );
40
41         return r == 0;
42     }
43
44     void lock()
45     {
46         for( unsigned k = 0; !try_lock(); ++k )
47         {
48             boost::detail::yield( k );
49         }
50     }
51
52     void unlock()
53     {
54         __asm__ __volatile__( "" ::: "memory" );
55         *const_cast< int volatile* >( &v_ ) = 0;
56     }
57
58 public:
59
60     class scoped_lock
61     {
62     private:
63
64         spinlock & sp_;
65
66         scoped_lock( scoped_lock const & );
67         scoped_lock & operator=( scoped_lock const & );
68
69     public:
70
71         explicit scoped_lock( spinlock & sp ): sp_( sp )
72         {
73             sp.lock();
74         }
75
76         ~scoped_lock()
77         {
78             sp_.unlock();
79         }
80     };
81 };
82
83 } // namespace detail
84 } // namespace boost
85 } // namespace dyn_detail
86
87 #define DYN_DETAIL_BOOST_DETAIL_SPINLOCK_INIT {0}
88
89 #endif // #ifndef DYN_DETAIL_BOOST_DETAIL_SPINLOCK_GCC_ARM_HPP_INCLUDED