Chris@16
|
1 #ifndef BOOST_ATOMIC_DETAIL_LOCKPOOL_HPP
|
Chris@16
|
2 #define BOOST_ATOMIC_DETAIL_LOCKPOOL_HPP
|
Chris@16
|
3
|
Chris@16
|
4 // Copyright (c) 2011 Helge Bahmann
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
7 // See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9
|
Chris@16
|
10 #include <boost/atomic/detail/config.hpp>
|
Chris@16
|
11 #include <boost/atomic/detail/link.hpp>
|
Chris@16
|
12 #ifndef BOOST_ATOMIC_FLAG_LOCK_FREE
|
Chris@16
|
13 #include <boost/smart_ptr/detail/lightweight_mutex.hpp>
|
Chris@16
|
14 #endif
|
Chris@16
|
15
|
Chris@16
|
16 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
17 #pragma once
|
Chris@16
|
18 #endif
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost {
|
Chris@16
|
21 namespace atomics {
|
Chris@16
|
22 namespace detail {
|
Chris@16
|
23
|
Chris@16
|
24 #ifndef BOOST_ATOMIC_FLAG_LOCK_FREE
|
Chris@16
|
25
|
Chris@16
|
26 class lockpool
|
Chris@16
|
27 {
|
Chris@16
|
28 public:
|
Chris@16
|
29 typedef boost::detail::lightweight_mutex lock_type;
|
Chris@16
|
30 class scoped_lock :
|
Chris@16
|
31 public lock_type::scoped_lock
|
Chris@16
|
32 {
|
Chris@16
|
33 typedef lock_type::scoped_lock base_type;
|
Chris@16
|
34
|
Chris@16
|
35 public:
|
Chris@16
|
36 explicit scoped_lock(const volatile void * addr) : base_type(get_lock_for(addr))
|
Chris@16
|
37 {
|
Chris@16
|
38 }
|
Chris@16
|
39
|
Chris@16
|
40 BOOST_DELETED_FUNCTION(scoped_lock(scoped_lock const&))
|
Chris@16
|
41 BOOST_DELETED_FUNCTION(scoped_lock& operator=(scoped_lock const&))
|
Chris@16
|
42 };
|
Chris@16
|
43
|
Chris@16
|
44 private:
|
Chris@16
|
45 static BOOST_ATOMIC_DECL lock_type& get_lock_for(const volatile void * addr);
|
Chris@16
|
46 };
|
Chris@16
|
47
|
Chris@16
|
48 #else
|
Chris@16
|
49
|
Chris@16
|
50 class lockpool
|
Chris@16
|
51 {
|
Chris@16
|
52 public:
|
Chris@16
|
53 typedef atomic_flag lock_type;
|
Chris@16
|
54
|
Chris@16
|
55 class scoped_lock
|
Chris@16
|
56 {
|
Chris@16
|
57 private:
|
Chris@16
|
58 atomic_flag& flag_;
|
Chris@16
|
59
|
Chris@16
|
60 public:
|
Chris@16
|
61 explicit
|
Chris@16
|
62 scoped_lock(const volatile void * addr) : flag_(get_lock_for(addr))
|
Chris@16
|
63 {
|
Chris@16
|
64 for (; flag_.test_and_set(memory_order_acquire);)
|
Chris@16
|
65 {
|
Chris@16
|
66 #if defined(BOOST_ATOMIC_X86_PAUSE)
|
Chris@16
|
67 BOOST_ATOMIC_X86_PAUSE();
|
Chris@16
|
68 #endif
|
Chris@16
|
69 }
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 ~scoped_lock(void)
|
Chris@16
|
73 {
|
Chris@16
|
74 flag_.clear(memory_order_release);
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 BOOST_DELETED_FUNCTION(scoped_lock(const scoped_lock &))
|
Chris@16
|
78 BOOST_DELETED_FUNCTION(scoped_lock& operator=(const scoped_lock &))
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 private:
|
Chris@16
|
82 static BOOST_ATOMIC_DECL lock_type& get_lock_for(const volatile void * addr);
|
Chris@16
|
83 };
|
Chris@16
|
84
|
Chris@16
|
85 #endif
|
Chris@16
|
86
|
Chris@16
|
87 }
|
Chris@16
|
88 }
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 #endif
|