atomic_counter.hpp 7.77 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 35

#include "stdint.hpp"
#include "platform.hpp"

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

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

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

    //  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
100
#if defined ZMQ_ATOMIC_COUNTER_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
101
            old_value = InterlockedExchangeAdd ((LONG*) &value, increment_);
102
#elif defined ZMQ_ATOMIC_COUNTER_INTRINSIC
103
            old_value = __atomic_fetch_add(&value, increment_, __ATOMIC_ACQ_REL);
104 105
#elif defined ZMQ_ATOMIC_COUNTER_CXX11
            old_value = value.fetch_add(increment_, std::memory_order_acq_rel);
106
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
107 108
            integer_t new_value = atomic_add_32_nv (&value, increment_);
            old_value = new_value - increment_;
109
#elif defined ZMQ_ATOMIC_COUNTER_TILE
110
            old_value = arch_atomic_add (&value, increment_);
Martin Sustrik's avatar
Martin Sustrik committed
111
#elif defined ZMQ_ATOMIC_COUNTER_X86
Martin Sustrik's avatar
Martin Sustrik committed
112
            __asm__ volatile (
113
                "lock; xadd %0, %1 \n\t"
Martin Sustrik's avatar
Martin Sustrik committed
114 115 116
                : "=r" (old_value), "=m" (value)
                : "0" (increment_), "m" (value)
                : "cc", "memory");
117 118 119 120 121 122 123 124 125 126 127 128 129
#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
130
#elif defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
131 132 133 134 135
            sync.lock ();
            old_value = value;
            value += increment_;
            sync.unlock ();
#else
136
#error atomic_counter is not implemented for this platform
Martin Sustrik's avatar
Martin Sustrik committed
137 138 139 140 141 142 143
#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
144
#if defined ZMQ_ATOMIC_COUNTER_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
145 146 147
            LONG delta = - ((LONG) decrement);
            integer_t old = InterlockedExchangeAdd ((LONG*) &value, delta);
            return old - decrement != 0;
148
#elif defined ZMQ_ATOMIC_COUNTER_INTRINSIC
149 150
            integer_t nv = __atomic_sub_fetch(&value, decrement, __ATOMIC_ACQ_REL);
            return nv != 0;
151 152 153
#elif defined ZMQ_ATOMIC_COUNTER_CXX11
            integer_t old = value.fetch_sub(decrement, std::memory_order_acq_rel);
            return old - decrement != 0;
154
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
155 156 157
            int32_t delta = - ((int32_t) decrement);
            integer_t nv = atomic_add_32_nv (&value, delta);
            return nv != 0;
158 159 160 161
#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
162
#elif defined ZMQ_ATOMIC_COUNTER_X86
Martin Sustrik's avatar
Martin Sustrik committed
163 164 165 166 167
            integer_t oldval = -decrement;
            volatile integer_t *val = &value;
            __asm__ volatile ("lock; xaddl %0,%1"
                : "=r" (oldval), "=m" (*val)
                : "0" (oldval), "m" (*val)
168
                : "cc", "memory");
Martin Sustrik's avatar
Martin Sustrik committed
169
            return oldval != decrement;
170 171 172 173 174 175 176 177 178 179 180 181 182 183
#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
184
#elif defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
185 186 187 188 189 190
            sync.lock ();
            value -= decrement;
            bool result = value ? true : false;
            sync.unlock ();
            return result;
#else
191
#error atomic_counter is not implemented for this platform
Martin Sustrik's avatar
Martin Sustrik committed
192 193 194
#endif
        }

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

    private:

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

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

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

}

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

#endif