00001 #ifndef BOOST_DETAIL_LWM_LINUX_HPP_INCLUDED 00002 #define BOOST_DETAIL_LWM_LINUX_HPP_INCLUDED 00003 00004 #if _MSC_VER >= 1020 00005 #pragma once 00006 #endif 00007 00008 // 00009 // boost/detail/lwm_linux.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 // 00020 // This implementation uses <asm/atomic.h>. This is a kernel header; 00021 // using kernel headers in a user program may cause a number of problems, 00022 // and not all flavors of Linux provide the atomic instructions. 00023 // 00024 // This file is only provided because the performance of this implementation 00025 // is about 3.5 times higher than the pthreads version. Use at your own risk 00026 // (by defining BOOST_USE_ASM_ATOMIC_H.) 00027 // 00028 00029 #include <asm/atomic.h> 00030 #include <sched.h> 00031 00032 namespace boost 00033 { 00034 00035 namespace detail 00036 { 00037 00038 class lightweight_mutex 00039 { 00040 private: 00041 00042 atomic_t a_; 00043 00044 lightweight_mutex(lightweight_mutex const &); 00045 lightweight_mutex & operator=(lightweight_mutex const &); 00046 00047 public: 00048 00049 lightweight_mutex() 00050 { 00051 atomic_t a = ATOMIC_INIT(1); 00052 a_ = a; 00053 } 00054 00055 class scoped_lock; 00056 friend class scoped_lock; 00057 00058 class scoped_lock 00059 { 00060 private: 00061 00062 lightweight_mutex & m_; 00063 00064 scoped_lock(scoped_lock const &); 00065 scoped_lock & operator=(scoped_lock const &); 00066 00067 public: 00068 00069 explicit scoped_lock(lightweight_mutex & m): m_(m) 00070 { 00071 while( !atomic_dec_and_test(&m_.a_) ) 00072 { 00073 atomic_inc(&m_.a_); 00074 sched_yield(); 00075 } 00076 } 00077 00078 ~scoped_lock() 00079 { 00080 atomic_inc(&m_.a_); 00081 } 00082 }; 00083 }; 00084 00085 } // namespace detail 00086 00087 } // namespace boost 00088 00089 #endif // #ifndef BOOST_DETAIL_LWM_LINUX_HPP_INCLUDED
1.5.1