]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #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 | ||
18 | #include <boost/smart_ptr/detail/yield_k.hpp> | |
19 | ||
20 | #if defined( __ia64__ ) && defined( __INTEL_COMPILER ) | |
21 | # include <ia64intrin.h> | |
22 | #endif | |
23 | ||
24 | namespace boost | |
25 | { | |
26 | ||
27 | namespace detail | |
28 | { | |
29 | ||
30 | class spinlock | |
31 | { | |
32 | public: | |
33 | ||
34 | int v_; | |
35 | ||
36 | public: | |
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 | boost::detail::yield( k ); | |
49 | } | |
50 | } | |
51 | ||
52 | void unlock() | |
53 | { | |
54 | __sync_lock_release( &v_ ); | |
55 | } | |
56 | ||
57 | public: | |
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 boost | |
84 | ||
85 | #define BOOST_DETAIL_SPINLOCK_INIT {0} | |
86 | ||
87 | #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED |