]> git.donarmstrong.com Git - rsem.git/blob - boost/smart_ptr/detail/spinlock_gcc_arm.hpp
ba6c511e28015409229a4ef1ec82428a909bc3ee
[rsem.git] / boost / smart_ptr / detail / spinlock_gcc_arm.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ARM_HPP_INCLUDED
2 #define BOOST_SMART_PTR_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 <boost/smart_ptr/detail/yield_k.hpp>
13
14 namespace boost
15 {
16
17 namespace detail
18 {
19
20 class spinlock
21 {
22 public:
23
24     int v_;
25
26 public:
27
28     bool try_lock()
29     {
30         int r;
31
32         __asm__ __volatile__(
33             "swp %0, %1, [%2]":
34             "=&r"( r ): // outputs
35             "r"( 1 ), "r"( &v_ ): // inputs
36             "memory", "cc" );
37
38         return r == 0;
39     }
40
41     void lock()
42     {
43         for( unsigned k = 0; !try_lock(); ++k )
44         {
45             boost::detail::yield( k );
46         }
47     }
48
49     void unlock()
50     {
51         __asm__ __volatile__( "" ::: "memory" );
52         *const_cast< int volatile* >( &v_ ) = 0;
53     }
54
55 public:
56
57     class scoped_lock
58     {
59     private:
60
61         spinlock & sp_;
62
63         scoped_lock( scoped_lock const & );
64         scoped_lock & operator=( scoped_lock const & );
65
66     public:
67
68         explicit scoped_lock( spinlock & sp ): sp_( sp )
69         {
70             sp.lock();
71         }
72
73         ~scoped_lock()
74         {
75             sp_.unlock();
76         }
77     };
78 };
79
80 } // namespace detail
81 } // namespace boost
82
83 #define BOOST_DETAIL_SPINLOCK_INIT {0}
84
85 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ARM_HPP_INCLUDED