1 #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
2 #define BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // boost/detail/lwm_win32.hpp
13 // Copyright (c) 2002, 2003 Peter Dimov
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.
21 #ifdef BOOST_USE_WINDOWS_H
26 # pragma warn -8027 // Functions containing while are not expanded inline
35 #ifndef BOOST_USE_WINDOWS_H
39 // Intel 6.0 on Win64 version, posted by Tim Fenders to [boost-users]
41 extern "C" long_type __cdecl _InterlockedExchange(long volatile *, long);
43 #pragma intrinsic(_InterlockedExchange)
45 inline long InterlockedExchange(long volatile* lp, long l)
47 return _InterlockedExchange(lp, l);
52 extern "C" __declspec(dllimport) long __stdcall InterlockedExchange(long volatile *, long);
56 extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long);
58 #endif // #ifndef BOOST_USE_WINDOWS_H
60 class lightweight_mutex
66 lightweight_mutex(lightweight_mutex const &);
67 lightweight_mutex & operator=(lightweight_mutex const &);
71 lightweight_mutex(): l_(0)
76 friend class scoped_lock;
82 lightweight_mutex & m_;
84 scoped_lock(scoped_lock const &);
85 scoped_lock & operator=(scoped_lock const &);
89 explicit scoped_lock(lightweight_mutex & m): m_(m)
91 while( InterlockedExchange(&m_.l_, 1) )
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.
104 InterlockedExchange(&m_.l_, 0);
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
115 } // namespace detail
120 # pragma warn .8027 // Functions containing while are not expanded inline
123 #endif // #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED