atomic_counter.hpp 6.01 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2007-2009 iMatix Corporation
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
10 11 12 13 14 15
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ 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
16
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
17

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

Martin Sustrik's avatar
Martin Sustrik committed
22 23
#ifndef __ZMQ_ATOMIC_COUNTER_HPP_INCLUDED__
#define __ZMQ_ATOMIC_COUNTER_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
24 25 26 27

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

Martin Sustrik's avatar
Martin Sustrik committed
28 29
#if defined ZMQ_FORCE_MUTEXES
#define ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
30
#elif (defined __i386__ || defined __x86_64__) && defined __GNUC__
Martin Sustrik's avatar
Martin Sustrik committed
31
#define ZMQ_ATOMIC_COUNTER_X86
32 33
#elif defined __ARM_ARCH_7A__ && defined __GNUC__
#define ZMQ_ATOMIC_COUNTER_ARM
Martin Sustrik's avatar
Martin Sustrik committed
34 35
#elif defined ZMQ_HAVE_WINDOWS
#define ZMQ_ATOMIC_COUNTER_WINDOWS
36 37
#elif (defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_NETBSD)
#define ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
38
#else
Martin Sustrik's avatar
Martin Sustrik committed
39
#define ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
40 41
#endif

Martin Sustrik's avatar
Martin Sustrik committed
42
#if defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
43
#include "mutex.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
44
#elif defined ZMQ_ATOMIC_COUNTER_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
45
#include "windows.hpp"
46
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
47 48 49
#include <atomic.h>
#endif

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

    //  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
82
#if defined ZMQ_ATOMIC_COUNTER_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
83
            old_value = InterlockedExchangeAdd ((LONG*) &value, increment_);
84
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
85 86
            integer_t new_value = atomic_add_32_nv (&value, increment_);
            old_value = new_value - increment_;
Martin Sustrik's avatar
Martin Sustrik committed
87
#elif defined ZMQ_ATOMIC_COUNTER_X86
Martin Sustrik's avatar
Martin Sustrik committed
88
            __asm__ volatile (
89
                "lock; xadd %0, %1 \n\t"
Martin Sustrik's avatar
Martin Sustrik committed
90 91 92
                : "=r" (old_value), "=m" (value)
                : "0" (increment_), "m" (value)
                : "cc", "memory");
93 94 95 96 97 98 99 100 101 102 103 104 105
#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
106
#elif defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
107 108 109 110 111
            sync.lock ();
            old_value = value;
            value += increment_;
            sync.unlock ();
#else
112
#error atomic_counter is not implemented for this platform
Martin Sustrik's avatar
Martin Sustrik committed
113 114 115 116 117 118 119
#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
120
#if defined ZMQ_ATOMIC_COUNTER_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
121 122 123
            LONG delta = - ((LONG) decrement);
            integer_t old = InterlockedExchangeAdd ((LONG*) &value, delta);
            return old - decrement != 0;
124
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
125 126 127
            int32_t delta = - ((int32_t) decrement);
            integer_t nv = atomic_add_32_nv (&value, delta);
            return nv != 0;
Martin Sustrik's avatar
Martin Sustrik committed
128
#elif defined ZMQ_ATOMIC_COUNTER_X86
Martin Sustrik's avatar
Martin Sustrik committed
129 130 131 132 133
            integer_t oldval = -decrement;
            volatile integer_t *val = &value;
            __asm__ volatile ("lock; xaddl %0,%1"
                : "=r" (oldval), "=m" (*val)
                : "0" (oldval), "m" (*val)
134
                : "cc", "memory");
Martin Sustrik's avatar
Martin Sustrik committed
135
            return oldval != decrement;
136 137 138 139 140 141 142 143 144 145 146 147 148 149
#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
150
#elif defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
151 152 153 154 155 156
            sync.lock ();
            value -= decrement;
            bool result = value ? true : false;
            sync.unlock ();
            return result;
#else
157
#error atomic_counter is not implemented for this platform
Martin Sustrik's avatar
Martin Sustrik committed
158 159 160 161 162 163 164 165 166 167 168
#endif
        }

        inline integer_t get ()
        {
            return value;
        }

    private:

        volatile integer_t value;
Martin Sustrik's avatar
Martin Sustrik committed
169
#if defined ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
170 171 172 173
        mutex_t sync;
#endif

        atomic_counter_t (const atomic_counter_t&);
174
        const atomic_counter_t& operator = (const atomic_counter_t&);
Martin Sustrik's avatar
Martin Sustrik committed
175 176 177 178 179
    };

}

//  Remove macros local to this file.
Martin Sustrik's avatar
Martin Sustrik committed
180 181
#if defined ZMQ_ATOMIC_COUNTER_WINDOWS
#undef ZMQ_ATOMIC_COUNTER_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
182
#endif
183 184
#if defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
#undef ZMQ_ATOMIC_COUNTER_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
185
#endif
Martin Sustrik's avatar
Martin Sustrik committed
186 187
#if defined ZMQ_ATOMIC_COUNTER_X86
#undef ZMQ_ATOMIC_COUNTER_X86
Martin Sustrik's avatar
Martin Sustrik committed
188
#endif
189 190 191
#if defined ZMQ_ATOMIC_COUNTER_ARM
#undef ZMQ_ATOMIC_COUNTER_ARM
#endif
Martin Sustrik's avatar
Martin Sustrik committed
192 193
#if defined ZMQ_ATOMIC_COUNTER_MUTEX
#undef ZMQ_ATOMIC_COUNTER_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
194 195 196
#endif

#endif
197