atomic_counter.hpp 7.75 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
Martin Sustrik's avatar
Martin Sustrik committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

Martin Sustrik's avatar
Martin Sustrik committed
30 31
#ifndef __ZMQ_ATOMIC_COUNTER_HPP_INCLUDED__
#define __ZMQ_ATOMIC_COUNTER_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
32 33 34

#include "stdint.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
35 36
#if defined ZMQ_FORCE_MUTEXES
#define ZMQ_ATOMIC_COUNTER_MUTEX
37
#elif defined ZMQ_HAVE_ATOMIC_INTRINSICS
38
#define ZMQ_ATOMIC_COUNTER_INTRINSIC
39 40
#elif (defined ZMQ_CXX11 && defined __cplusplus && __cplusplus >= 201103L)
#define ZMQ_ATOMIC_COUNTER_CXX11
Martin Sustrik's avatar
Martin Sustrik committed
41
#elif (defined __i386__ || defined __x86_64__) && defined __GNUC__
Martin Sustrik's avatar
Martin Sustrik committed
42
#define ZMQ_ATOMIC_COUNTER_X86
43 44
#elif defined __ARM_ARCH_7A__ && defined __GNUC__
#define ZMQ_ATOMIC_COUNTER_ARM
Martin Sustrik's avatar
Martin Sustrik committed
45 46
#elif defined ZMQ_HAVE_WINDOWS
#define ZMQ_ATOMIC_COUNTER_WINDOWS
47
#elif (defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_NETBSD || defined ZMQ_HAVE_GNU)
48
#define ZMQ_ATOMIC_COUNTER_ATOMIC_H
49 50
#elif defined __tile__
#define ZMQ_ATOMIC_COUNTER_TILE
Martin Sustrik's avatar
Martin Sustrik committed
51
#else
Martin Sustrik's avatar
Martin Sustrik committed
52
#define ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
53 54
#endif

Martin Sustrik's avatar
Martin Sustrik committed
55
#if defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
56
#include "mutex.hpp"
57 58
#elif defined ZMQ_ATOMIC_COUNTER_CXX11
#include <atomic>
Martin Sustrik's avatar
Martin Sustrik committed
59
#elif defined ZMQ_ATOMIC_COUNTER_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
60
#include "windows.hpp"
61
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
62
#include <atomic.h>
63 64
#elif defined ZMQ_ATOMIC_COUNTER_TILE
#include <arch/atomic.h>
Martin Sustrik's avatar
Martin Sustrik committed
65 66
#endif

Martin Sustrik's avatar
Martin Sustrik committed
67
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
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
{

    //  This class represents an integer that can be incremented/decremented
    //  in atomic fashion.

    class atomic_counter_t
    {
    public:

        typedef uint32_t integer_t;

        inline atomic_counter_t (integer_t value_ = 0) :
            value (value_)
        {
        }

        inline ~atomic_counter_t ()
        {
        }

        //  Set counter value (not thread-safe).
        inline void set (integer_t value_)
        {
            value = value_;
        }

        //  Atomic addition. Returns the old value.
        inline integer_t add (integer_t increment_)
        {
            integer_t old_value;

Martin Sustrik's avatar
Martin Sustrik committed
99
#if defined ZMQ_ATOMIC_COUNTER_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
100
            old_value = InterlockedExchangeAdd ((LONG*) &value, increment_);
101
#elif defined ZMQ_ATOMIC_COUNTER_INTRINSIC
102
            old_value = __atomic_fetch_add(&value, increment_, __ATOMIC_ACQ_REL);
103 104
#elif defined ZMQ_ATOMIC_COUNTER_CXX11
            old_value = value.fetch_add(increment_, std::memory_order_acq_rel);
105
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
106 107
            integer_t new_value = atomic_add_32_nv (&value, increment_);
            old_value = new_value - increment_;
108
#elif defined ZMQ_ATOMIC_COUNTER_TILE
109
            old_value = arch_atomic_add (&value, increment_);
Martin Sustrik's avatar
Martin Sustrik committed
110
#elif defined ZMQ_ATOMIC_COUNTER_X86
Martin Sustrik's avatar
Martin Sustrik committed
111
            __asm__ volatile (
112
                "lock; xadd %0, %1 \n\t"
Martin Sustrik's avatar
Martin Sustrik committed
113 114 115
                : "=r" (old_value), "=m" (value)
                : "0" (increment_), "m" (value)
                : "cc", "memory");
116 117 118 119 120 121 122 123 124 125 126 127 128
#elif defined ZMQ_ATOMIC_COUNTER_ARM
            integer_t flag, tmp;
            __asm__ volatile (
                "       dmb     sy\n\t"
                "1:     ldrex   %0, [%5]\n\t"
                "       add     %2, %0, %4\n\t"
                "       strex   %1, %2, [%5]\n\t"
                "       teq     %1, #0\n\t"
                "       bne     1b\n\t"
                "       dmb     sy\n\t"
                : "=&r"(old_value), "=&r"(flag), "=&r"(tmp), "+Qo"(value)
                : "Ir"(increment_), "r"(&value)
                : "cc");
Martin Sustrik's avatar
Martin Sustrik committed
129
#elif defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
130 131 132 133 134
            sync.lock ();
            old_value = value;
            value += increment_;
            sync.unlock ();
#else
135
#error atomic_counter is not implemented for this platform
Martin Sustrik's avatar
Martin Sustrik committed
136 137 138 139 140 141 142
#endif
            return old_value;
        }

        //  Atomic subtraction. Returns false if the counter drops to zero.
        inline bool sub (integer_t decrement)
        {
Martin Sustrik's avatar
Martin Sustrik committed
143
#if defined ZMQ_ATOMIC_COUNTER_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
144 145 146
            LONG delta = - ((LONG) decrement);
            integer_t old = InterlockedExchangeAdd ((LONG*) &value, delta);
            return old - decrement != 0;
147
#elif defined ZMQ_ATOMIC_COUNTER_INTRINSIC
148 149
            integer_t nv = __atomic_sub_fetch(&value, decrement, __ATOMIC_ACQ_REL);
            return nv != 0;
150 151 152
#elif defined ZMQ_ATOMIC_COUNTER_CXX11
            integer_t old = value.fetch_sub(decrement, std::memory_order_acq_rel);
            return old - decrement != 0;
153
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
154 155 156
            int32_t delta = - ((int32_t) decrement);
            integer_t nv = atomic_add_32_nv (&value, delta);
            return nv != 0;
157 158 159 160
#elif defined ZMQ_ATOMIC_COUNTER_TILE
            int32_t delta = - ((int32_t) decrement);
            integer_t nv = arch_atomic_add (&value, delta);
            return nv != 0;
Martin Sustrik's avatar
Martin Sustrik committed
161
#elif defined ZMQ_ATOMIC_COUNTER_X86
Martin Sustrik's avatar
Martin Sustrik committed
162 163 164 165 166
            integer_t oldval = -decrement;
            volatile integer_t *val = &value;
            __asm__ volatile ("lock; xaddl %0,%1"
                : "=r" (oldval), "=m" (*val)
                : "0" (oldval), "m" (*val)
167
                : "cc", "memory");
Martin Sustrik's avatar
Martin Sustrik committed
168
            return oldval != decrement;
169 170 171 172 173 174 175 176 177 178 179 180 181 182
#elif defined ZMQ_ATOMIC_COUNTER_ARM
            integer_t old_value, flag, tmp;
            __asm__ volatile (
                "       dmb     sy\n\t"
                "1:     ldrex   %0, [%5]\n\t"
                "       sub     %2, %0, %4\n\t"
                "       strex   %1, %2, [%5]\n\t"
                "       teq     %1, #0\n\t"
                "       bne     1b\n\t"
                "       dmb     sy\n\t"
                : "=&r"(old_value), "=&r"(flag), "=&r"(tmp), "+Qo"(value)
                : "Ir"(decrement), "r"(&value)
                : "cc");
            return old_value - decrement != 0;
Martin Sustrik's avatar
Martin Sustrik committed
183
#elif defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
184 185 186 187 188 189
            sync.lock ();
            value -= decrement;
            bool result = value ? true : false;
            sync.unlock ();
            return result;
#else
190
#error atomic_counter is not implemented for this platform
Martin Sustrik's avatar
Martin Sustrik committed
191 192 193
#endif
        }

Jens Auer's avatar
Jens Auer committed
194
        inline integer_t get () const
Martin Sustrik's avatar
Martin Sustrik committed
195 196 197 198 199 200
        {
            return value;
        }

    private:

201 202 203
#if defined ZMQ_ATOMIC_COUNTER_CXX11
        std::atomic<integer_t> value;
#else
Martin Sustrik's avatar
Martin Sustrik committed
204
        volatile integer_t value;
205 206
#endif

Martin Sustrik's avatar
Martin Sustrik committed
207
#if defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
208 209 210
        mutex_t sync;
#endif

211
#if ! defined ZMQ_ATOMIC_COUNTER_CXX11
Martin Sustrik's avatar
Martin Sustrik committed
212
        atomic_counter_t (const atomic_counter_t&);
213
        const atomic_counter_t& operator = (const atomic_counter_t&);
214
#endif
Martin Sustrik's avatar
Martin Sustrik committed
215 216 217 218 219
    };

}

//  Remove macros local to this file.
220
#undef ZMQ_ATOMIC_COUNTER_MUTEX
221
#undef ZMQ_ATOMIC_COUNTER_INTRINSIC
222
#undef ZMQ_ATOMIC_COUNTER_CXX11
Martin Sustrik's avatar
Martin Sustrik committed
223
#undef ZMQ_ATOMIC_COUNTER_X86
224
#undef ZMQ_ATOMIC_COUNTER_ARM
225 226 227
#undef ZMQ_ATOMIC_COUNTER_WINDOWS
#undef ZMQ_ATOMIC_COUNTER_ATOMIC_H
#undef ZMQ_ATOMIC_COUNTER_TILE
Martin Sustrik's avatar
Martin Sustrik committed
228 229

#endif