File: Atomic_Op_T.h

package info (click to toggle)
ace 6.4.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 48,640 kB
  • ctags: 41,204
  • sloc: cpp: 336,448; perl: 33,068; ansic: 20,676; sh: 3,735; exp: 787; python: 635; yacc: 511; xml: 330; lex: 158; lisp: 116; makefile: 80; csh: 20; tcl: 5
file content (360 lines) | stat: -rw-r--r-- 8,683 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// -*- C++ -*-

//=============================================================================
/**
 *  @file    Atomic_Op_T.h
 *
 *  @author Douglas C. Schmidt <schmidt@uci.edu>
 */
//=============================================================================

#ifndef ACE_ATOMIC_OP_T_H
#define ACE_ATOMIC_OP_T_H
#include /**/ "ace/pre.h"

#include /**/ "ace/config-all.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

template<typename TYPE>
struct ACE_Type_Traits
{
  typedef TYPE const & parameter_type;
};

template<>
struct ACE_Type_Traits<bool>
{
  typedef bool parameter_type;
};

template<>
struct ACE_Type_Traits<char>
{
  typedef char parameter_type;
};

template<>
struct ACE_Type_Traits<signed char>
{
  typedef signed char parameter_type;
};

template<>
struct ACE_Type_Traits<unsigned char>
{
  typedef unsigned char parameter_type;
};

template<>
struct ACE_Type_Traits<short>
{
  typedef short parameter_type;
};

template<>
struct ACE_Type_Traits<unsigned short>
{
  typedef unsigned short parameter_type;
};

template<>
struct ACE_Type_Traits<int>
{
  typedef int parameter_type;
};

template<>
struct ACE_Type_Traits<unsigned int>
{
  typedef unsigned int parameter_type;
};

template<>
struct ACE_Type_Traits<long>
{
  typedef long parameter_type;
};

template<>
struct ACE_Type_Traits<unsigned long>
{
  typedef unsigned long parameter_type;
};

template<>
struct ACE_Type_Traits<long long>
{
  typedef long long parameter_type;
};

template<>
struct ACE_Type_Traits<unsigned long long>
{
  typedef unsigned long long parameter_type;
};

template<>
struct ACE_Type_Traits<float>
{
  typedef float parameter_type;
};

template<>
struct ACE_Type_Traits<double>
{
  typedef double parameter_type;
};

template<>
struct ACE_Type_Traits<long double>
{
  typedef long double parameter_type;
};

template<typename TYPE>
struct ACE_Type_Traits<TYPE*>
{
  typedef TYPE* parameter_type;
};

/**
 * @class ACE_Atomic_Op_Ex
 *
 * @brief Transparently parameterizes synchronization into basic
 * arithmetic operations.
 *
 * This class is described in an article in the July/August 1994
 * issue of the C++ Report magazine.  It implements a
 * templatized version of the Decorator pattern from the GoF book.
 *
 * ACE_Atomic_Op_Ex objects must be constructed with a reference
 * to an existing lock. A single lock can be shared between
 * multiple ACE_Atomic_Op_Ex objects. If you do not require this
 * ability consider using the ACE_Atomic_Op class instead, which
 * may be able to take advantage of platform-specific
 * optimisations to provide atomic operations without requiring a
 * lock.
 */
template <class ACE_LOCK, typename TYPE>
class ACE_Atomic_Op_Ex
{
public:

  typedef typename ACE_Type_Traits<TYPE>::parameter_type arg_type;

  // = Initialization methods.

  /// Initialize @c value_ to 0.
  ACE_Atomic_Op_Ex (ACE_LOCK & mtx);

  /// Initialize @c value_ to c.
  ACE_Atomic_Op_Ex (ACE_LOCK & mtx, arg_type c);

  // = Accessors.

  /// Atomically pre-increment @c value_.
  TYPE operator++ (void);

  /// Atomically post-increment @c value_.
  TYPE operator++ (int);

  /// Atomically increment @c value_ by rhs.
  TYPE operator+= (arg_type rhs);

  /// Atomically pre-decrement @c value_.
  TYPE operator-- (void);

  /// Atomically post-decrement @c value_.
  TYPE operator-- (int);

  /// Atomically decrement @c value_ by rhs.
  TYPE operator-= (arg_type rhs);

  /// Atomically compare @c value_ with rhs.
  bool operator== (arg_type rhs) const;

  /// Atomically compare @c value_ with rhs.
  bool operator!= (arg_type rhs) const;

  /// Atomically check if @c value_ greater than or equal to rhs.
  bool operator>= (arg_type rhs) const;

  /// Atomically check if @c value_ greater than rhs.
  bool operator> (arg_type rhs) const;

  /// Atomically check if @c value_ less than or equal to rhs.
  bool operator<= (arg_type rhs) const;

  /// Atomically check if @c value_ less than rhs.
  bool operator< (arg_type rhs) const;

  /// Atomically assign rhs to @c value_.
  ACE_Atomic_Op_Ex<ACE_LOCK, TYPE> &operator= (arg_type rhs);

  /// Atomically assign <rhs> to @c value_.
  ACE_Atomic_Op_Ex<ACE_LOCK, TYPE> &operator= (
    ACE_Atomic_Op_Ex<ACE_LOCK, TYPE> const & rhs);

  /// Exchange value with @a newval.
  TYPE exchange (TYPE newval);

  /// Explicitly return @c value_.
  TYPE value (void) const;

  /// Dump the state of an object.
  void dump (void) const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

  /// Manage copying...
  ACE_Atomic_Op_Ex (ACE_Atomic_Op_Ex<ACE_LOCK, TYPE> const &);

  /**
   * Returns a reference to the underlying ACE_LOCK.  This makes it
   * possible to acquire the lock explicitly, which can be useful in
   * some cases if you instantiate the ACE_Atomic_Op_Ex with an
   * ACE_Recursive_Mutex or ACE_Process_Mutex.
   *
   * @note The right name would be lock_, but HP/C++ will choke on that!
   */
  ACE_LOCK & mutex (void);

  /**
   * Explicitly return @c value_ (by reference).  This gives the user
   * full, unrestricted access to the underlying value.  This method
   * will usually be used in conjunction with explicit access to the
   * lock.  Use with care ;-)
   */
  TYPE & value_i (void);

private:
  /// Type of synchronization mechanism.
  ACE_LOCK & mutex_;

  /// Current object decorated by the atomic op.
  TYPE value_;
};

/**
 * @class ACE_Atomic_Op
 *
 * @brief Transparently parameterizes synchronization into basic
 * arithmetic operations.
 *
 * This class is described in an article in the July/August 1994
 * issue of the C++ Report magazine.  It implements a
 * templatized version of the Decorator pattern from the GoF book.
 *
 * Certain platforms may provide a template specialization for
 * ACE_Atomic_Op <ACE_Thread_Mutex, long> that provides optimized
 * atomic integer operations without actually requiring a mutex.
 */
template <class ACE_LOCK, typename TYPE>
class ACE_Atomic_Op
{
public:

  typedef typename ACE_Type_Traits<TYPE>::parameter_type arg_type;

  /// Initialize @c value_ to 0.
  ACE_Atomic_Op (void);

  /// Initialize @c value_ to c.
  ACE_Atomic_Op (arg_type c);

  /// Manage copying...
  ACE_Atomic_Op (ACE_Atomic_Op<ACE_LOCK, TYPE> const & c);

  /// Atomically assign @a rhs to @c value_.
  ACE_Atomic_Op<ACE_LOCK, TYPE> & operator= (arg_type rhs);

  /// Atomically assign @a rhs to @c value_.
  ACE_Atomic_Op<ACE_LOCK, TYPE> & operator= (
    ACE_Atomic_Op<ACE_LOCK, TYPE> const & rhs);

  /// Atomically pre-increment @c value_.
  TYPE operator++ (void);

  /// Atomically post-increment @c value_.
  TYPE operator++ (int);

  /// Atomically increment @c value_ by rhs.
  TYPE operator+= (arg_type rhs);

  /// Atomically pre-decrement @c value_.
  TYPE operator-- (void);

  /// Atomically post-decrement @c value_.
  TYPE operator-- (int);

  /// Atomically decrement @c value_ by @a rhs.
  TYPE operator-= (arg_type rhs);

  /// Atomically compare @c value_ with @a rhs.
  bool operator== (arg_type rhs) const;

  /// Atomically compare @c value_ with @a rhs.
  bool operator!= (arg_type rhs) const;

  /// Atomically check if @c value_ greater than or equal to @a rhs.
  bool operator>= (arg_type rhs) const;

  /// Atomically check if @c value_ greater than @a rhs.
  bool operator> (arg_type rhs) const;

  /// Atomically check if @c value_ less than or equal to @a rhs.
  bool operator<= (arg_type rhs) const;

  /// Atomically check if @c value_ less than @a rhs.
  bool operator< (arg_type rhs) const;

  /// Exchange value with @a newval.
  TYPE exchange (TYPE newval);

  /// Explicitly return @c value_.
  TYPE value (void) const;

  /// Dump the state of an object.
  void dump (void) const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

  /**
   * Explicitly return @c value_ (by reference).  This gives the user
   * full, unrestricted access to the underlying value.  This method
   * will usually be used in conjunction with explicit access to the
   * lock.  Use with care ;-)
   */
  TYPE & value_i (void);

private:
  /// Type of synchronization mechanism.
  ACE_LOCK own_mutex_;

  /// Underlying atomic op implementation.
  ACE_Atomic_Op_Ex <ACE_LOCK, TYPE> impl_;
};

ACE_END_VERSIONED_NAMESPACE_DECL

#if defined (__ACE_INLINE__)
#include "ace/Atomic_Op_T.inl"
#endif /* __ACE_INLINE__ */

#if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#include "ace/Atomic_Op_T.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#pragma implementation ("Atomic_Op_T.cpp")
#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */

#include /**/ "ace/post.h"
#endif /*ACE_ATOMIC_OP_T_H*/