]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/lwm_linux.hpp
update to boost 1.32.0
[lyx.git] / boost / boost / detail / lwm_linux.hpp
1 #ifndef BOOST_DETAIL_LWM_LINUX_HPP_INCLUDED
2 #define BOOST_DETAIL_LWM_LINUX_HPP_INCLUDED
3
4 //
5 //  boost/detail/lwm_linux.hpp
6 //
7 //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 //
13
14 //
15 //  This implementation uses <asm/atomic.h>. This is a kernel header;
16 //  using kernel headers in a user program may cause a number of problems,
17 //  and not all flavors of Linux provide the atomic instructions.
18 //
19 //  This file is only provided because the performance of this implementation
20 //  is about 3.5 times higher than the pthreads version. Use at your own risk
21 //  (by defining BOOST_USE_ASM_ATOMIC_H.)
22 //
23
24 #include <asm/atomic.h>
25 #include <sched.h>
26
27 namespace boost
28 {
29
30 namespace detail
31 {
32
33 class lightweight_mutex
34 {
35 private:
36
37     atomic_t a_;
38
39     lightweight_mutex(lightweight_mutex const &);
40     lightweight_mutex & operator=(lightweight_mutex const &);
41
42 public:
43
44     lightweight_mutex()
45     {
46         atomic_t a = ATOMIC_INIT(1);
47         a_ = a;
48     }
49
50     class scoped_lock;
51     friend class scoped_lock;
52
53     class scoped_lock
54     {
55     private:
56
57         lightweight_mutex & m_;
58
59         scoped_lock(scoped_lock const &);
60         scoped_lock & operator=(scoped_lock const &);
61
62     public:
63
64         explicit scoped_lock(lightweight_mutex & m): m_(m)
65         {
66             while( !atomic_dec_and_test(&m_.a_) )
67             {
68                 atomic_inc(&m_.a_);
69                 sched_yield();
70             }
71         }
72
73         ~scoped_lock()
74         {
75             atomic_inc(&m_.a_);
76         }
77     };
78 };
79
80 } // namespace detail
81
82 } // namespace boost
83
84 #endif // #ifndef BOOST_DETAIL_LWM_LINUX_HPP_INCLUDED