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