00001 #ifndef BOOST_DETAIL_LWM_GCC_HPP_INCLUDED 00002 #define BOOST_DETAIL_LWM_GCC_HPP_INCLUDED 00003 00004 // 00005 // boost/detail/lwm_gcc.hpp 00006 // 00007 // lightweight_mutex for GNU libstdc++ v3 00008 // 00009 // http://gcc.gnu.org/onlinedocs/porting/Thread-safety.html 00010 // 00011 // Copyright (c) 2002 Peter Dimov and Multi Media Ltd. 00012 // Copyright (c) 2002 Lars Gullik Bjønnes <larsbj@lyx.org> 00013 // 00014 // Permission to copy, use, modify, sell and distribute this software 00015 // is granted provided this copyright notice appears in all copies. 00016 // This software is provided "as is" without express or implied 00017 // warranty, and with no claim as to its suitability for any purpose. 00018 // 00019 00020 #include <bits/atomicity.h> 00021 #include <sched.h> 00022 00023 namespace boost 00024 { 00025 00026 namespace detail 00027 { 00028 00029 class lightweight_mutex 00030 { 00031 private: 00032 00033 _Atomic_word a_; 00034 00035 lightweight_mutex(lightweight_mutex const &); 00036 lightweight_mutex & operator=(lightweight_mutex const &); 00037 00038 public: 00039 00040 lightweight_mutex(): a_(1) 00041 { 00042 } 00043 00044 class scoped_lock; 00045 friend class scoped_lock; 00046 00047 class scoped_lock 00048 { 00049 private: 00050 00051 lightweight_mutex & m_; 00052 00053 scoped_lock(scoped_lock const &); 00054 scoped_lock & operator=(scoped_lock const &); 00055 00056 public: 00057 00058 explicit scoped_lock(lightweight_mutex & m): m_(m) 00059 { 00060 while( !__exchange_and_add(&m_.a_, -1) ) 00061 { 00062 __atomic_add(&m_.a_, 1); 00063 sched_yield(); 00064 } 00065 } 00066 00067 ~scoped_lock() 00068 { 00069 __atomic_add(&m_.a_, 1); 00070 } 00071 }; 00072 }; 00073 00074 } // namespace detail 00075 00076 } // namespace boost 00077 00078 #endif // #ifndef BOOST_DETAIL_LWM_GCC_HPP_INCLUDED
1.5.1