Boost logo

Boost :

From: Vadim Egorov (egorovv_at_[hidden])
Date: 2002-03-01 20:58:53


Here is my attempt to fix boost atomicity support using gcc
inline asm for i386 platform.

Comments?

Index: boost/config/compiler/gcc.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/config/compiler/gcc.hpp,v
retrieving revision 1.11
diff -c -r1.11 gcc.hpp
*** boost/config/compiler/gcc.hpp 2 Feb 2002 18:36:11 -0000 1.11
--- boost/config/compiler/gcc.hpp 28 Feb 2002 21:27:04 -0000
***************
*** 35,40 ****
--- 35,44 ----
  //
  #define BOOST_HAS_THREADS
  
+ #ifdef __i386__
+ # define BOOST_HAS_ATOMICOPS
+ #endif
+
  //
  // gcc has "long long"
  //
Index: boost/detail/atomic_count.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/detail/atomic_count.hpp,v
retrieving revision 1.3
diff -c -r1.3 atomic_count.hpp
*** boost/detail/atomic_count.hpp 4 Feb 2002 08:00:16 -0000 1.3
--- boost/detail/atomic_count.hpp 28 Feb 2002 21:27:05 -0000
***************
*** 93,101 ****
  
  #include <boost/detail/atomic_count_win32.hpp>
  
! #elif defined(linux) || defined(__linux) || defined(__linux__)
  
! #include <boost/detail/atomic_count_linux.hpp>
  
  #elif defined(BOOST_HAS_PTHREADS)
  
--- 93,101 ----
  
  #include <boost/detail/atomic_count_win32.hpp>
  
! #elif defined(BOOST_HAS_ATOMICOPS)
  
! #include <boost/detail/atomic_count_atomic.hpp>
  
  #elif defined(BOOST_HAS_PTHREADS)
  
Index: boost/detail/atomic_count_atomic.hpp
===================================================================
RCS file: boost/detail/atomic_count_atomic.hpp
diff -N boost/detail/atomic_count_atomic.hpp
*** /dev/null 1 Jan 1970 00:00:00 -0000
--- boost/detail/atomic_count_atomic.hpp 28 Feb 2002 21:27:05 -0000
***************
*** 0 ****
--- 1,47 ----
+ #ifndef BOOST_DETAIL_ATOMIC_COUNT_ATOMIC_HPP_INCLUDED
+ #define BOOST_DETAIL_ATOMIC_COUNT_ATOMIC_HPP_INCLUDED
+
+ #include <boost/detail/atomic_ops.hpp>
+
+ namespace boost
+ {
+
+ namespace detail
+ {
+
+ class atomic_count
+ {
+ public:
+
+ explicit atomic_count(long v) : v_(v)
+ {
+ }
+
+ void operator++()
+ {
+ atomic_inc(&value_);
+ }
+
+ long operator--()
+ {
+ return !atomic_dec_and_test(&value_);
+ }
+
+ operator long() const
+ {
+ return value_;
+ }
+
+ private:
+
+ atomic_count(atomic_count const &);
+ atomic_count & operator=(atomic_count const &);
+
+ atomic_t value_;
+ };
+
+ } // namespace detail
+
+ } // namespace boost
+
+ #endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_ATOMIC_HPP_INCLUDED
Index: boost/detail/atomic_ops.hpp
===================================================================
RCS file: boost/detail/atomic_ops.hpp
diff -N boost/detail/atomic_ops.hpp
*** /dev/null 1 Jan 1970 00:00:00 -0000
--- boost/detail/atomic_ops.hpp 28 Feb 2002 21:27:05 -0000
***************
*** 0 ****
--- 1,30 ----
+ #ifndef BOOST_DETAIL_ATOMIC_OPS_HPP_INCLUDED
+ #define BOOST_DETAIL_ATOMIC_OPS_HPP_INCLUDED
+
+ #include <boost/config.hpp>
+
+ #if defined(__GNUC__) && defined(__i386__)
+
+ static inline long
+ atomic_inc(volatile long *mem)
+ {
+ register long result;
+ __asm__ __volatile__ ("lock; incl %0"
+ :"=m" (*mem)
+ :"m" (*mem));
+ return result;
+ }
+
+ static inline bool
+ atomic_dec_and_test(volatile long* mem)
+ {
+ unsigned char c;
+ __asm__ __volatile__ ("lock; decl %0; sete %1"
+ :"=m" (*mem), "=qm" (c)
+ :"m" (*mem) : "memory");
+ return c != 0;
+ }
+
+ #endif // defined(__GNUC__) && defined(__i386__)
+
+ #endif // #ifndef BOOST_DETAIL_ATOMIC_OPS_HPP_INCLUDED
Index: boost/detail/lightweight_mutex.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/detail/lightweight_mutex.hpp,v
retrieving revision 1.3
diff -c -r1.3 lightweight_mutex.hpp
*** boost/detail/lightweight_mutex.hpp 27 Feb 2002 16:35:15 -0000 1.3
--- boost/detail/lightweight_mutex.hpp 28 Feb 2002 21:27:05 -0000
***************
*** 38,47 ****
  # include <boost/detail/lwm_nop.hpp>
  #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
  # include <boost/detail/lwm_win32.hpp>
- #elif defined(linux) || defined(__linux) || defined(__linux__)
- # include <boost/detail/lwm_linux.hpp>
  #elif defined(__sgi)
  # include <boost/detail/lwm_irix.hpp>
  #elif defined(BOOST_HAS_PTHREADS)
  # include <boost/detail/lwm_pthreads.hpp>
  #else
--- 38,47 ----
  # include <boost/detail/lwm_nop.hpp>
  #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
  # include <boost/detail/lwm_win32.hpp>
  #elif defined(__sgi)
  # include <boost/detail/lwm_irix.hpp>
+ #elif defined(BOOST_HAS_ATOMICOPS)
+ # include <boost/detail/lwm_atomic.hpp>
  #elif defined(BOOST_HAS_PTHREADS)
  # include <boost/detail/lwm_pthreads.hpp>
  #else
Index: boost/detail/lwm_atomic.hpp
===================================================================
RCS file: boost/detail/lwm_atomic.hpp
diff -N boost/detail/lwm_atomic.hpp
*** /dev/null 1 Jan 1970 00:00:00 -0000
--- boost/detail/lwm_atomic.hpp 28 Feb 2002 21:27:06 -0000
***************
*** 0 ****
--- 1,70 ----
+ #ifndef BOOST_DETAIL_LWM_ATOMIC_HPP_INCLUDED
+ #define BOOST_DETAIL_LWM_ATOMIC_HPP_INCLUDED
+
+ #if _MSC_VER >= 1020
+ #pragma once
+ #endif
+
+ #if defined(BOOST_HAS_SCHED_YIELD)
+ #include <sched.h>
+ #endif // BOOST_HAS_SCHED_YIELD
+ #include <boost/detail/atomic_ops.hpp>
+
+ namespace boost
+ {
+
+ namespace detail
+ {
+
+ class lightweight_mutex
+ {
+ private:
+
+ long a_;
+
+ lightweight_mutex(lightweight_mutex const &);
+ lightweight_mutex & operator=(lightweight_mutex const &);
+
+ public:
+
+ lightweight_mutex() : a_(1)
+ {
+ }
+
+ class scoped_lock;
+ friend class scoped_lock;
+
+ class scoped_lock
+ {
+ private:
+
+ lightweight_mutex & m_;
+
+ scoped_lock(scoped_lock const &);
+ scoped_lock & operator=(scoped_lock const &);
+
+ public:
+
+ explicit scoped_lock(lightweight_mutex & m): m_(m)
+ {
+ while( !atomic_dec_and_test(&m_.a_) )
+ {
+ atomic_inc(&m_.a_);
+ #if defined(BOOST_HAS_SCHED_YIELD)
+ sched_yield();
+ #endif
+ }
+ }
+
+ ~scoped_lock()
+ {
+ atomic_inc(&m_.a_);
+ }
+ };
+ };
+
+ } // namespace detail
+
+ } // namespace boost
+
+ #endif // #ifndef BOOST_DETAIL_LWM_ATOMIC_HPP_INCLUDED

-- 
Thanks,
Vadim

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com



Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk