Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Atomic_Op_T.h
blobe3752ccbd4cbca08840fc60a075de12faaf11b5f
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Atomic_Op_T.h
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
8 */
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)
18 # 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;
29 template<>
30 struct ACE_Type_Traits<bool>
32 typedef bool parameter_type;
35 template<>
36 struct ACE_Type_Traits<char>
38 typedef char parameter_type;
41 template<>
42 struct ACE_Type_Traits<signed char>
44 typedef signed char parameter_type;
47 template<>
48 struct ACE_Type_Traits<unsigned char>
50 typedef unsigned char parameter_type;
53 template<>
54 struct ACE_Type_Traits<short>
56 typedef short parameter_type;
59 template<>
60 struct ACE_Type_Traits<unsigned short>
62 typedef unsigned short parameter_type;
65 template<>
66 struct ACE_Type_Traits<int>
68 typedef int parameter_type;
71 template<>
72 struct ACE_Type_Traits<unsigned int>
74 typedef unsigned int parameter_type;
77 template<>
78 struct ACE_Type_Traits<long>
80 typedef long parameter_type;
83 template<>
84 struct ACE_Type_Traits<unsigned long>
86 typedef unsigned long parameter_type;
89 template<>
90 struct ACE_Type_Traits<long long>
92 typedef long long parameter_type;
95 template<>
96 struct ACE_Type_Traits<unsigned long long>
98 typedef unsigned long long parameter_type;
101 template<>
102 struct ACE_Type_Traits<float>
104 typedef float parameter_type;
107 template<>
108 struct ACE_Type_Traits<double>
110 typedef double parameter_type;
113 template<>
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
141 * lock.
143 template <class ACE_LOCK, typename TYPE>
144 class ACE_Atomic_Op_Ex
146 public:
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);
155 // = Accessors.
157 /// Atomically pre-increment @c value_.
158 TYPE operator++ ();
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-- ();
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 () const;
206 /// Dump the state of an object.
207 void dump () 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 ();
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 ();
233 private:
234 /// Type of synchronization mechanism.
235 ACE_LOCK & mutex_;
237 /// Current object decorated by the atomic op.
238 TYPE value_;
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>
256 class ACE_Atomic_Op
258 public:
259 typedef typename ACE_Type_Traits<TYPE>::parameter_type arg_type;
261 /// Initialize @c value_ to 0.
262 ACE_Atomic_Op ();
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++ ();
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-- ();
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 () const;
319 /// Dump the state of an object.
320 void dump () 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 ();
333 private:
334 /// Type of synchronization mechanism.
335 ACE_LOCK own_mutex_;
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 #include "ace/Atomic_Op_T.cpp"
349 #include /**/ "ace/post.h"
350 #endif /*ACE_ATOMIC_OP_T_H*/