1
#ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
2
#define BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
9
// boost/detail/lwm_win32.hpp
11
// Copyright (c) 2002, 2003 Peter Dimov
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.
19
#ifdef BOOST_USE_WINDOWS_H
24
# pragma warn -8027 // Functions containing while are not expanded inline
33
#ifndef BOOST_USE_WINDOWS_H
37
// Intel 6.0 on Win64 version, posted by Tim Fenders to [boost-users]
39
extern "C" long_type __cdecl _InterlockedExchange(long volatile *, long);
41
#pragma intrinsic(_InterlockedExchange)
43
inline long InterlockedExchange(long volatile* lp, long l)
45
return _InterlockedExchange(lp, l);
50
extern "C" __declspec(dllimport) long __stdcall InterlockedExchange(long volatile *, long);
54
extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long);
56
#endif // #ifndef BOOST_USE_WINDOWS_H
58
class lightweight_mutex
64
lightweight_mutex(lightweight_mutex const &);
65
lightweight_mutex & operator=(lightweight_mutex const &);
69
lightweight_mutex(): l_(0)
74
friend class scoped_lock;
80
lightweight_mutex & m_;
82
scoped_lock(scoped_lock const &);
83
scoped_lock & operator=(scoped_lock const &);
87
explicit scoped_lock(lightweight_mutex & m): m_(m)
89
while( InterlockedExchange(&m_.l_, 1) )
91
// Note: changed to Sleep(1) from Sleep(0).
92
// According to MSDN, Sleep(0) will never yield
93
// to a lower-priority thread, whereas Sleep(1)
94
// will. Performance seems not to be affected.
102
InterlockedExchange(&m_.l_, 0);
104
// Note: adding a yield here will make
105
// the spinlock more fair and will increase the overall
106
// performance of some applications substantially in
107
// high contention situations, but will penalize the
108
// low contention / single thread case up to 5x
113
} // namespace detail
118
# pragma warn .8027 // Functions containing while are not expanded inline
121
#endif // #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED