3 //=============================================================================
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
9 //=============================================================================
11 #ifndef ACE_ATOMIC_OP_T_H
12 #define ACE_ATOMIC_OP_T_H
13 #include /**/ "ace/pre.h"
15 #include /**/ "ace/config-all.h"
17 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 #endif /* ACE_LACKS_PRAGMA_ONCE */
21 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
23 template<typename TYPE
>
24 struct ACE_Type_Traits
26 typedef TYPE
const & parameter_type
;
30 struct ACE_Type_Traits
<bool>
32 typedef bool parameter_type
;
36 struct ACE_Type_Traits
<char>
38 typedef char parameter_type
;
42 struct ACE_Type_Traits
<signed char>
44 typedef signed char parameter_type
;
48 struct ACE_Type_Traits
<unsigned char>
50 typedef unsigned char parameter_type
;
54 struct ACE_Type_Traits
<short>
56 typedef short parameter_type
;
60 struct ACE_Type_Traits
<unsigned short>
62 typedef unsigned short parameter_type
;
66 struct ACE_Type_Traits
<int>
68 typedef int parameter_type
;
72 struct ACE_Type_Traits
<unsigned int>
74 typedef unsigned int parameter_type
;
78 struct ACE_Type_Traits
<long>
80 typedef long parameter_type
;
84 struct ACE_Type_Traits
<unsigned long>
86 typedef unsigned long parameter_type
;
90 struct ACE_Type_Traits
<long long>
92 typedef long long parameter_type
;
96 struct ACE_Type_Traits
<unsigned long long>
98 typedef unsigned long long parameter_type
;
102 struct ACE_Type_Traits
<float>
104 typedef float parameter_type
;
108 struct ACE_Type_Traits
<double>
110 typedef double parameter_type
;
114 struct ACE_Type_Traits
<long double>
116 typedef long double parameter_type
;
119 template<typename TYPE
>
120 struct ACE_Type_Traits
<TYPE
*>
122 typedef TYPE
* parameter_type
;
126 * @class ACE_Atomic_Op_Ex
128 * @brief Transparently parameterizes synchronization into basic
129 * arithmetic operations.
131 * This class is described in an article in the July/August 1994
132 * issue of the C++ Report magazine. It implements a
133 * templatized version of the Decorator pattern from the GoF book.
135 * ACE_Atomic_Op_Ex objects must be constructed with a reference
136 * to an existing lock. A single lock can be shared between
137 * multiple ACE_Atomic_Op_Ex objects. If you do not require this
138 * ability consider using the ACE_Atomic_Op class instead, which
139 * may be able to take advantage of platform-specific
140 * optimisations to provide atomic operations without requiring a
143 template <class ACE_LOCK
, typename TYPE
>
144 class ACE_Atomic_Op_Ex
147 typedef typename ACE_Type_Traits
<TYPE
>::parameter_type arg_type
;
149 /// Initialize @c value_ to 0.
150 ACE_Atomic_Op_Ex (ACE_LOCK
& mtx
);
152 /// Initialize @c value_ to c.
153 ACE_Atomic_Op_Ex (ACE_LOCK
& mtx
, arg_type c
);
157 /// Atomically pre-increment @c value_.
158 TYPE
operator++ (void);
160 /// Atomically post-increment @c value_.
161 TYPE
operator++ (int);
163 /// Atomically increment @c value_ by rhs.
164 TYPE
operator+= (arg_type rhs
);
166 /// Atomically pre-decrement @c value_.
167 TYPE
operator-- (void);
169 /// Atomically post-decrement @c value_.
170 TYPE
operator-- (int);
172 /// Atomically decrement @c value_ by rhs.
173 TYPE
operator-= (arg_type rhs
);
175 /// Atomically compare @c value_ with rhs.
176 bool operator== (arg_type rhs
) const;
178 /// Atomically compare @c value_ with rhs.
179 bool operator!= (arg_type rhs
) const;
181 /// Atomically check if @c value_ greater than or equal to rhs.
182 bool operator>= (arg_type rhs
) const;
184 /// Atomically check if @c value_ greater than rhs.
185 bool operator> (arg_type rhs
) const;
187 /// Atomically check if @c value_ less than or equal to rhs.
188 bool operator<= (arg_type rhs
) const;
190 /// Atomically check if @c value_ less than rhs.
191 bool operator< (arg_type rhs
) const;
193 /// Atomically assign rhs to @c value_.
194 ACE_Atomic_Op_Ex
<ACE_LOCK
, TYPE
> &operator= (arg_type rhs
);
196 /// Atomically assign <rhs> to @c value_.
197 ACE_Atomic_Op_Ex
<ACE_LOCK
, TYPE
> &operator= (
198 ACE_Atomic_Op_Ex
<ACE_LOCK
, TYPE
> const & rhs
);
200 /// Exchange value with @a newval.
201 TYPE
exchange (TYPE newval
);
203 /// Explicitly return @c value_.
204 TYPE
value (void) const;
206 /// Dump the state of an object.
207 void dump (void) const;
209 /// Declare the dynamic allocation hooks.
210 ACE_ALLOC_HOOK_DECLARE
;
212 /// Manage copying...
213 ACE_Atomic_Op_Ex (ACE_Atomic_Op_Ex
<ACE_LOCK
, TYPE
> const &);
216 * Returns a reference to the underlying ACE_LOCK. This makes it
217 * possible to acquire the lock explicitly, which can be useful in
218 * some cases if you instantiate the ACE_Atomic_Op_Ex with an
219 * ACE_Recursive_Mutex or ACE_Process_Mutex.
221 * @note The right name would be lock_, but HP/C++ will choke on that!
223 ACE_LOCK
& mutex (void);
226 * Explicitly return @c value_ (by reference). This gives the user
227 * full, unrestricted access to the underlying value. This method
228 * will usually be used in conjunction with explicit access to the
229 * lock. Use with care ;-)
231 TYPE
& value_i (void);
234 /// Type of synchronization mechanism.
237 /// Current object decorated by the atomic op.
242 * @class ACE_Atomic_Op
244 * @brief Transparently parameterizes synchronization into basic
245 * arithmetic operations.
247 * This class is described in an article in the July/August 1994
248 * issue of the C++ Report magazine. It implements a
249 * templatized version of the Decorator pattern from the GoF book.
251 * Certain platforms may provide a template specialization for
252 * ACE_Atomic_Op <ACE_Thread_Mutex, long> that provides optimized
253 * atomic integer operations without actually requiring a mutex.
255 template <class ACE_LOCK
, typename TYPE
>
259 typedef typename ACE_Type_Traits
<TYPE
>::parameter_type arg_type
;
261 /// Initialize @c value_ to 0.
262 ACE_Atomic_Op (void);
264 /// Initialize @c value_ to c.
265 ACE_Atomic_Op (arg_type c
);
267 /// Manage copying...
268 ACE_Atomic_Op (ACE_Atomic_Op
<ACE_LOCK
, TYPE
> const & c
);
270 /// Atomically assign @a rhs to @c value_.
271 ACE_Atomic_Op
<ACE_LOCK
, TYPE
> & operator= (arg_type rhs
);
273 /// Atomically assign @a rhs to @c value_.
274 ACE_Atomic_Op
<ACE_LOCK
, TYPE
> & operator= (
275 ACE_Atomic_Op
<ACE_LOCK
, TYPE
> const & rhs
);
277 /// Atomically pre-increment @c value_.
278 TYPE
operator++ (void);
280 /// Atomically post-increment @c value_.
281 TYPE
operator++ (int);
283 /// Atomically increment @c value_ by rhs.
284 TYPE
operator+= (arg_type rhs
);
286 /// Atomically pre-decrement @c value_.
287 TYPE
operator-- (void);
289 /// Atomically post-decrement @c value_.
290 TYPE
operator-- (int);
292 /// Atomically decrement @c value_ by @a rhs.
293 TYPE
operator-= (arg_type rhs
);
295 /// Atomically compare @c value_ with @a rhs.
296 bool operator== (arg_type rhs
) const;
298 /// Atomically compare @c value_ with @a rhs.
299 bool operator!= (arg_type rhs
) const;
301 /// Atomically check if @c value_ greater than or equal to @a rhs.
302 bool operator>= (arg_type rhs
) const;
304 /// Atomically check if @c value_ greater than @a rhs.
305 bool operator> (arg_type rhs
) const;
307 /// Atomically check if @c value_ less than or equal to @a rhs.
308 bool operator<= (arg_type rhs
) const;
310 /// Atomically check if @c value_ less than @a rhs.
311 bool operator< (arg_type rhs
) const;
313 /// Exchange value with @a newval.
314 TYPE
exchange (TYPE newval
);
316 /// Explicitly return @c value_.
317 TYPE
value (void) const;
319 /// Dump the state of an object.
320 void dump (void) const;
322 /// Declare the dynamic allocation hooks.
323 ACE_ALLOC_HOOK_DECLARE
;
326 * Explicitly return @c value_ (by reference). This gives the user
327 * full, unrestricted access to the underlying value. This method
328 * will usually be used in conjunction with explicit access to the
329 * lock. Use with care ;-)
331 TYPE
& value_i (void);
334 /// Type of synchronization mechanism.
337 /// Underlying atomic op implementation.
338 ACE_Atomic_Op_Ex
<ACE_LOCK
, TYPE
> impl_
;
341 ACE_END_VERSIONED_NAMESPACE_DECL
343 #if defined (__ACE_INLINE__)
344 #include "ace/Atomic_Op_T.inl"
345 #endif /* __ACE_INLINE__ */
347 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
348 #include "ace/Atomic_Op_T.cpp"
349 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
351 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
352 #pragma implementation ("Atomic_Op_T.cpp")
353 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
355 #include /**/ "ace/post.h"
356 #endif /*ACE_ATOMIC_OP_T_H*/