atomic_ptr.hpp 5.26 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU General Public License as published by
    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
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


Martin Sustrik's avatar
Martin Sustrik committed
21 22
#ifndef __ZMQ_ATOMIC_PTR_HPP_INCLUDED__
#define __ZMQ_ATOMIC_PTR_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
23 24 25

#include "platform.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
26 27
#if defined ZMQ_FORCE_MUTEXES
#define ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
28
#elif (defined __i386__ || defined __x86_64__) && defined __GNUC__
Martin Sustrik's avatar
Martin Sustrik committed
29
#define ZMQ_ATOMIC_PTR_X86
Martin Sustrik's avatar
Martin Sustrik committed
30
#elif 0 && defined __sparc__ && defined __GNUC__
Martin Sustrik's avatar
Martin Sustrik committed
31 32 33
#define ZMQ_ATOMIC_PTR_SPARC
#elif defined ZMQ_HAVE_WINDOWS
#define ZMQ_ATOMIC_PTR_WINDOWS
Martin Lucina's avatar
Martin Lucina committed
34 35
#elif (defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_NETBSD)
#define ZMQ_ATOMIC_PTR_SYSTEM
Martin Sustrik's avatar
Martin Sustrik committed
36
#else
Martin Sustrik's avatar
Martin Sustrik committed
37
#define ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
38 39
#endif

Martin Sustrik's avatar
Martin Sustrik committed
40
#if defined ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
41
#include "mutex.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
42
#elif defined ZMQ_ATOMIC_PTR_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
43
#include "windows.hpp"
Martin Lucina's avatar
Martin Lucina committed
44
#elif defined ZMQ_ATOMIC_PTR_SYSTEM
Martin Sustrik's avatar
Martin Sustrik committed
45 46 47
#include <atomic.h>
#endif

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

    //  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
80
#if defined ZMQ_ATOMIC_PTR_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
81
            return (T*) InterlockedExchangePointer (&ptr, val_);
Martin Lucina's avatar
Martin Lucina committed
82
#elif defined ZMQ_ATOMIC_PTR_SYSTEM
Martin Sustrik's avatar
Martin Sustrik committed
83
            return (T*) atomic_swap_ptr (&ptr, val_);
Martin Sustrik's avatar
Martin Sustrik committed
84
#elif defined ZMQ_ATOMIC_PTR_X86
Martin Sustrik's avatar
Martin Sustrik committed
85 86 87 88 89 90
            T *old;
            __asm__ volatile (
                "lock; xchg %0, %2"
                : "=r" (old), "=m" (ptr)
                : "m" (ptr), "0" (val_));
            return old;
Martin Sustrik's avatar
Martin Sustrik committed
91
#elif defined ZMQ_ATOMIC_PTR_SPARC
Martin Sustrik's avatar
Martin Sustrik committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
            T* newptr = val_;
            volatile T** ptrin = &ptr;
            T* tmp;
            T* prev;
            __asm__ __volatile__(
                "ld [%4], %1\n\t"
                "1:\n\t"
                "mov %0, %2\n\t"
                "cas [%4], %1, %2\n\t"
                "cmp %1, %2\n\t"
                "bne,a,pn %%icc, 1b\n\t"
                "mov %2, %1\n\t"
                : "+r" (newptr), "=&r" (tmp), "=&r" (prev), "+m" (*ptrin)
                : "r" (ptrin)
                : "cc");
            return prev;
Martin Sustrik's avatar
Martin Sustrik committed
108
#elif defined ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
            sync.lock ();
            T *old = (T*) ptr;
            ptr = val_;
            sync.unlock ();
            return old;
#else
#error
#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
125
#if defined ZMQ_ATOMIC_PTR_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
126 127
            return (T*) InterlockedCompareExchangePointer (
                (volatile PVOID*) &ptr, val_, cmp_);
Martin Lucina's avatar
Martin Lucina committed
128
#elif defined ZMQ_ATOMIC_PTR_SYSTEM
Martin Sustrik's avatar
Martin Sustrik committed
129
            return (T*) atomic_cas_ptr (&ptr, cmp_, val_);
Martin Sustrik's avatar
Martin Sustrik committed
130
#elif defined ZMQ_ATOMIC_PTR_X86
Martin Sustrik's avatar
Martin Sustrik committed
131 132 133 134 135 136 137
            T *old;
            __asm__ volatile (
                "lock; cmpxchg %2, %3"
                : "=a" (old), "=m" (ptr)
                : "r" (val_), "m" (ptr), "0" (cmp_)
                : "cc");
            return old;
Martin Sustrik's avatar
Martin Sustrik committed
138
#elif defined ZMQ_ATOMIC_PTR_SPARC
Martin Sustrik's avatar
Martin Sustrik committed
139 140 141 142 143 144 145 146
            volatile T** ptrin = &ptr;
            volatile T* prev = ptr;
            __asm__ __volatile__(
                "cas [%3], %1, %2\n\t"
                : "+m" (*ptrin)
                : "r" (cmp_), "r" (val_), "r" (ptrin)
                : "cc");
            return prev;
Martin Sustrik's avatar
Martin Sustrik committed
147
#elif defined ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161
            sync.lock ();
            T *old = (T*) ptr;
            if (ptr == cmp_)
                ptr = val_;
            sync.unlock ();
            return old;
#else
#error
#endif
        }

    private:

        volatile T *ptr;
Martin Sustrik's avatar
Martin Sustrik committed
162
#if defined ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
163 164 165 166 167 168 169 170 171 172
        mutex_t sync;
#endif

        atomic_ptr_t (const atomic_ptr_t&);
        void operator = (const atomic_ptr_t&);
    };

}

//  Remove macros local to this file.
Martin Sustrik's avatar
Martin Sustrik committed
173 174
#if defined ZMQ_ATOMIC_PTR_WINDOWS
#undef ZMQ_ATOMIC_PTR_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
175
#endif
Martin Lucina's avatar
Martin Lucina committed
176 177
#if defined ZMQ_ATOMIC_PTR_SYSTEM
#undef ZMQ_ATOMIC_PTR_SYSTEM
Martin Sustrik's avatar
Martin Sustrik committed
178
#endif
Martin Sustrik's avatar
Martin Sustrik committed
179 180
#if defined ZMQ_ATOMIC_PTR_X86
#undef ZMQ_ATOMIC_PTR_X86
Martin Sustrik's avatar
Martin Sustrik committed
181
#endif
Martin Sustrik's avatar
Martin Sustrik committed
182 183
#if defined ZMQ_ATOMIC_PTR_SPARC
#undef ZMQ_ATOMIC_PTR_SPARC
Martin Sustrik's avatar
Martin Sustrik committed
184
#endif
Martin Sustrik's avatar
Martin Sustrik committed
185 186
#if defined ZMQ_ATOMIC_PTR_MUTEX
#undef ZMQ_ATOMIC_PTR_MUTEX
Martin Sustrik's avatar
Martin Sustrik committed
187 188 189
#endif

#endif