]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/lwm_win32.hpp
22282314fb05fc1a589d20170c627385e9c7ee28
[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 // MS compatible compilers support #pragma once
5
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
9
10 //
11 //  boost/detail/lwm_win32.hpp
12 //
13 //  Copyright (c) 2002, 2003 Peter Dimov
14 //
15 //  Permission to copy, use, modify, sell and distribute this software
16 //  is granted provided this copyright notice appears in all copies.
17 //  This software is provided "as is" without express or implied
18 //  warranty, and with no claim as to its suitability for any purpose.
19 //
20
21 #ifdef BOOST_USE_WINDOWS_H
22 #  include <windows.h>
23 #endif
24
25 #ifdef __BORLANDC__
26 # pragma warn -8027     // Functions containing while are not expanded inline
27 #endif
28
29 namespace boost
30 {
31
32 namespace detail
33 {
34
35 #ifndef BOOST_USE_WINDOWS_H
36
37 #ifdef _WIN64
38
39 // Intel 6.0 on Win64 version, posted by Tim Fenders to [boost-users]
40
41 extern "C" long_type __cdecl _InterlockedExchange(long volatile *, long);
42
43 #pragma intrinsic(_InterlockedExchange)
44
45 inline long InterlockedExchange(long volatile* lp, long l)
46 {
47     return _InterlockedExchange(lp, l);
48 }
49
50 #else  // _WIN64
51
52 extern "C" __declspec(dllimport) long __stdcall InterlockedExchange(long volatile *, long);
53
54 #endif // _WIN64
55
56 extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long);
57
58 #endif // #ifndef BOOST_USE_WINDOWS_H
59
60 class lightweight_mutex
61 {
62 private:
63
64     long l_;
65
66     lightweight_mutex(lightweight_mutex const &);
67     lightweight_mutex & operator=(lightweight_mutex const &);
68
69 public:
70
71     lightweight_mutex(): l_(0)
72     {
73     }
74
75     class scoped_lock;
76     friend class scoped_lock;
77
78     class scoped_lock
79     {
80     private:
81
82         lightweight_mutex & m_;
83
84         scoped_lock(scoped_lock const &);
85         scoped_lock & operator=(scoped_lock const &);
86
87     public:
88
89         explicit scoped_lock(lightweight_mutex & m): m_(m)
90         {
91             while( InterlockedExchange(&m_.l_, 1) )
92             {
93                 // Note: changed to Sleep(1) from Sleep(0).
94                 // According to MSDN, Sleep(0) will never yield
95                 // to a lower-priority thread, whereas Sleep(1)
96                 // will. Performance seems not to be affected.
97
98                 Sleep(1);
99             }
100         }
101
102         ~scoped_lock()
103         {
104             InterlockedExchange(&m_.l_, 0);
105
106             // Note: adding a yield here will make
107             // the spinlock more fair and will increase the overall
108             // performance of some applications substantially in
109             // high contention situations, but will penalize the
110             // low contention / single thread case up to 5x
111         }
112     };
113 };
114
115 } // namespace detail
116
117 } // namespace boost
118
119 #ifdef __BORLANDC__
120 # pragma warn .8027     // Functions containing while are not expanded inline
121 #endif
122
123 #endif // #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED