]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/lwm_gcc.hpp
update boost
[lyx.git] / boost / boost / detail / lwm_gcc.hpp
1 #ifndef BOOST_DETAIL_LWM_GCC_HPP_INCLUDED
2 #define BOOST_DETAIL_LWM_GCC_HPP_INCLUDED
3
4 //
5 //  boost/detail/lwm_gcc.hpp
6 //
7 //  lightweight_mutex for GNU libstdc++ v3
8 //
9 //  http://gcc.gnu.org/onlinedocs/porting/Thread-safety.html
10 //
11 //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
12 //  Copyright (c) 2002 Lars Gullik Bjønnes <larsbj@lyx.org>
13 //
14 //  Permission to copy, use, modify, sell and distribute this software
15 //  is granted provided this copyright notice appears in all copies.
16 //  This software is provided "as is" without express or implied
17 //  warranty, and with no claim as to its suitability for any purpose.
18 //
19
20 #include <bits/atomicity.h>
21 #include <sched.h>
22
23 namespace boost
24 {
25
26 namespace detail
27 {
28
29 class lightweight_mutex
30 {
31 private:
32
33     _Atomic_word a_;
34
35     lightweight_mutex(lightweight_mutex const &);
36     lightweight_mutex & operator=(lightweight_mutex const &);
37
38 public:
39
40     lightweight_mutex(): a_(1)
41     {
42     }
43
44     class scoped_lock;
45     friend class scoped_lock;
46
47     class scoped_lock
48     {
49     private:
50
51         lightweight_mutex & m_;
52
53         scoped_lock(scoped_lock const &);
54         scoped_lock & operator=(scoped_lock const &);
55
56     public:
57
58         explicit scoped_lock(lightweight_mutex & m): m_(m)
59         {
60             while( !__exchange_and_add(&m_.a_, -1) )
61             {
62                 __atomic_add(&m_.a_, 1);
63                 sched_yield();
64             }
65         }
66
67         ~scoped_lock()
68         {
69             __atomic_add(&m_.a_, 1);
70         }
71     };
72 };
73
74 } // namespace detail
75
76 } // namespace boost
77
78 #endif // #ifndef BOOST_DETAIL_LWM_GCC_HPP_INCLUDED