2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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
|
2009-07-29 12:07:54 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-07-29 12:07:54 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#ifndef __ZMQ_ATOMIC_PTR_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_ATOMIC_PTR_HPP_INCLUDED__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#include "platform.hpp"
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#if defined ZMQ_FORCE_MUTEXES
|
|
|
|
#define ZMQ_ATOMIC_PTR_MUTEX
|
2015-01-28 10:27:15 -06:00
|
|
|
#elif defined ZMQ_HAVE_ATOMIC_INTRINSICS
|
|
|
|
#define ZMQ_ATOMIC_PTR_INTRINSIC
|
2015-06-30 20:48:29 +01:00
|
|
|
#elif (defined ZMQ_CXX11 && defined __cplusplus && __cplusplus >= 201103L)
|
2015-07-02 17:41:09 +01:00
|
|
|
#define ZMQ_ATOMIC_PTR_CXX11
|
2009-07-29 12:07:54 +02:00
|
|
|
#elif (defined __i386__ || defined __x86_64__) && defined __GNUC__
|
2009-08-03 11:30:13 +02:00
|
|
|
#define ZMQ_ATOMIC_PTR_X86
|
2012-04-13 10:48:15 -05:00
|
|
|
#elif defined __ARM_ARCH_7A__ && defined __GNUC__
|
|
|
|
#define ZMQ_ATOMIC_PTR_ARM
|
2013-03-20 12:54:38 -04:00
|
|
|
#elif defined __tile__
|
|
|
|
#define ZMQ_ATOMIC_PTR_TILE
|
2009-08-03 11:30:13 +02:00
|
|
|
#elif defined ZMQ_HAVE_WINDOWS
|
|
|
|
#define ZMQ_ATOMIC_PTR_WINDOWS
|
2016-01-16 02:26:20 +01:00
|
|
|
#elif (defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_NETBSD || defined ZMQ_HAVE_GNU)
|
2010-06-04 15:35:14 +02:00
|
|
|
#define ZMQ_ATOMIC_PTR_ATOMIC_H
|
2009-07-29 12:07:54 +02:00
|
|
|
#else
|
2009-08-03 11:30:13 +02:00
|
|
|
#define ZMQ_ATOMIC_PTR_MUTEX
|
2009-07-29 12:07:54 +02:00
|
|
|
#endif
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#if defined ZMQ_ATOMIC_PTR_MUTEX
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "mutex.hpp"
|
2015-07-02 17:41:09 +01:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_CXX11
|
2015-06-30 20:48:29 +01:00
|
|
|
#include <atomic>
|
2009-08-03 11:30:13 +02:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_WINDOWS
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "windows.hpp"
|
2010-05-12 16:46:59 +02:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_ATOMIC_H
|
2009-07-29 12:07:54 +02:00
|
|
|
#include <atomic.h>
|
2013-03-20 12:54:38 -04:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_TILE
|
|
|
|
#include <arch/atomic.h>
|
2009-07-29 12:07:54 +02:00
|
|
|
#endif
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
namespace zmq
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
// 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_)
|
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
#if defined ZMQ_ATOMIC_PTR_WINDOWS
|
2010-04-15 07:32:25 +02:00
|
|
|
return (T*) InterlockedExchangePointer ((PVOID*) &ptr, val_);
|
2015-01-28 10:27:15 -06:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_INTRINSIC
|
|
|
|
return (T*) __atomic_exchange_n (&ptr, val_, __ATOMIC_ACQ_REL);
|
2015-07-02 17:41:09 +01:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_CXX11
|
2015-06-30 20:48:29 +01:00
|
|
|
return ptr.exchange(val_, std::memory_order_acq_rel);
|
2010-05-12 16:46:59 +02:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_ATOMIC_H
|
2009-07-29 12:07:54 +02:00
|
|
|
return (T*) atomic_swap_ptr (&ptr, val_);
|
2013-03-20 12:54:38 -04:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_TILE
|
|
|
|
return (T*) arch_atomic_exchange (&ptr, val_);
|
2009-08-03 11:30:13 +02:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_X86
|
2009-07-29 12:07:54 +02:00
|
|
|
T *old;
|
|
|
|
__asm__ volatile (
|
|
|
|
"lock; xchg %0, %2"
|
|
|
|
: "=r" (old), "=m" (ptr)
|
|
|
|
: "m" (ptr), "0" (val_));
|
|
|
|
return old;
|
2012-04-13 10:48:15 -05:00
|
|
|
#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;
|
2009-08-03 11:30:13 +02:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_MUTEX
|
2009-07-29 12:07:54 +02:00
|
|
|
sync.lock ();
|
|
|
|
T *old = (T*) ptr;
|
|
|
|
ptr = val_;
|
|
|
|
sync.unlock ();
|
|
|
|
return old;
|
|
|
|
#else
|
2010-05-12 16:46:59 +02:00
|
|
|
#error atomic_ptr is not implemented for this platform
|
2009-07-29 12:07:54 +02:00
|
|
|
#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_)
|
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
#if defined ZMQ_ATOMIC_PTR_WINDOWS
|
2009-07-29 12:07:54 +02:00
|
|
|
return (T*) InterlockedCompareExchangePointer (
|
|
|
|
(volatile PVOID*) &ptr, val_, cmp_);
|
2015-01-28 10:27:15 -06:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_INTRINSIC
|
|
|
|
T *old = cmp_;
|
2015-01-28 16:38:14 -06:00
|
|
|
__atomic_compare_exchange_n (&ptr, (volatile T**) &old, val_, false,
|
2015-01-28 10:27:15 -06:00
|
|
|
__ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
|
|
|
|
return old;
|
2015-07-02 17:41:09 +01:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_CXX11
|
2015-06-30 22:26:35 +01:00
|
|
|
ptr.compare_exchange_strong(cmp_, val_, std::memory_order_acq_rel);
|
2015-06-30 20:48:29 +01:00
|
|
|
return cmp_;
|
2010-05-12 16:46:59 +02:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_ATOMIC_H
|
2009-07-29 12:07:54 +02:00
|
|
|
return (T*) atomic_cas_ptr (&ptr, cmp_, val_);
|
2013-03-20 12:54:38 -04:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_TILE
|
|
|
|
return (T*) arch_atomic_val_compare_and_exchange (&ptr, cmp_, val_);
|
2009-08-03 11:30:13 +02:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_X86
|
2009-07-29 12:07:54 +02:00
|
|
|
T *old;
|
|
|
|
__asm__ volatile (
|
|
|
|
"lock; cmpxchg %2, %3"
|
|
|
|
: "=a" (old), "=m" (ptr)
|
|
|
|
: "r" (val_), "m" (ptr), "0" (cmp_)
|
|
|
|
: "cc");
|
|
|
|
return old;
|
2012-04-13 10:48:15 -05:00
|
|
|
#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"
|
2012-08-23 12:49:21 -07:00
|
|
|
" it eq\n\t"
|
2012-04-13 10:48:15 -05:00
|
|
|
" 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;
|
2009-08-03 11:30:13 +02:00
|
|
|
#elif defined ZMQ_ATOMIC_PTR_MUTEX
|
2009-07-29 12:07:54 +02:00
|
|
|
sync.lock ();
|
|
|
|
T *old = (T*) ptr;
|
|
|
|
if (ptr == cmp_)
|
|
|
|
ptr = val_;
|
|
|
|
sync.unlock ();
|
|
|
|
return old;
|
|
|
|
#else
|
2010-05-12 16:46:59 +02:00
|
|
|
#error atomic_ptr is not implemented for this platform
|
2009-07-29 12:07:54 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-07-02 17:41:09 +01:00
|
|
|
#if defined ZMQ_ATOMIC_PTR_CXX11
|
2015-06-30 20:48:29 +01:00
|
|
|
std::atomic<T*> ptr;
|
|
|
|
#else
|
2009-07-29 12:07:54 +02:00
|
|
|
volatile T *ptr;
|
2015-06-30 20:48:29 +01:00
|
|
|
#endif
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#if defined ZMQ_ATOMIC_PTR_MUTEX
|
2009-07-29 12:07:54 +02:00
|
|
|
mutex_t sync;
|
|
|
|
#endif
|
|
|
|
|
2015-07-02 17:41:09 +01:00
|
|
|
#if ! defined ZMQ_ATOMIC_PTR_CXX11
|
2009-07-29 12:07:54 +02:00
|
|
|
atomic_ptr_t (const atomic_ptr_t&);
|
2011-01-13 11:44:23 +01:00
|
|
|
const atomic_ptr_t &operator = (const atomic_ptr_t&);
|
2015-06-30 20:48:29 +01:00
|
|
|
#endif
|
2009-07-29 12:07:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove macros local to this file.
|
2015-06-30 22:56:58 +01:00
|
|
|
#undef ZMQ_ATOMIC_PTR_MUTEX
|
|
|
|
#undef ZMQ_ATOMIC_PTR_INTRINSIC
|
2015-07-02 17:41:09 +01:00
|
|
|
#undef ZMQ_ATOMIC_PTR_CXX11
|
2009-08-03 11:30:13 +02:00
|
|
|
#undef ZMQ_ATOMIC_PTR_X86
|
2012-04-13 10:48:15 -05:00
|
|
|
#undef ZMQ_ATOMIC_PTR_ARM
|
2015-06-30 22:56:58 +01:00
|
|
|
#undef ZMQ_ATOMIC_PTR_TILE
|
|
|
|
#undef ZMQ_ATOMIC_PTR_WINDOWS
|
|
|
|
#undef ZMQ_ATOMIC_PTR_ATOMIC_H
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#endif
|