]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/smart_ptr/detail/spinlock_w32.hpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
8caa33c8724ddae649f4d62e8b8951c2b03db876
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / smart_ptr / detail / spinlock_w32.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED\r
2 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED\r
3 \r
4 // MS compatible compilers support #pragma once\r
5 \r
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)\r
7 # pragma once\r
8 #endif\r
9 \r
10 //\r
11 //  Copyright (c) 2008 Peter Dimov\r
12 //\r
13 //  Distributed under the Boost Software License, Version 1.0.\r
14 //  See accompanying file LICENSE_1_0.txt or copy at\r
15 //  http://www.boost.org/LICENSE_1_0.txt)\r
16 //\r
17 \r
18 #include <boost/detail/interlocked.hpp>\r
19 #include <boost/smart_ptr/detail/yield_k.hpp>\r
20 \r
21 // BOOST_COMPILER_FENCE\r
22 \r
23 #if defined(__INTEL_COMPILER)\r
24 \r
25 #define BOOST_COMPILER_FENCE __memory_barrier();\r
26 \r
27 #elif defined( _MSC_VER ) && _MSC_VER >= 1310\r
28 \r
29 extern "C" void _ReadWriteBarrier();\r
30 #pragma intrinsic( _ReadWriteBarrier )\r
31 \r
32 #define BOOST_COMPILER_FENCE _ReadWriteBarrier();\r
33 \r
34 #elif defined(__GNUC__)\r
35 \r
36 #define BOOST_COMPILER_FENCE __asm__ __volatile__( "" : : : "memory" );\r
37 \r
38 #else\r
39 \r
40 #define BOOST_COMPILER_FENCE\r
41 \r
42 #endif\r
43 \r
44 //\r
45 \r
46 namespace boost\r
47 {\r
48 \r
49 namespace detail\r
50 {\r
51 \r
52 class spinlock\r
53 {\r
54 public:\r
55 \r
56     long v_;\r
57 \r
58 public:\r
59 \r
60     bool try_lock()\r
61     {\r
62         long r = BOOST_INTERLOCKED_EXCHANGE( &v_, 1 );\r
63 \r
64         BOOST_COMPILER_FENCE\r
65 \r
66         return r == 0;\r
67     }\r
68 \r
69     void lock()\r
70     {\r
71         for( unsigned k = 0; !try_lock(); ++k )\r
72         {\r
73             boost::detail::yield( k );\r
74         }\r
75     }\r
76 \r
77     void unlock()\r
78     {\r
79         BOOST_COMPILER_FENCE\r
80         *const_cast< long volatile* >( &v_ ) = 0;\r
81     }\r
82 \r
83 public:\r
84 \r
85     class scoped_lock\r
86     {\r
87     private:\r
88 \r
89         spinlock & sp_;\r
90 \r
91         scoped_lock( scoped_lock const & );\r
92         scoped_lock & operator=( scoped_lock const & );\r
93 \r
94     public:\r
95 \r
96         explicit scoped_lock( spinlock & sp ): sp_( sp )\r
97         {\r
98             sp.lock();\r
99         }\r
100 \r
101         ~scoped_lock()\r
102         {\r
103             sp_.unlock();\r
104         }\r
105     };\r
106 };\r
107 \r
108 } // namespace detail\r
109 } // namespace boost\r
110 \r
111 #define BOOST_DETAIL_SPINLOCK_INIT {0}\r
112 \r
113 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED\r