]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/lwm_win32.hpp
2002-05-24 Lars Gullik Bj�nnes <larsbj@birdstep.com>
[lyx.git] / boost / boost / detail / lwm_win32.hpp
1 #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
2 #define BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
3
4 #if _MSC_VER >= 1020
5 #pragma once
6 #endif
7
8 //
9 //  boost/detail/lwm_win32.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 #include <boost/detail/winapi.hpp>
20
21 namespace boost
22 {
23
24 namespace detail
25 {
26
27 class lightweight_mutex
28 {
29 private:
30
31     long l_;
32
33     lightweight_mutex(lightweight_mutex const &);
34     lightweight_mutex & operator=(lightweight_mutex const &);
35
36 public:
37
38     lightweight_mutex(): l_(0)
39     {
40     }
41
42     class scoped_lock;
43     friend class scoped_lock;
44
45     class scoped_lock
46     {
47     private:
48
49         lightweight_mutex & m_;
50
51         scoped_lock(scoped_lock const &);
52         scoped_lock & operator=(scoped_lock const &);
53
54     public:
55
56         explicit scoped_lock(lightweight_mutex & m): m_(m)
57         {
58             while( winapi::InterlockedExchange(&m_.l_, 1) )
59             {
60                 winapi::Sleep(0);
61             }
62         }
63
64         ~scoped_lock()
65         {
66             winapi::InterlockedExchange(&m_.l_, 0);
67
68             // Note: adding a Sleep(0) here will make
69             // the mutex more fair and will increase the overall
70             // performance of some applications substantially in
71             // high contention situations, but will penalize the
72             // low contention / single thread case up to 5x
73         }
74     };
75 };
76
77 } // namespace detail
78
79 } // namespace boost
80
81 #endif // #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED