Atomic_Op.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Atomic_Op.h
00006  *
00007  *  Atomic_Op.h,v 4.22 2005/11/24 09:48:54 ossama Exp
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@uci.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_ATOMIC_OP_H
00014 #define ACE_ATOMIC_OP_H
00015 #include /**/ "ace/pre.h"
00016 
00017 #include "ace/config-all.h"
00018 
00019 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00020 # pragma once
00021 #endif /* ACE_LACKS_PRAGMA_ONCE */
00022 
00023 #include "ace/Thread_Mutex.h"
00024 
00025 // Include the templates here.
00026 #include "ace/Atomic_Op_T.h"
00027 
00028 // Determine whether builtin atomic op support is
00029 // available on this platform.
00030 #if defined (ACE_HAS_THREADS)
00031 # if defined (WIN32)
00032 #  if defined (ACE_HAS_INTERLOCKED_EXCHANGEADD)
00033 #   define ACE_HAS_BUILTIN_ATOMIC_OP
00034 #  else /* ACE_HAS_INTERLOCKED_EXCHANGEADD */
00035     // Inline assembly emulation of InterlockedExchangeAdd
00036     // is currently only implemented for MSVC (x86 only) and Borland.
00037 #   if (defined (_MSC_VER) && defined (_M_IX86)) || defined (__BORLANDC__)
00038 #    define ACE_HAS_BUILTIN_ATOMIC_OP
00039 #   endif /* _MSC_VER || __BORLANDC__ */
00040 #  endif /* ACE_HAS_INTERLOCKED_EXCHANGEADD */
00041 # elif defined (__GNUC__) && (defined (ACE_HAS_PENTIUM) || defined (__amd64__))
00042 #  define ACE_HAS_BUILTIN_ATOMIC_OP
00043 # endif /* WIN32 */
00044 #endif /* ACE_HAS_THREADS */
00045 
00046 #if defined (ACE_HAS_BUILTIN_ATOMIC_OP)
00047 
00048 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00049 
00050 /**
00051  * @class ACE_Atomic_Op<ACE_Thread_Mutex, long>
00052  *
00053  * @brief Specialization of ACE_Atomic_Op for platforms that
00054  *        support atomic integer operations.
00055  *
00056  * Specialization of ACE_Atomic_Op for platforms that support atomic
00057  * integer operations.
00058  */
00059 template<>
00060 class ACE_Export ACE_Atomic_Op<ACE_Thread_Mutex, long>
00061 {
00062 public:
00063   /// Initialize <value_> to 0.
00064   ACE_Atomic_Op (void);
00065 
00066   /// Initialize <value_> to c.
00067   ACE_Atomic_Op (const long &c);
00068 
00069   /// Manage copying...
00070   ACE_Atomic_Op (const ACE_Atomic_Op<ACE_Thread_Mutex, long> &c);
00071 
00072   /// Atomically pre-increment <value_>.
00073   long operator++ (void);
00074 
00075   /// Atomically post-increment <value_>.
00076   long operator++ (int);
00077 
00078   /// Atomically increment <value_> by rhs.
00079   long operator+= (long rhs);
00080 
00081   /// Atomically pre-decrement <value_>.
00082   long operator-- (void);
00083 
00084   /// Atomically post-decrement <value_>.
00085   long operator-- (int);
00086 
00087   /// Atomically decrement <value_> by rhs.
00088   long operator-= (long rhs);
00089 
00090   /// Atomically compare <value_> with rhs.
00091   bool operator== (long rhs) const;
00092 
00093   /// Atomically compare <value_> with rhs.
00094   bool operator!= (long rhs) const;
00095 
00096   /// Atomically check if <value_> greater than or equal to rhs.
00097   bool operator>= (long rhs) const;
00098 
00099   /// Atomically check if <value_> greater than rhs.
00100   bool operator> (long rhs) const;
00101 
00102   /// Atomically check if <value_> less than or equal to rhs.
00103   bool operator<= (long rhs) const;
00104 
00105   /// Atomically check if <value_> less than rhs.
00106   bool operator< (long rhs) const;
00107 
00108   /// Atomically assign rhs to <value_>.
00109   ACE_Atomic_Op<ACE_Thread_Mutex, long> &operator= (long rhs);
00110 
00111   /// Atomically assign <rhs> to <value_>.
00112   ACE_Atomic_Op<ACE_Thread_Mutex, long> &operator= (const ACE_Atomic_Op<ACE_Thread_Mutex, long> &rhs);
00113 
00114   /// Explicitly return <value_>.
00115   long value (void) const;
00116 
00117   /// Dump the state of an object.
00118   void dump (void) const;
00119 
00120   /// Explicitly return <value_> (by reference).
00121   volatile long &value_i (void);
00122 
00123   // ACE_ALLOC_HOOK_DECLARE;
00124   // Declare the dynamic allocation hooks.
00125 
00126   /// Used during ACE object manager initialization to optimize the fast
00127   /// atomic op implementation according to the number of CPUs.
00128   static void init_functions (void);
00129 
00130 private:
00131 
00132   // This function cannot be supported by this template specialization.
00133   // If you need access to an underlying lock, use the ACE_Atomic_Op_Ex
00134   // template instead.
00135   ACE_Thread_Mutex &mutex (void);
00136 
00137 private:
00138 
00139   /// Current object decorated by the atomic op.
00140   volatile long value_;
00141 
00142   // Pointers to selected atomic op implementations.
00143   static long (*increment_fn_) (volatile long *);
00144   static long (*decrement_fn_) (volatile long *);
00145   static long (*exchange_fn_) (volatile long *, long);
00146   static long (*exchange_add_fn_) (volatile long *, long);
00147 };
00148 
00149 /**
00150  * @class ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>
00151  *
00152  * @brief Specialization of ACE_Atomic_Op for platforms that
00153  *        support atomic integer operations.
00154  *
00155  * Specialization of ACE_Atomic_Op for platforms that support atomic
00156  * integer operations.
00157  */
00158 template<>
00159 class ACE_Export ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>
00160 {
00161 public:
00162   /// Initialize <value_> to 0.
00163   ACE_Atomic_Op (void);
00164 
00165   /// Initialize <value_> to c.
00166   ACE_Atomic_Op (const unsigned long &c);
00167 
00168   /// Manage copying...
00169   ACE_Atomic_Op (const ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> &c);
00170 
00171   /// Atomically pre-increment <value_>.
00172   unsigned long operator++ (void);
00173 
00174   /// Atomically post-increment <value_>.
00175   unsigned long operator++ (int);
00176 
00177   /// Atomically increment <value_> by rhs.
00178   unsigned long operator+= (unsigned long rhs);
00179 
00180   /// Atomically pre-decrement <value_>.
00181   unsigned long operator-- (void);
00182 
00183   /// Atomically post-decrement <value_>.
00184   unsigned long operator-- (int);
00185 
00186   /// Atomically decrement <value_> by rhs.
00187   unsigned long operator-= (unsigned long rhs);
00188 
00189   /// Atomically compare <value_> with rhs.
00190   bool operator== (unsigned long rhs) const;
00191 
00192   /// Atomically compare <value_> with rhs.
00193   bool operator!= (unsigned long rhs) const;
00194 
00195   /// Atomically check if <value_> greater than or equal to rhs.
00196   bool operator>= (unsigned long rhs) const;
00197 
00198   /// Atomically check if <value_> greater than rhs.
00199   bool operator> (unsigned long rhs) const;
00200 
00201   /// Atomically check if <value_> less than or equal to rhs.
00202   bool operator<= (unsigned long rhs) const;
00203 
00204   /// Atomically check if <value_> less than rhs.
00205   bool operator< (unsigned long rhs) const;
00206 
00207   /// Atomically assign rhs to <value_>.
00208   ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> &operator= (unsigned long rhs);
00209 
00210   /// Atomically assign <rhs> to <value_>.
00211   ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> &operator= (const ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> &rhs);
00212 
00213   /// Explicitly return <value_>.
00214   unsigned long value (void) const;
00215 
00216   /// Dump the state of an object.
00217   void dump (void) const;
00218 
00219   /// Explicitly return <value_> (by reference).
00220   volatile unsigned long &value_i (void);
00221 
00222   // ACE_ALLOC_HOOK_DECLARE;
00223   // Declare the dynamic allocation hooks.
00224 
00225   /// Used during ACE object manager initialization to optimize the fast
00226   /// atomic op implementation according to the number of CPUs.
00227   static void init_functions (void);
00228 
00229 private:
00230 
00231   // This function cannot be supported by this template specialization.
00232   // If you need access to an underlying lock, use the ACE_Atomic_Op_Ex
00233   // template instead.
00234   ACE_Thread_Mutex &mutex (void);
00235 
00236 private:
00237 
00238   /// Current object decorated by the atomic op.
00239   volatile unsigned long value_;
00240 
00241   // Pointers to selected atomic op implementations.
00242   static long (*increment_fn_) (volatile long *);
00243   static long (*decrement_fn_) (volatile long *);
00244   static long (*exchange_fn_) (volatile long *, long);
00245   static long (*exchange_add_fn_) (volatile long *, long);
00246 };
00247 
00248 ACE_END_VERSIONED_NAMESPACE_DECL
00249 
00250 #endif /* ACE_HAS_BUILTIN_ATOMIC_OP */
00251 
00252 #if defined (__ACE_INLINE__)
00253 #include "ace/Atomic_Op.inl"
00254 #endif /* __ACE_INLINE__ */
00255 
00256 #include /**/ "ace/post.h"
00257 #endif /*ACE_ATOMIC_OP_H*/

Generated on Thu Nov 9 09:41:46 2006 for ACE by doxygen 1.3.6