atomic_ptr.hpp 7.43 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2015 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_PTR_HPP_INCLUDED__
#define __ZMQ_ATOMIC_PTR_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
32 33 34

#include "platform.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
35 36
#if defined ZMQ_FORCE_MUTEXES
#define ZMQ_ATOMIC_PTR_MUTEX
37 38
#elif defined ZMQ_HAVE_ATOMIC_INTRINSICS
#define ZMQ_ATOMIC_PTR_INTRINSIC
39
#elif (defined ZMQ_CXX11 && defined __cplusplus && __cplusplus >= 201103L)
40
#define ZMQ_ATOMIC_PTR_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_PTR_X86
43 44
#elif defined __ARM_ARCH_7A__ && defined __GNUC__
#define ZMQ_ATOMIC_PTR_ARM
45 46
#elif defined __tile__
#define ZMQ_ATOMIC_PTR_TILE
Martin Sustrik's avatar
Martin Sustrik committed
47 48
#elif defined ZMQ_HAVE_WINDOWS
#define ZMQ_ATOMIC_PTR_WINDOWS
49
#elif (defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_NETBSD)
50
#define ZMQ_ATOMIC_PTR_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
51
#else
Martin Sustrik's avatar
Martin Sustrik committed
52
#define ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
53 54
#endif

Martin Sustrik's avatar
Martin Sustrik committed
55
#if defined ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
56
#include "mutex.hpp"
57
#elif defined ZMQ_ATOMIC_PTR_CXX11
58
#include <atomic>
Martin Sustrik's avatar
Martin Sustrik committed
59
#elif defined ZMQ_ATOMIC_PTR_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
60
#include "windows.hpp"
61
#elif defined ZMQ_ATOMIC_PTR_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
62
#include <atomic.h>
63 64
#elif defined ZMQ_ATOMIC_PTR_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 encapsulates several atomic operations on pointers.

    template <typename T> class atomic_ptr_t
    {
    public:

        //  Initialise atomic pointer
        inline atomic_ptr_t ()
        {
            ptr = NULL;
        }

        //  Destroy atomic pointer
        inline ~atomic_ptr_t ()
        {
        }

        //  Set value of atomic pointer in a non-threadsafe way
        //  Use this function only when you are sure that at most one
        //  thread is accessing the pointer at the moment.
        inline void set (T *ptr_)
        {
            this->ptr = ptr_;
        }

        //  Perform atomic 'exchange pointers' operation. Pointer is set
        //  to the 'val' value. Old value is returned.
        inline T *xchg (T *val_)
        {
Martin Sustrik's avatar
Martin Sustrik committed
99
#if defined ZMQ_ATOMIC_PTR_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
100
            return (T*) InterlockedExchangePointer ((PVOID*) &ptr, val_);
101 102
#elif defined ZMQ_ATOMIC_PTR_INTRINSIC
            return (T*) __atomic_exchange_n (&ptr, val_, __ATOMIC_ACQ_REL);
103
#elif defined ZMQ_ATOMIC_PTR_CXX11
104
            return ptr.exchange(val_, std::memory_order_acq_rel);
105
#elif defined ZMQ_ATOMIC_PTR_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
106
            return (T*) atomic_swap_ptr (&ptr, val_);
107 108
#elif defined ZMQ_ATOMIC_PTR_TILE
            return (T*) arch_atomic_exchange (&ptr, val_);
Martin Sustrik's avatar
Martin Sustrik committed
109
#elif defined ZMQ_ATOMIC_PTR_X86
Martin Sustrik's avatar
Martin Sustrik committed
110 111 112 113 114 115
            T *old;
            __asm__ volatile (
                "lock; xchg %0, %2"
                : "=r" (old), "=m" (ptr)
                : "m" (ptr), "0" (val_));
            return old;
116 117 118 119 120 121 122 123 124 125 126 127 128 129
#elif defined ZMQ_ATOMIC_PTR_ARM
            T* old;
            unsigned int flag;
            __asm__ volatile (
                "       dmb     sy\n\t"
                "1:     ldrex   %1, [%3]\n\t"
                "       strex   %0, %4, [%3]\n\t"
                "       teq     %0, #0\n\t"
                "       bne     1b\n\t"
                "       dmb     sy\n\t"
                : "=&r"(flag), "=&r"(old), "+Qo"(ptr)
                : "r"(&ptr), "r"(val_)
                : "cc");
            return old;
Martin Sustrik's avatar
Martin Sustrik committed
130
#elif defined ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
131 132 133 134 135 136
            sync.lock ();
            T *old = (T*) ptr;
            ptr = val_;
            sync.unlock ();
            return old;
#else
137
#error atomic_ptr is not implemented for this platform
Martin Sustrik's avatar
Martin Sustrik committed
138 139 140 141 142 143 144 145 146
#endif
        }

        //  Perform atomic 'compare and swap' operation on the pointer.
        //  The pointer is compared to 'cmp' argument and if they are
        //  equal, its value is set to 'val'. Old value of the pointer
        //  is returned.
        inline T *cas (T *cmp_, T *val_)
        {
Martin Sustrik's avatar
Martin Sustrik committed
147
#if defined ZMQ_ATOMIC_PTR_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
148 149
            return (T*) InterlockedCompareExchangePointer (
                (volatile PVOID*) &ptr, val_, cmp_);
150 151
#elif defined ZMQ_ATOMIC_PTR_INTRINSIC
            T *old = cmp_;
152
            __atomic_compare_exchange_n (&ptr, (volatile T**) &old, val_, false,
153 154
                    __ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
            return old;
155
#elif defined ZMQ_ATOMIC_PTR_CXX11
156
            ptr.compare_exchange_strong(cmp_, val_, std::memory_order_acq_rel);
157
            return cmp_;
158
#elif defined ZMQ_ATOMIC_PTR_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
159
            return (T*) atomic_cas_ptr (&ptr, cmp_, val_);
160 161
#elif defined ZMQ_ATOMIC_PTR_TILE
            return (T*) arch_atomic_val_compare_and_exchange (&ptr, cmp_, val_);
Martin Sustrik's avatar
Martin Sustrik committed
162
#elif defined ZMQ_ATOMIC_PTR_X86
Martin Sustrik's avatar
Martin Sustrik committed
163 164 165 166 167 168 169
            T *old;
            __asm__ volatile (
                "lock; cmpxchg %2, %3"
                : "=a" (old), "=m" (ptr)
                : "r" (val_), "m" (ptr), "0" (cmp_)
                : "cc");
            return old;
170 171 172 173 174 175 176 177
#elif defined ZMQ_ATOMIC_PTR_ARM
            T *old;
            unsigned int flag;
            __asm__ volatile (
                "       dmb     sy\n\t"
                "1:     ldrex   %1, [%3]\n\t"
                "       mov     %0, #0\n\t"
                "       teq     %1, %4\n\t"
178
                "       it      eq\n\t"
179 180 181 182 183 184 185 186
                "       strexeq %0, %5, [%3]\n\t"
                "       teq     %0, #0\n\t"
                "       bne     1b\n\t"
                "       dmb     sy\n\t"
                : "=&r"(flag), "=&r"(old), "+Qo"(ptr)
                : "r"(&ptr), "r"(cmp_), "r"(val_)
                : "cc");
            return old;
Martin Sustrik's avatar
Martin Sustrik committed
187
#elif defined ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
188 189 190 191 192 193 194
            sync.lock ();
            T *old = (T*) ptr;
            if (ptr == cmp_)
                ptr = val_;
            sync.unlock ();
            return old;
#else
195
#error atomic_ptr is not implemented for this platform
Martin Sustrik's avatar
Martin Sustrik committed
196 197 198 199 200
#endif
        }

    private:

201
#if defined ZMQ_ATOMIC_PTR_CXX11
202 203
        std::atomic<T*> ptr;
#else
Martin Sustrik's avatar
Martin Sustrik committed
204
        volatile T *ptr;
205 206
#endif

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

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

}

//  Remove macros local to this file.
220 221
#undef ZMQ_ATOMIC_PTR_MUTEX
#undef ZMQ_ATOMIC_PTR_INTRINSIC
222
#undef ZMQ_ATOMIC_PTR_CXX11
Martin Sustrik's avatar
Martin Sustrik committed
223
#undef ZMQ_ATOMIC_PTR_X86
224
#undef ZMQ_ATOMIC_PTR_ARM
225 226 227
#undef ZMQ_ATOMIC_PTR_TILE
#undef ZMQ_ATOMIC_PTR_WINDOWS
#undef ZMQ_ATOMIC_PTR_ATOMIC_H
Martin Sustrik's avatar
Martin Sustrik committed
228 229

#endif