]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/lwm_linux.hpp
Boost 1.31.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 //  Permission to copy, use, modify, sell and distribute this software
10 //  is granted provided this copyright notice appears in all copies.
11 //  This software is provided "as is" without express or implied
12 //  warranty, and with no claim as to its suitability for any purpose.
13 //
14
15 //
16 //  This implementation uses <asm/atomic.h>. This is a kernel header;
17 //  using kernel headers in a user program may cause a number of problems,
18 //  and not all flavors of Linux provide the atomic instructions.
19 //
20 //  This file is only provided because the performance of this implementation
21 //  is about 3.5 times higher than the pthreads version. Use at your own risk
22 //  (by defining BOOST_USE_ASM_ATOMIC_H.)
23 //
24
25 #include <asm/atomic.h>
26 #include <sched.h>
27
28 namespace boost
29 {
30
31 namespace detail
32 {
33
34 class lightweight_mutex
35 {
36 private:
37
38     atomic_t a_;
39
40     lightweight_mutex(lightweight_mutex const &);
41     lightweight_mutex & operator=(lightweight_mutex const &);
42
43 public:
44
45     lightweight_mutex()
46     {
47         atomic_t a = ATOMIC_INIT(1);
48         a_ = a;
49     }
50
51     class scoped_lock;
52     friend class scoped_lock;
53
54     class scoped_lock
55     {
56     private:
57
58         lightweight_mutex & m_;
59
60         scoped_lock(scoped_lock const &);
61         scoped_lock & operator=(scoped_lock const &);
62
63     public:
64
65         explicit scoped_lock(lightweight_mutex & m): m_(m)
66         {
67             while( !atomic_dec_and_test(&m_.a_) )
68             {
69                 atomic_inc(&m_.a_);
70                 sched_yield();
71             }
72         }
73
74         ~scoped_lock()
75         {
76             atomic_inc(&m_.a_);
77         }
78     };
79 };
80
81 } // namespace detail
82
83 } // namespace boost
84
85 #endif // #ifndef BOOST_DETAIL_LWM_LINUX_HPP_INCLUDED