]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/lwm_win32.hpp
abd18697e7c6a208f790f919edb40fff4bb767ae
[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 #ifdef __BORLANDC__
22 # pragma warn -8027     // Functions containing while are not expanded inline
23 #endif
24
25 namespace boost
26 {
27
28 namespace detail
29 {
30
31 class lightweight_mutex
32 {
33 private:
34
35     long l_;
36
37     lightweight_mutex(lightweight_mutex const &);
38     lightweight_mutex & operator=(lightweight_mutex const &);
39
40 public:
41
42     lightweight_mutex(): l_(0)
43     {
44     }
45
46     class scoped_lock;
47     friend class scoped_lock;
48
49     class scoped_lock
50     {
51     private:
52
53         lightweight_mutex & m_;
54
55         scoped_lock(scoped_lock const &);
56         scoped_lock & operator=(scoped_lock const &);
57
58     public:
59
60         explicit scoped_lock(lightweight_mutex & m): m_(m)
61         {
62             while( winapi::InterlockedExchange(&m_.l_, 1) )
63             {
64                 // Note: changed to Sleep(1) from Sleep(0).
65                 // According to MSDN, Sleep(0) will never yield
66                 // to a lower-priority thread, whereas Sleep(1)
67                 // will. Performance seems not to be affected.
68
69                 winapi::Sleep(1);
70             }
71         }
72
73         ~scoped_lock()
74         {
75             winapi::InterlockedExchange(&m_.l_, 0);
76
77             // Note: adding a yield here will make
78             // the spinlock more fair and will increase the overall
79             // performance of some applications substantially in
80             // high contention situations, but will penalize the
81             // low contention / single thread case up to 5x
82         }
83     };
84 };
85
86 } // namespace detail
87
88 } // namespace boost
89
90 #ifdef __BORLANDC__
91 # pragma warn .8027     // Functions containing while are not expanded inline
92 #endif
93
94 #endif // #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED