src/sysc/packages/boost/detail/lwm_win32.hpp

Go to the documentation of this file.
00001 #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
00002 #define BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
00003 
00004 #if _MSC_VER >= 1020
00005 #pragma once
00006 #endif
00007 
00008 //
00009 //  boost/detail/lwm_win32.hpp
00010 //
00011 //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
00012 //
00013 //  Permission to copy, use, modify, sell and distribute this software
00014 //  is granted provided this copyright notice appears in all copies.
00015 //  This software is provided "as is" without express or implied
00016 //  warranty, and with no claim as to its suitability for any purpose.
00017 //
00018 
00019 #include <sysc/packages/boost/detail/winapi.hpp>
00020 
00021 #ifdef __BORLANDC__
00022 # pragma warn -8027     // Functions containing while are not expanded inline
00023 #endif
00024 
00025 namespace boost
00026 {
00027 
00028 namespace detail
00029 {
00030 
00031 class lightweight_mutex
00032 {
00033 private:
00034 
00035     long l_;
00036 
00037     lightweight_mutex(lightweight_mutex const &);
00038     lightweight_mutex & operator=(lightweight_mutex const &);
00039 
00040 public:
00041 
00042     lightweight_mutex(): l_(0)
00043     {
00044     }
00045 
00046     class scoped_lock;
00047     friend class scoped_lock;
00048 
00049     class scoped_lock
00050     {
00051     private:
00052 
00053         lightweight_mutex & m_;
00054 
00055         scoped_lock(scoped_lock const &);
00056         scoped_lock & operator=(scoped_lock const &);
00057 
00058     public:
00059 
00060         explicit scoped_lock(lightweight_mutex & m): m_(m)
00061         {
00062             while( winapi::InterlockedExchange(&m_.l_, 1) )
00063             {
00064                 // Note: changed to Sleep(1) from Sleep(0).
00065                 // According to MSDN, Sleep(0) will never yield
00066                 // to a lower-priority thread, whereas Sleep(1)
00067                 // will. Performance seems not to be affected.
00068 
00069                 winapi::Sleep(1);
00070             }
00071         }
00072 
00073         ~scoped_lock()
00074         {
00075             winapi::InterlockedExchange(&m_.l_, 0);
00076 
00077             // Note: adding a yield here will make
00078             // the spinlock more fair and will increase the overall
00079             // performance of some applications substantially in
00080             // high contention situations, but will penalize the
00081             // low contention / single thread case up to 5x
00082         }
00083     };
00084 };
00085 
00086 } // namespace detail
00087 
00088 } // namespace boost
00089 
00090 #ifdef __BORLANDC__
00091 # pragma warn .8027     // Functions containing while are not expanded inline
00092 #endif
00093 
00094 #endif // #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED

Generated on Wed Apr 25 13:53:28 2007 for SystemC by  doxygen 1.5.1