Atomic_Op.h

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

Generated on Tue Feb 2 17:18:38 2010 for ACE by  doxygen 1.4.7