2009-03-16 10:40:18 +00:00
|
|
|
/**
|
|
|
|
* Many similar implementations exist. See for example libwsbm
|
|
|
|
* or the linux kernel include/atomic.h
|
|
|
|
*
|
|
|
|
* No copyright claimed on this file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
include: Add helper header to help trap includes inside extern C.
This is just to help repro and fixing these issues with any C++ compiler --
Commiting this will of course wait until all issues are addressed.
$ scons src/glsl/
scons: Reading SConscript files ...
Checking for GCC ... yes
Checking for Clang ... no
Checking for X11 (x11 xext xdamage xfixes glproto >= 1.4.13)... yes
Checking for XCB (x11-xcb xcb-glx >= 1.8.1 xcb-dri2 >= 1.8)... yes
Checking for XF86VIDMODE (xxf86vm)... yes
Checking for DRM (libdrm >= 2.4.38)... yes
Checking for UDEV (libudev >= 151)... yes
warning: LLVM disabled: not building llvmpipe
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: build/linux-x86_64-debug/glsl
Compiling src/glsl/ast_array_index.cpp ...
Compiling src/glsl/ast_expr.cpp ...
Compiling src/glsl/ast_function.cpp ...
Compiling src/glsl/ast_to_hir.cpp ...
Compiling src/glsl/ast_type.cpp ...
Compiling src/glsl/builtin_functions.cpp ...
In file included from include/c99_compat.h:28:0,
from src/mapi/u_compiler.h:4,
from src/mapi/u_thread.h:47,
from src/mapi/glapi/glapi.h:47,
from src/mesa/main/mtypes.h:42,
from src/mesa/main/errors.h:47,
from src/mesa/main/imports.h:41,
from src/mesa/main/core.h:44,
from src/glsl/builtin_functions.cpp:58:
include/no_extern_c.h:48:1: error: template with C linkage
template<class T> class _IncludeInsideExternCNotPortable;
^
In file included from include/c99_compat.h:28:0,
from include/c11/threads.h:38,
from src/mapi/u_thread.h:49,
from src/mapi/glapi/glapi.h:47,
from src/mesa/main/mtypes.h:42,
from src/mesa/main/errors.h:47,
from src/mesa/main/imports.h:41,
from src/mesa/main/core.h:44,
from src/glsl/builtin_functions.cpp:58:
include/no_extern_c.h:48:1: error: template with C linkage
template<class T> class _IncludeInsideExternCNotPortable;
^
Compiling src/glsl/builtin_types.cpp ...
Compiling src/glsl/builtin_variables.cpp ...
scons: *** [build/linux-x86_64-debug/glsl/builtin_functions.os] Error 1
scons: building terminated because of errors.
Reviewed-by: Mark Janes <mark.a.janes@intel.com>
2014-12-11 22:14:14 +00:00
|
|
|
#include "no_extern_c.h"
|
|
|
|
|
2010-02-02 15:18:01 +00:00
|
|
|
#ifndef U_ATOMIC_H
|
|
|
|
#define U_ATOMIC_H
|
2009-03-16 10:40:18 +00:00
|
|
|
|
2014-11-22 00:44:43 +00:00
|
|
|
#include <stdbool.h>
|
2017-11-03 14:19:57 +00:00
|
|
|
#include <stdint.h>
|
2014-11-22 00:44:43 +00:00
|
|
|
|
2009-03-18 11:29:01 +00:00
|
|
|
/* Favor OS-provided implementations.
|
2009-04-17 11:01:22 +01:00
|
|
|
*
|
|
|
|
* Where no OS-provided implementation is available, fall back to
|
|
|
|
* locally coded assembly, compiler intrinsic or ultimately a
|
|
|
|
* mutex-based implementation.
|
2009-03-18 11:29:01 +00:00
|
|
|
*/
|
2014-11-21 23:50:58 +00:00
|
|
|
#if defined(__sun)
|
2010-02-06 03:36:42 +00:00
|
|
|
#define PIPE_ATOMIC_OS_SOLARIS
|
2014-11-21 23:50:58 +00:00
|
|
|
#elif defined(_MSC_VER)
|
2010-02-01 16:12:44 +00:00
|
|
|
#define PIPE_ATOMIC_MSVC_INTRINSIC
|
2014-12-12 09:47:43 +00:00
|
|
|
#elif defined(__GNUC__)
|
2014-11-21 22:29:41 +00:00
|
|
|
#define PIPE_ATOMIC_GCC_INTRINSIC
|
2009-04-17 11:01:22 +01:00
|
|
|
#else
|
2010-02-01 14:23:36 +00:00
|
|
|
#error "Unsupported platform"
|
2009-04-17 11:01:22 +01:00
|
|
|
#endif
|
2009-03-18 11:29:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Implementation using GCC-provided synchronization intrinsics
|
|
|
|
*/
|
2009-04-17 16:02:34 +01:00
|
|
|
#if defined(PIPE_ATOMIC_GCC_INTRINSIC)
|
2009-03-18 11:29:01 +00:00
|
|
|
|
|
|
|
#define PIPE_ATOMIC "GCC Sync Intrinsics"
|
|
|
|
|
2016-10-04 15:06:31 +01:00
|
|
|
#if defined(USE_GCC_ATOMIC_BUILTINS)
|
|
|
|
|
|
|
|
/* The builtins with explicit memory model are available since GCC 4.7. */
|
|
|
|
#define p_atomic_set(_v, _i) __atomic_store_n((_v), (_i), __ATOMIC_RELEASE)
|
|
|
|
#define p_atomic_read(_v) __atomic_load_n((_v), __ATOMIC_ACQUIRE)
|
2021-01-28 17:55:29 +00:00
|
|
|
#define p_atomic_read_relaxed(_v) __atomic_load_n((_v), __ATOMIC_RELAXED)
|
2016-10-04 15:06:31 +01:00
|
|
|
#define p_atomic_dec_zero(v) (__atomic_sub_fetch((v), 1, __ATOMIC_ACQ_REL) == 0)
|
|
|
|
#define p_atomic_inc(v) (void) __atomic_add_fetch((v), 1, __ATOMIC_ACQ_REL)
|
|
|
|
#define p_atomic_dec(v) (void) __atomic_sub_fetch((v), 1, __ATOMIC_ACQ_REL)
|
|
|
|
#define p_atomic_add(v, i) (void) __atomic_add_fetch((v), (i), __ATOMIC_ACQ_REL)
|
|
|
|
#define p_atomic_inc_return(v) __atomic_add_fetch((v), 1, __ATOMIC_ACQ_REL)
|
|
|
|
#define p_atomic_dec_return(v) __atomic_sub_fetch((v), 1, __ATOMIC_ACQ_REL)
|
2019-12-02 20:36:39 +00:00
|
|
|
#define p_atomic_add_return(v, i) __atomic_add_fetch((v), (i), __ATOMIC_ACQ_REL)
|
2022-07-08 20:30:45 +01:00
|
|
|
#define p_atomic_fetch_add(v, i) __atomic_fetch_add((v), (i), __ATOMIC_ACQ_REL)
|
2017-11-03 14:19:57 +00:00
|
|
|
#define p_atomic_xchg(v, i) __atomic_exchange_n((v), (i), __ATOMIC_ACQ_REL)
|
|
|
|
#define PIPE_NATIVE_ATOMIC_XCHG
|
2016-10-04 15:06:31 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2010-02-02 14:43:48 +00:00
|
|
|
#define p_atomic_set(_v, _i) (*(_v) = (_i))
|
|
|
|
#define p_atomic_read(_v) (*(_v))
|
2021-01-28 17:55:29 +00:00
|
|
|
#define p_atomic_read_relaxed(_v) (*(_v))
|
2014-11-22 00:33:40 +00:00
|
|
|
#define p_atomic_dec_zero(v) (__sync_sub_and_fetch((v), 1) == 0)
|
|
|
|
#define p_atomic_inc(v) (void) __sync_add_and_fetch((v), 1)
|
|
|
|
#define p_atomic_dec(v) (void) __sync_sub_and_fetch((v), 1)
|
2015-02-05 23:36:59 +00:00
|
|
|
#define p_atomic_add(v, i) (void) __sync_add_and_fetch((v), (i))
|
2014-11-22 00:33:40 +00:00
|
|
|
#define p_atomic_inc_return(v) __sync_add_and_fetch((v), 1)
|
|
|
|
#define p_atomic_dec_return(v) __sync_sub_and_fetch((v), 1)
|
2019-12-02 20:36:39 +00:00
|
|
|
#define p_atomic_add_return(v, i) __sync_add_and_fetch((v), (i))
|
2022-07-08 20:30:45 +01:00
|
|
|
#define p_atomic_fetch_add(v, i) __sync_fetch_and_add((v), (i))
|
2016-10-04 15:06:31 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* There is no __atomic_* compare and exchange that returns the current value.
|
|
|
|
* Also, GCC 5.4 seems unable to optimize a compound statement expression that
|
|
|
|
* uses an additional stack variable with __atomic_compare_exchange[_n].
|
|
|
|
*/
|
2014-11-22 00:33:40 +00:00
|
|
|
#define p_atomic_cmpxchg(v, old, _new) \
|
|
|
|
__sync_val_compare_and_swap((v), (old), (_new))
|
2010-02-03 22:15:53 +00:00
|
|
|
|
2009-03-18 11:29:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Unlocked version for single threaded environments, such as some
|
|
|
|
* windows kernel modules.
|
|
|
|
*/
|
2022-03-18 05:14:55 +00:00
|
|
|
#if defined(PIPE_ATOMIC_OS_UNLOCKED)
|
2009-03-16 10:40:18 +00:00
|
|
|
|
2009-03-18 11:29:01 +00:00
|
|
|
#define PIPE_ATOMIC "Unlocked"
|
2009-03-17 09:54:08 +00:00
|
|
|
|
2010-02-02 14:43:48 +00:00
|
|
|
#define p_atomic_set(_v, _i) (*(_v) = (_i))
|
|
|
|
#define p_atomic_read(_v) (*(_v))
|
2021-01-28 17:55:29 +00:00
|
|
|
#define p_atomic_read_relaxed(_v) (*(_v))
|
2014-11-25 14:25:28 +00:00
|
|
|
#define p_atomic_dec_zero(_v) (p_atomic_dec_return(_v) == 0)
|
|
|
|
#define p_atomic_inc(_v) ((void) p_atomic_inc_return(_v))
|
|
|
|
#define p_atomic_dec(_v) ((void) p_atomic_dec_return(_v))
|
2019-12-09 17:49:13 +00:00
|
|
|
#define p_atomic_add(_v, _i) ((void) p_atomic_add_return((_v), (_i)))
|
2014-11-25 14:25:28 +00:00
|
|
|
#define p_atomic_inc_return(_v) (++(*(_v)))
|
|
|
|
#define p_atomic_dec_return(_v) (--(*(_v)))
|
2019-12-05 17:49:18 +00:00
|
|
|
#define p_atomic_add_return(_v, _i) (*(_v) = *(_v) + (_i))
|
2022-07-08 20:30:45 +01:00
|
|
|
#define p_atomic_fetch_add(_v, _i) (*(_v) = *(_v) + (_i), *(_v) - (_i))
|
2014-11-25 14:25:28 +00:00
|
|
|
#define p_atomic_cmpxchg(_v, _old, _new) (*(_v) == (_old) ? (*(_v) = (_new), (_old)) : *(_v))
|
2009-03-17 09:54:08 +00:00
|
|
|
|
2009-03-18 11:29:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-02-01 16:12:44 +00:00
|
|
|
#if defined(PIPE_ATOMIC_MSVC_INTRINSIC)
|
2009-03-18 11:29:01 +00:00
|
|
|
|
2010-02-01 16:12:44 +00:00
|
|
|
#define PIPE_ATOMIC "MSVC Intrinsics"
|
2009-03-16 12:07:22 +00:00
|
|
|
|
2015-01-20 23:34:26 +00:00
|
|
|
/* We use the Windows header's Interlocked*64 functions instead of the
|
|
|
|
* _Interlocked*64 intrinsics wherever we can, as support for the latter varies
|
2014-11-22 00:33:40 +00:00
|
|
|
* with target CPU, whereas Windows headers take care of all portability
|
|
|
|
* issues: using intrinsics where available, falling back to library
|
|
|
|
* implementations where not.
|
|
|
|
*/
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
|
|
#endif
|
|
|
|
#include <windows.h>
|
2010-02-01 16:12:44 +00:00
|
|
|
#include <intrin.h>
|
2014-11-22 00:33:40 +00:00
|
|
|
#include <assert.h>
|
2010-02-01 16:12:44 +00:00
|
|
|
|
2014-11-22 00:33:40 +00:00
|
|
|
/* MSVC supports decltype keyword, but it's only supported on C++ and doesn't
|
|
|
|
* quite work here; and if a C++-only solution is worthwhile, then it would be
|
|
|
|
* better to use templates / function overloading, instead of decltype magic.
|
|
|
|
* Therefore, we rely on implicit casting to LONGLONG for the functions that return
|
|
|
|
*/
|
2010-02-03 22:15:53 +00:00
|
|
|
|
2010-02-02 14:43:48 +00:00
|
|
|
#define p_atomic_set(_v, _i) (*(_v) = (_i))
|
|
|
|
#define p_atomic_read(_v) (*(_v))
|
2021-01-28 17:55:29 +00:00
|
|
|
#define p_atomic_read_relaxed(_v) (*(_v))
|
2009-03-16 12:07:22 +00:00
|
|
|
|
2014-11-22 00:33:40 +00:00
|
|
|
#define p_atomic_dec_zero(_v) \
|
|
|
|
(p_atomic_dec_return(_v) == 0)
|
|
|
|
|
|
|
|
#define p_atomic_inc(_v) \
|
|
|
|
((void) p_atomic_inc_return(_v))
|
|
|
|
|
|
|
|
#define p_atomic_inc_return(_v) (\
|
2015-01-20 23:34:26 +00:00
|
|
|
sizeof *(_v) == sizeof(short) ? _InterlockedIncrement16((short *) (_v)) : \
|
|
|
|
sizeof *(_v) == sizeof(long) ? _InterlockedIncrement ((long *) (_v)) : \
|
|
|
|
sizeof *(_v) == sizeof(__int64) ? InterlockedIncrement64 ((__int64 *)(_v)) : \
|
2014-11-22 00:33:40 +00:00
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
|
|
|
#define p_atomic_dec(_v) \
|
|
|
|
((void) p_atomic_dec_return(_v))
|
|
|
|
|
|
|
|
#define p_atomic_dec_return(_v) (\
|
2015-01-20 23:34:26 +00:00
|
|
|
sizeof *(_v) == sizeof(short) ? _InterlockedDecrement16((short *) (_v)) : \
|
|
|
|
sizeof *(_v) == sizeof(long) ? _InterlockedDecrement ((long *) (_v)) : \
|
|
|
|
sizeof *(_v) == sizeof(__int64) ? InterlockedDecrement64 ((__int64 *)(_v)) : \
|
2014-11-22 00:33:40 +00:00
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2019-12-02 20:36:39 +00:00
|
|
|
#define p_atomic_add(_v, _i) \
|
2022-07-08 20:30:45 +01:00
|
|
|
((void) p_atomic_fetch_add((_v), (_i)))
|
2019-12-02 20:36:39 +00:00
|
|
|
|
|
|
|
#define p_atomic_add_return(_v, _i) (\
|
2022-07-08 20:29:41 +01:00
|
|
|
sizeof *(_v) == sizeof(long) ? InterlockedAdd ((long *) (_v), (_i)) : \
|
|
|
|
sizeof *(_v) == sizeof(__int64) ? InterlockedAdd64((__int64 *)(_v), (_i)) : \
|
2015-02-05 23:36:59 +00:00
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2022-07-08 20:30:45 +01:00
|
|
|
#define p_atomic_fetch_add(_v, _i) (\
|
|
|
|
sizeof *(_v) == sizeof(char) ? _InterlockedExchangeAdd8 ((char *) (_v), (_i)) : \
|
|
|
|
sizeof *(_v) == sizeof(short) ? _InterlockedExchangeAdd16((short *) (_v), (_i)) : \
|
|
|
|
sizeof *(_v) == sizeof(long) ? InterlockedExchangeAdd ((long *) (_v), (_i)) : \
|
|
|
|
sizeof *(_v) == sizeof(__int64) ? InterlockedExchangeAdd64((__int64 *)(_v), (_i)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2014-11-22 00:33:40 +00:00
|
|
|
#define p_atomic_cmpxchg(_v, _old, _new) (\
|
2015-01-20 23:34:26 +00:00
|
|
|
sizeof *(_v) == sizeof(char) ? _InterlockedCompareExchange8 ((char *) (_v), (char) (_new), (char) (_old)) : \
|
|
|
|
sizeof *(_v) == sizeof(short) ? _InterlockedCompareExchange16((short *) (_v), (short) (_new), (short) (_old)) : \
|
|
|
|
sizeof *(_v) == sizeof(long) ? _InterlockedCompareExchange ((long *) (_v), (long) (_new), (long) (_old)) : \
|
|
|
|
sizeof *(_v) == sizeof(__int64) ? InterlockedCompareExchange64 ((__int64 *)(_v), (__int64)(_new), (__int64)(_old)) : \
|
2014-11-22 00:33:40 +00:00
|
|
|
(assert(!"should not get here"), 0))
|
2010-02-03 22:15:53 +00:00
|
|
|
|
2022-07-08 19:15:13 +01:00
|
|
|
#define PIPE_NATIVE_ATOMIC_XCHG
|
|
|
|
#define p_atomic_xchg(_v, _new) (\
|
|
|
|
sizeof *(_v) == sizeof(long) ? InterlockedExchange ((long *) (_v), (long) (_new)) : \
|
|
|
|
sizeof *(_v) == sizeof(__int64) ? InterlockedExchange64((__int64 *)(_v), (__int64)(_new)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2009-03-18 11:29:01 +00:00
|
|
|
#endif
|
|
|
|
|
2010-02-06 03:36:42 +00:00
|
|
|
#if defined(PIPE_ATOMIC_OS_SOLARIS)
|
|
|
|
|
|
|
|
#define PIPE_ATOMIC "Solaris OS atomic functions"
|
|
|
|
|
|
|
|
#include <atomic.h>
|
2014-11-22 00:33:40 +00:00
|
|
|
#include <assert.h>
|
2010-02-06 03:36:42 +00:00
|
|
|
|
|
|
|
#define p_atomic_set(_v, _i) (*(_v) = (_i))
|
|
|
|
#define p_atomic_read(_v) (*(_v))
|
|
|
|
|
2014-11-22 00:33:40 +00:00
|
|
|
#define p_atomic_dec_zero(v) (\
|
|
|
|
sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) == 0 : \
|
|
|
|
sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) == 0 : \
|
|
|
|
sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) == 0 : \
|
|
|
|
sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) == 0 : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
|
|
|
#define p_atomic_inc(v) (void) (\
|
|
|
|
sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8 ((uint8_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16((uint16_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32((uint32_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64((uint64_t *)(v)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2019-10-16 13:58:56 +01:00
|
|
|
#define p_atomic_inc_return(v) (__typeof(*v))( \
|
2014-11-22 00:33:40 +00:00
|
|
|
sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8_nv ((uint8_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16_nv((uint16_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32_nv((uint32_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64_nv((uint64_t *)(v)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2017-02-12 02:35:55 +00:00
|
|
|
#define p_atomic_dec(v) (void) ( \
|
2014-11-22 00:33:40 +00:00
|
|
|
sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8 ((uint8_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16((uint16_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32((uint32_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64((uint64_t *)(v)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2019-10-16 13:58:56 +01:00
|
|
|
#define p_atomic_dec_return(v) (__typeof(*v))( \
|
2014-11-22 00:33:40 +00:00
|
|
|
sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) : \
|
|
|
|
sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2017-02-12 02:35:55 +00:00
|
|
|
#define p_atomic_add(v, i) (void) ( \
|
2015-02-05 23:36:59 +00:00
|
|
|
sizeof(*v) == sizeof(uint8_t) ? atomic_add_8 ((uint8_t *)(v), (i)) : \
|
|
|
|
sizeof(*v) == sizeof(uint16_t) ? atomic_add_16((uint16_t *)(v), (i)) : \
|
|
|
|
sizeof(*v) == sizeof(uint32_t) ? atomic_add_32((uint32_t *)(v), (i)) : \
|
|
|
|
sizeof(*v) == sizeof(uint64_t) ? atomic_add_64((uint64_t *)(v), (i)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2019-12-08 12:52:21 +00:00
|
|
|
#define p_atomic_add_return(v, i) (__typeof(*v)) ( \
|
2019-12-02 20:36:39 +00:00
|
|
|
sizeof(*v) == sizeof(uint8_t) ? atomic_add_8_nv ((uint8_t *)(v), (i)) : \
|
|
|
|
sizeof(*v) == sizeof(uint16_t) ? atomic_add_16_nv((uint16_t *)(v), (i)) : \
|
|
|
|
sizeof(*v) == sizeof(uint32_t) ? atomic_add_32_nv((uint32_t *)(v), (i)) : \
|
|
|
|
sizeof(*v) == sizeof(uint64_t) ? atomic_add_64_nv((uint64_t *)(v), (i)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2022-07-08 20:30:45 +01:00
|
|
|
#define p_atomic_fetch_add(v, i) (__typeof(*v)) ( \
|
|
|
|
sizeof(*v) == sizeof(uint8_t) ? atomic_add_8_nv ((uint8_t *)(v), (i)) - (i) : \
|
|
|
|
sizeof(*v) == sizeof(uint16_t) ? atomic_add_16_nv((uint16_t *)(v), (i)) - (i) : \
|
|
|
|
sizeof(*v) == sizeof(uint32_t) ? atomic_add_32_nv((uint32_t *)(v), (i)) - (i) : \
|
|
|
|
sizeof(*v) == sizeof(uint64_t) ? atomic_add_64_nv((uint64_t *)(v), (i)) - (i) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
|
2019-10-16 13:58:56 +01:00
|
|
|
#define p_atomic_cmpxchg(v, old, _new) (__typeof(*v))( \
|
2014-11-22 00:33:40 +00:00
|
|
|
sizeof(*v) == sizeof(uint8_t) ? atomic_cas_8 ((uint8_t *)(v), (uint8_t )(old), (uint8_t )(_new)) : \
|
|
|
|
sizeof(*v) == sizeof(uint16_t) ? atomic_cas_16((uint16_t *)(v), (uint16_t)(old), (uint16_t)(_new)) : \
|
|
|
|
sizeof(*v) == sizeof(uint32_t) ? atomic_cas_32((uint32_t *)(v), (uint32_t)(old), (uint32_t)(_new)) : \
|
|
|
|
sizeof(*v) == sizeof(uint64_t) ? atomic_cas_64((uint64_t *)(v), (uint64_t)(old), (uint64_t)(_new)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
2010-02-06 03:36:42 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-03-18 11:29:01 +00:00
|
|
|
#ifndef PIPE_ATOMIC
|
|
|
|
#error "No pipe_atomic implementation selected"
|
|
|
|
#endif
|
|
|
|
|
2017-11-03 14:19:57 +00:00
|
|
|
#ifndef PIPE_NATIVE_ATOMIC_XCHG
|
|
|
|
static inline uint32_t p_atomic_xchg_32(uint32_t *v, uint32_t i)
|
|
|
|
{
|
|
|
|
uint32_t actual = p_atomic_read(v);
|
|
|
|
uint32_t expected;
|
|
|
|
do {
|
|
|
|
expected = actual;
|
|
|
|
actual = p_atomic_cmpxchg(v, expected, i);
|
|
|
|
} while (expected != actual);
|
|
|
|
return actual;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t p_atomic_xchg_64(uint64_t *v, uint64_t i)
|
|
|
|
{
|
|
|
|
uint64_t actual = p_atomic_read(v);
|
|
|
|
uint64_t expected;
|
|
|
|
do {
|
|
|
|
expected = actual;
|
|
|
|
actual = p_atomic_cmpxchg(v, expected, i);
|
|
|
|
} while (expected != actual);
|
|
|
|
return actual;
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:58:56 +01:00
|
|
|
#define p_atomic_xchg(v, i) (__typeof(*(v)))( \
|
2017-11-03 14:19:57 +00:00
|
|
|
sizeof(*(v)) == sizeof(uint32_t) ? p_atomic_xchg_32((uint32_t *)(v), (uint32_t)(i)) : \
|
|
|
|
sizeof(*(v)) == sizeof(uint64_t) ? p_atomic_xchg_64((uint64_t *)(v), (uint64_t)(i)) : \
|
|
|
|
(assert(!"should not get here"), 0))
|
|
|
|
#endif
|
2009-03-16 10:40:18 +00:00
|
|
|
|
2010-02-02 15:18:01 +00:00
|
|
|
#endif /* U_ATOMIC_H */
|