Skip to content

Commit 35376e5

Browse files
authored
Merge pull request #28236 from charris/backport-28234
BUG: Add cpp atomic support (#28234)
2 parents 26923d2 + 9016914 commit 35376e5

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

numpy/_core/src/common/npy_atomic.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99

1010
#include "numpy/npy_common.h"
1111

12-
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
12+
#ifdef __cplusplus
13+
extern "C++" {
14+
#include <atomic>
15+
}
16+
#define _NPY_USING_STD using namespace std
17+
#define _Atomic(tp) atomic<tp>
18+
#define STDC_ATOMICS
19+
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
1320
&& !defined(__STDC_NO_ATOMICS__)
14-
// TODO: support C++ atomics as well if this header is ever needed in C++
1521
#include <stdatomic.h>
1622
#include <stdint.h>
23+
#define _NPY_USING_STD
1724
#define STDC_ATOMICS
1825
#elif _MSC_VER
1926
#include <intrin.h>
@@ -35,6 +42,7 @@
3542
static inline npy_uint8
3643
npy_atomic_load_uint8(const npy_uint8 *obj) {
3744
#ifdef STDC_ATOMICS
45+
_NPY_USING_STD;
3846
return (npy_uint8)atomic_load((const _Atomic(uint8_t)*)obj);
3947
#elif defined(MSC_ATOMICS)
4048
#if defined(_M_X64) || defined(_M_IX86)
@@ -50,6 +58,7 @@ npy_atomic_load_uint8(const npy_uint8 *obj) {
5058
static inline void*
5159
npy_atomic_load_ptr(const void *obj) {
5260
#ifdef STDC_ATOMICS
61+
_NPY_USING_STD;
5362
return atomic_load((const _Atomic(void *)*)obj);
5463
#elif defined(MSC_ATOMICS)
5564
#if SIZEOF_VOID_P == 8
@@ -73,6 +82,7 @@ npy_atomic_load_ptr(const void *obj) {
7382
static inline void
7483
npy_atomic_store_uint8(npy_uint8 *obj, npy_uint8 value) {
7584
#ifdef STDC_ATOMICS
85+
_NPY_USING_STD;
7686
atomic_store((_Atomic(uint8_t)*)obj, value);
7787
#elif defined(MSC_ATOMICS)
7888
_InterlockedExchange8((volatile char *)obj, (char)value);
@@ -85,6 +95,7 @@ static inline void
8595
npy_atomic_store_ptr(void *obj, void *value)
8696
{
8797
#ifdef STDC_ATOMICS
98+
_NPY_USING_STD;
8899
atomic_store((_Atomic(void *)*)obj, value);
89100
#elif defined(MSC_ATOMICS)
90101
_InterlockedExchangePointer((void * volatile *)obj, (void *)value);

0 commit comments

Comments
 (0)