00001 /* -*- C++ -*- */ 00002 00003 //============================================================================= 00004 /** 00005 * @file Atomic_Op_T.h 00006 * 00007 * $Id: Atomic_Op_T.h,v 1.1.1.1 2003/02/21 18:36:32 chad Exp $ 00008 * 00009 * @author Douglas C. Schmidt <schmidt@uci.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_ATOMIC_OP_T_H 00014 #define ACE_ATOMIC_OP_T_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/Synch.h" 00024 00025 00026 /** 00027 * @class ACE_Atomic_Op_Ex 00028 * 00029 * @brief Transparently parameterizes synchronization into basic 00030 * arithmetic operations. 00031 * 00032 * This class is described in an article in the July/August 1994 00033 * issue of the C++ Report magazine. It implements a 00034 * templatized version of the Decorator pattern from the GoF book. 00035 * 00036 * ACE_Atomic_Op_Ex objects must be constructed with a reference 00037 * to an existing lock. A single lock can be shared between 00038 * multiple ACE_Atomic_Op_Ex objects. If you do not require this 00039 * ability consider using the ACE_Atomic_Op class instead, which 00040 * may be able to take advantage of platform-specific 00041 * optimisations to provide atomic operations without requiring a 00042 * lock. 00043 */ 00044 template <class ACE_LOCK, class TYPE> 00045 class ACE_Atomic_Op_Ex 00046 { 00047 public: 00048 // = Initialization methods. 00049 00050 /// Initialize <value_> to 0. 00051 ACE_Atomic_Op_Ex (ACE_LOCK &mtx); 00052 00053 /// Initialize <value_> to c. 00054 ACE_Atomic_Op_Ex (ACE_LOCK &mtx, const TYPE &c); 00055 00056 // = Accessors. 00057 00058 /// Atomically pre-increment <value_>. 00059 TYPE operator++ (void); 00060 00061 /// Atomically post-increment <value_>. 00062 TYPE operator++ (int); 00063 00064 /// Atomically increment <value_> by rhs. 00065 TYPE operator+= (const TYPE &rhs); 00066 00067 /// Atomically pre-decrement <value_>. 00068 TYPE operator-- (void); 00069 00070 /// Atomically post-decrement <value_>. 00071 TYPE operator-- (int); 00072 00073 /// Atomically decrement <value_> by rhs. 00074 TYPE operator-= (const TYPE &rhs); 00075 00076 /// Atomically compare <value_> with rhs. 00077 int operator== (const TYPE &rhs) const; 00078 00079 /// Atomically compare <value_> with rhs. 00080 int operator!= (const TYPE &rhs) const; 00081 00082 /// Atomically check if <value_> greater than or equal to rhs. 00083 int operator>= (const TYPE &rhs) const; 00084 00085 /// Atomically check if <value_> greater than rhs. 00086 int operator> (const TYPE &rhs) const; 00087 00088 /// Atomically check if <value_> less than or equal to rhs. 00089 int operator<= (const TYPE &rhs) const; 00090 00091 /// Atomically check if <value_> less than rhs. 00092 int operator< (const TYPE &rhs) const; 00093 00094 /// Atomically assign rhs to <value_>. 00095 void operator= (const TYPE &rhs); 00096 00097 /// Atomically assign <rhs> to <value_>. 00098 void operator= (const ACE_Atomic_Op_Ex<ACE_LOCK, TYPE> &rhs); 00099 00100 /// Explicitly return <value_>. 00101 TYPE value (void) const; 00102 00103 /// Dump the state of an object. 00104 void dump (void) const; 00105 00106 // ACE_ALLOC_HOOK_DECLARE; 00107 // Declare the dynamic allocation hooks. 00108 00109 /// Manage copying... 00110 ACE_Atomic_Op_Ex (const ACE_Atomic_Op_Ex<ACE_LOCK, TYPE> &); 00111 00112 /** 00113 * Returns a reference to the underlying <ACE_LOCK>. This makes it 00114 * possible to acquire the lock explicitly, which can be useful in 00115 * some cases if you instantiate the <ACE_Atomic_Op_Ex> with an 00116 * <ACE_Recursive_Mutex> or <ACE_Process_Mutex>. NOTE: the right 00117 * name would be lock_, but HP/C++ will choke on that! 00118 */ 00119 ACE_LOCK &mutex (void); 00120 00121 /** 00122 * Explicitly return <value_> (by reference). This gives the user 00123 * full, unrestricted access to the underlying value. This method 00124 * will usually be used in conjunction with explicit access to the 00125 * lock. Use with care ;-) 00126 */ 00127 TYPE &value_i (void); 00128 00129 private: 00130 /// Type of synchronization mechanism. 00131 ACE_LOCK &mutex_; 00132 00133 /// Current object decorated by the atomic op. 00134 TYPE value_; 00135 }; 00136 00137 /** 00138 * @class ACE_Atomic_Op 00139 * 00140 * @brief Transparently parameterizes synchronization into basic 00141 * arithmetic operations. 00142 * 00143 * This class is described in an article in the July/August 1994 00144 * issue of the C++ Report magazine. It implements a 00145 * templatized version of the Decorator pattern from the GoF book. 00146 * 00147 * Certain platforms may provide a template specialization for 00148 * ACE_Atomic_Op <ACE_Thread_Mutex, long> that provides optimized 00149 * atomic integer operations without actually requiring a mutex. 00150 */ 00151 template <class ACE_LOCK, class TYPE> 00152 class ACE_Atomic_Op 00153 { 00154 public: 00155 /// Initialize <value_> to 0. 00156 ACE_Atomic_Op (void); 00157 00158 /// Initialize <value_> to c. 00159 ACE_Atomic_Op (const TYPE &c); 00160 00161 /// Manage copying... 00162 ACE_Atomic_Op (const ACE_Atomic_Op<ACE_LOCK, TYPE> &c); 00163 00164 /// Atomically assign rhs to <value_>. 00165 void operator= (const TYPE &rhs); 00166 00167 /// Atomically assign <rhs> to <value_>. 00168 void operator= (const ACE_Atomic_Op<ACE_LOCK, TYPE> &rhs); 00169 00170 /// Atomically pre-increment <value_>. 00171 TYPE operator++ (void); 00172 00173 /// Atomically post-increment <value_>. 00174 TYPE operator++ (int); 00175 00176 /// Atomically increment <value_> by rhs. 00177 TYPE operator+= (const TYPE &rhs); 00178 00179 /// Atomically pre-decrement <value_>. 00180 TYPE operator-- (void); 00181 00182 /// Atomically post-decrement <value_>. 00183 TYPE operator-- (int); 00184 00185 /// Atomically decrement <value_> by rhs. 00186 TYPE operator-= (const TYPE &rhs); 00187 00188 /// Atomically compare <value_> with rhs. 00189 int operator== (const TYPE &rhs) const; 00190 00191 /// Atomically compare <value_> with rhs. 00192 int operator!= (const TYPE &rhs) const; 00193 00194 /// Atomically check if <value_> greater than or equal to rhs. 00195 int operator>= (const TYPE &rhs) const; 00196 00197 /// Atomically check if <value_> greater than rhs. 00198 int operator> (const TYPE &rhs) const; 00199 00200 /// Atomically check if <value_> less than or equal to rhs. 00201 int operator<= (const TYPE &rhs) const; 00202 00203 /// Atomically check if <value_> less than rhs. 00204 int operator< (const TYPE &rhs) const; 00205 00206 /// Explicitly return <value_>. 00207 TYPE value (void) const; 00208 00209 /// Dump the state of an object. 00210 void dump (void) const; 00211 00212 /** 00213 * Returns a reference to the underlying <ACE_LOCK>. This makes it 00214 * possible to acquire the lock explicitly, which can be useful in 00215 * some cases if you instantiate the <ACE_Atomic_Op> with an 00216 * <ACE_Recursive_Mutex> or <ACE_Process_Mutex>. 00217 * 00218 * NOTE: This member function is deprecated and so may go away in 00219 * the future. If you need access to the underlying mutex, consider 00220 * using the ACE_Atomic_Op_Ex template instead. 00221 */ 00222 ACE_LOCK &mutex (void); 00223 00224 /** 00225 * Explicitly return <value_> (by reference). This gives the user 00226 * full, unrestricted access to the underlying value. This method 00227 * will usually be used in conjunction with explicit access to the 00228 * lock. Use with care ;-) 00229 */ 00230 TYPE &value_i (void); 00231 00232 private: 00233 /// Type of synchronization mechanism. 00234 ACE_LOCK own_mutex_; 00235 00236 /// Underlying atomic op implementation. 00237 ACE_Atomic_Op_Ex <ACE_LOCK, TYPE> impl_; 00238 }; 00239 00240 00241 #if defined (__ACE_INLINE__) 00242 #include "ace/Atomic_Op_T.i" 00243 #endif /* __ACE_INLINE__ */ 00244 00245 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00246 #include "Atomic_Op_T.cpp" 00247 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00248 00249 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00250 #pragma implementation ("Atomic_Op_T.cpp") 00251 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00252 00253 #include "ace/post.h" 00254 #endif /*ACE_ATOMIC_OP_T_H*/
1.2.14 written by Dimitri van Heesch,
© 1997-2002