File: atomic.h

package info (click to toggle)
libcds 2.3.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,632 kB
  • sloc: cpp: 135,002; ansic: 7,234; perl: 243; sh: 237; makefile: 6
file content (496 lines) | stat: -rw-r--r-- 16,802 bytes parent folder | download | duplicates (3)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef CDSLIB_CXX11_ATOMIC_H
#define CDSLIB_CXX11_ATOMIC_H

#include <cds/details/defs.h>
#include <cds/user_setup/cache_line.h>

namespace cds {

/// C++11 Atomic library support
/** @anchor cds_cxx11_atomic
    \p libcds can use the following implementations of the atomics:
    - STL \p &lt;atomic&gt;. This is used by default
    - \p boost.atomic for boost 1.54 and above. To use it you should define \p CDS_USE_BOOST_ATOMIC for
      your compiler invocation, for example, for gcc specify \p -DCDS_USE_BOOST_ATOMIC
      in command line
    - \p libcds implementation of atomic operation according to C++11 standard as
      specified in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf">N3242, p.29</a>.
      \p libcds implementation is not the full standard compliant, it provides only C++ part of standard,
      for example, \p libcds has no static initialization of the atomic variables and some other C features.
      However, that imlementation is enough for the library purposes. Supported architecture: x86, amd64,
      ia64 (Itanium) 64bit, 64bit Sparc. To use \p libcds atomic you should define \p CDS_USE_LIBCDS_ATOMIC
      in the compiler command line (\p -DCDS_USE_LIBCDS_ATOMIC for gcc/clang).

      @note For Clang compiler \p libcds doesn't use native \p libc++ \p &lt;atomic&gt; due some problems.
      Instead, \p libcds atomic is used by default, or you can try to use \p boost.atomic.

      The library defines \p atomics alias for atomic namespace:
      - <tt>namespace atomics = std</tt> for STL
      - <tt>namespace atomics = boost</tt> for \p boost.atomic
      - <tt>namespace atomics = cds::cxx11_atomic</tt> for library-provided atomic implementation
*/
namespace cxx11_atomic {
}} // namespace cds::cxx11_atomic

//@cond
#if defined(CDS_USE_BOOST_ATOMIC)
    // boost atomic
#   include <boost/version.hpp>
#   if BOOST_VERSION >= 105400
#       include <boost/atomic.hpp>
        namespace atomics = boost;
#       define CDS_CXX11_ATOMIC_BEGIN_NAMESPACE namespace boost {
#       define CDS_CXX11_ATOMIC_END_NAMESPACE }
#   else
#       error "Boost version 1.54 or above is needed for boost.atomic"
#   endif
#elif defined(CDS_USE_LIBCDS_ATOMIC)
    // libcds atomic
#   include <cds/compiler/cxx11_atomic.h>
    namespace atomics = cds::cxx11_atomic;
#   define CDS_CXX11_ATOMIC_BEGIN_NAMESPACE namespace cds { namespace cxx11_atomic {
#   define CDS_CXX11_ATOMIC_END_NAMESPACE }}
#else
    // Compiler provided C++11 atomic
#   include <atomic>
    namespace atomics = std;
#   define CDS_CXX11_ATOMIC_BEGIN_NAMESPACE namespace std {
#   define CDS_CXX11_ATOMIC_END_NAMESPACE }
#endif
//@endcond

namespace cds {

    /// Atomic primitives
    /**
        This namespace contains useful primitives derived from <tt>std::atomic</tt>.
    */
    namespace atomicity {

        /// Atomic event counter.
        /**
            This class is based on <tt>std::atomic_size_t</tt>.
            It uses relaxed memory ordering \p memory_order_relaxed and may be used as a statistic counter.
        */
        class event_counter
        {
            //@cond
            atomics::atomic_size_t   m_counter;
            //@endcond

        public:
            typedef size_t      value_type  ;       ///< Type of counter

        public:
            // Initializes event counter with zero
            event_counter() noexcept
                : m_counter(size_t(0))
            {}

            /// Assign operator
            /**
                Returns \p n.
            */
            value_type operator =(
                value_type n    ///< new value of the counter
            ) noexcept
            {
                m_counter.exchange( n, atomics::memory_order_relaxed );
                return n;
            }

            /// Addition
            /**
                Returns new value of the atomic counter.
            */
            size_t operator +=(
                size_t n    ///< addendum
            ) noexcept
            {
                return m_counter.fetch_add( n, atomics::memory_order_relaxed ) + n;
            }

            /// Substraction
            /**
                Returns new value of the atomic counter.
            */
            size_t operator -=(
                size_t n    ///< subtrahend
            ) noexcept
            {
                return m_counter.fetch_sub( n, atomics::memory_order_relaxed ) - n;
            }

            /// Get current value of the counter
            operator size_t () const noexcept
            {
                return m_counter.load( atomics::memory_order_relaxed );
            }

            /// Preincrement
            size_t operator ++() noexcept
            {
                return m_counter.fetch_add( 1, atomics::memory_order_relaxed ) + 1;
            }
            /// Postincrement
            size_t operator ++(int) noexcept
            {
                return m_counter.fetch_add( 1, atomics::memory_order_relaxed );
            }

            /// Predecrement
            size_t operator --() noexcept
            {
                return m_counter.fetch_sub( 1, atomics::memory_order_relaxed ) - 1;
            }
            /// Postdecrement
            size_t operator --(int) noexcept
            {
                return m_counter.fetch_sub( 1, atomics::memory_order_relaxed );
            }

            /// Get current value of the counter
            size_t get() const noexcept
            {
                return m_counter.load( atomics::memory_order_relaxed );
            }

            /// Resets the counter to 0
            void reset() noexcept
            {
                m_counter.store( 0, atomics::memory_order_release );
            }
        };

        /// Atomic item counter
        /**
            This class is simplified interface around \p std::atomic_size_t.
            The class supports getting current value of the counter and increment/decrement its value.

            See also: improved version that eliminates false sharing - \p cache_friendly_item_counter.
        */
        class item_counter
        {
        public:
            typedef atomics::atomic_size_t   atomic_type;   ///< atomic type used
            typedef size_t counter_type;                    ///< Integral item counter type (size_t)

        private:
            //@cond
            atomic_type     m_Counter;   ///< Atomic item counter
            //@endcond

        public:
            /// Default ctor initializes the counter to zero.
            item_counter()
                : m_Counter(counter_type(0))
            {}

            /// Returns current value of the counter
            counter_type value(atomics::memory_order order = atomics::memory_order_relaxed) const
            {
                return m_Counter.load( order );
            }

            /// Same as \ref value() with relaxed memory ordering
            operator counter_type() const
            {
                return value();
            }

            /// Returns underlying atomic interface
            atomic_type& getAtomic()
            {
                return m_Counter;
            }

            /// Returns underlying atomic interface (const)
            const atomic_type& getAtomic() const
            {
                return m_Counter;
            }

            /// Increments the counter. Semantics: postincrement
            counter_type inc(atomics::memory_order order = atomics::memory_order_relaxed )
            {
                return m_Counter.fetch_add( 1, order );
            }

            /// Increments the counter. Semantics: postincrement
            counter_type inc( counter_type count, atomics::memory_order order = atomics::memory_order_relaxed )
            {
                return m_Counter.fetch_add( count, order );
            }

            /// Decrements the counter. Semantics: postdecrement
            counter_type dec(atomics::memory_order order = atomics::memory_order_relaxed)
            {
                return m_Counter.fetch_sub( 1, order );
            }

            /// Decrements the counter. Semantics: postdecrement
            counter_type dec( counter_type count, atomics::memory_order order = atomics::memory_order_relaxed )
            {
                return m_Counter.fetch_sub( count, order );
            }

            /// Preincrement
            counter_type operator ++()
            {
                return inc() + 1;
            }
            /// Postincrement
            counter_type operator ++(int)
            {
                return inc();
            }

            /// Predecrement
            counter_type operator --()
            {
                return dec() - 1;
            }
            /// Postdecrement
            counter_type operator --(int)
            {
                return dec();
            }

            /// Increment by \p count
            counter_type operator +=( counter_type count )
            {
                return inc( count ) + count;
            }

            /// Decrement by \p count
            counter_type operator -=( counter_type count )
            {
                return dec( count ) - count;
            }

            /// Resets count to 0
            void reset(atomics::memory_order order = atomics::memory_order_relaxed)
            {
                m_Counter.store( 0, order );
            }
        };

#if CDS_COMPILER == CDS_COMPILER_CLANG
    // CLang unhappy: pad1_ and pad2_ - unused private field warning
#   pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-Wunused-private-field"
#endif
        /// Atomic cache-friendly item counter
        /**
            Atomic item counter with cache-line padding to avoid false sharing.
            Adding cache-line padding before and after atomic counter eliminates the contention
            in read path of many containers and can notably improve search operations in sets/maps.
        */
        class cache_friendly_item_counter
        {
        public:
            typedef atomics::atomic_size_t   atomic_type;   ///< atomic type used
            typedef size_t counter_type;                    ///< Integral item counter type (size_t)

        private:
            //@cond
            char            pad1_[cds::c_nCacheLineSize];
            atomic_type     m_Counter;   ///< Atomic item counter
            char            pad2_[cds::c_nCacheLineSize - sizeof( atomic_type )];
            //@endcond

        public:
            /// Default ctor initializes the counter to zero.
            cache_friendly_item_counter()
                : m_Counter(counter_type(0))
            {}

            /// Returns current value of the counter
            counter_type value(atomics::memory_order order = atomics::memory_order_relaxed) const
            {
                return m_Counter.load( order );
            }

            /// Same as \ref value() with relaxed memory ordering
            operator counter_type() const
            {
                return value();
            }

            /// Returns underlying atomic interface
            atomic_type& getAtomic()
            {
                return m_Counter;
            }

            /// Returns underlying atomic interface (const)
            const atomic_type& getAtomic() const
            {
                return m_Counter;
            }

            /// Increments the counter. Semantics: postincrement
            counter_type inc(atomics::memory_order order = atomics::memory_order_relaxed )
            {
                return m_Counter.fetch_add( 1, order );
            }

            /// Increments the counter. Semantics: postincrement
            counter_type inc( counter_type count, atomics::memory_order order = atomics::memory_order_relaxed )
            {
                return m_Counter.fetch_add( count, order );
            }

            /// Decrements the counter. Semantics: postdecrement
            counter_type dec(atomics::memory_order order = atomics::memory_order_relaxed)
            {
                return m_Counter.fetch_sub( 1, order );
            }

            /// Decrements the counter. Semantics: postdecrement
            counter_type dec( counter_type count, atomics::memory_order order = atomics::memory_order_relaxed )
            {
                return m_Counter.fetch_sub( count, order );
            }

            /// Preincrement
            counter_type operator ++()
            {
                return inc() + 1;
            }
            /// Postincrement
            counter_type operator ++(int)
            {
                return inc();
            }

            /// Predecrement
            counter_type operator --()
            {
                return dec() - 1;
            }
            /// Postdecrement
            counter_type operator --(int)
            {
                return dec();
            }

            /// Increment by \p count
            counter_type operator +=( counter_type count )
            {
                return inc( count ) + count;
            }

            /// Decrement by \p count
            counter_type operator -=( counter_type count )
            {
                return dec( count ) - count;
            }

            /// Resets count to 0
            void reset(atomics::memory_order order = atomics::memory_order_relaxed)
            {
                m_Counter.store( 0, order );
            }
        };
#if CDS_COMPILER == CDS_COMPILER_CLANG
#   pragma GCC diagnostic pop
#endif

        /// Empty item counter
        /**
            This class may be used instead of \ref item_counter when you do not need full \ref item_counter interface.
            All methods of the class is empty and returns 0.

            The object of this class should not be used in data structure that behavior significantly depends on item counting
            (for example, in many hash map implementation).
        */
        class empty_item_counter {
        public:
            typedef size_t counter_type    ;  ///< Counter type
        public:
            /// Returns 0
            static counter_type value(atomics::memory_order /*order*/ = atomics::memory_order_relaxed)
            {
                return 0;
            }

            /// Same as \ref value(), always returns 0.
            operator counter_type() const
            {
                return value();
            }

            /// Dummy increment. Always returns 0
            static counter_type inc(atomics::memory_order /*order*/ = atomics::memory_order_relaxed)
            {
                return 0;
            }

            /// Dummy increment. Always returns 0
            static counter_type inc( counter_type /*count*/, atomics::memory_order /*order*/ = atomics::memory_order_relaxed )
            {
                return 0;
            }

            /// Dummy increment. Always returns 0
            static counter_type dec(atomics::memory_order /*order*/ = atomics::memory_order_relaxed)
            {
                return 0;
            }

            /// Dummy increment. Always returns 0
            static counter_type dec( counter_type /*count*/, atomics::memory_order /*order*/ = atomics::memory_order_relaxed )
            {
                return 0;
            }

            /// Dummy pre-increment. Always returns 0
            counter_type operator ++() const
            {
                return 0;
            }
            /// Dummy post-increment. Always returns 0
            counter_type operator ++(int) const
            {
                return 0;
            }

            /// Dummy pre-decrement. Always returns 0
            counter_type operator --() const
            {
                return 0;
            }
            /// Dummy post-decrement. Always returns 0
            counter_type operator --(int) const
            {
                return 0;
            }

            /// Dummy increment by \p count, always returns 0
            counter_type operator +=( counter_type count )
            {
                CDS_UNUSED( count );
                return 0;
            }

            /// Dummy decrement by \p count, always returns 0
            counter_type operator -=( counter_type count )
            {
                CDS_UNUSED( count );
                return 0;
            }

            /// Dummy function
            static void reset(atomics::memory_order /*order*/ = atomics::memory_order_relaxed)
            {}
        };
    }   // namespace atomicity
}   // namespace cds

#endif // #ifndef CDSLIB_CXX11_ATOMIC_H