Blender V5.3
atomic_ops_msvc.h
Go to the documentation of this file.
1/*
2 * Adopted from jemalloc with this license:
3 *
4 * Copyright (C) 2002-2013 Jason Evans <jasone@canonware.com>.
5 * All rights reserved.
6 * Copyright (C) 2007-2012 Mozilla Foundation. All rights reserved.
7 * Copyright (C) 2009-2013 Facebook, Inc. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright notice(s),
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice(s),
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
32
33#ifndef __ATOMIC_OPS_MSVC_H__
34#define __ATOMIC_OPS_MSVC_H__
35
36#include "atomic_ops_utils.h"
37
38#define NOGDI
39#ifndef WIN32_LEAN_AND_MEAN
40# define WIN32_LEAN_AND_MEAN
41#endif
42
43#include <intrin.h>
44#include <windows.h>
45
46#if defined(__clang__)
47# pragma GCC diagnostic push
48# pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
49#endif
50
51/* TODO(sergey): On x64 platform both read and write of a variable aligned to its type size is
52 * atomic, so in theory it is possible to avoid memory barrier and gain performance. The downside
53 * of that would be that it will impose requirement to value which is being operated on. */
54#define __atomic_impl_load_generic(v) (MemoryBarrier(), *(v))
55#define __atomic_impl_store_generic(p, v) \
56 do { \
57 *(p) = (v); \
58 MemoryBarrier(); \
59 } while (0)
60
61/* 64-bit operations. */
62/* Unsigned */
64{
65 return InterlockedExchangeAdd64((int64_t *)p, (int64_t)x) + x;
66}
67
69{
70 return InterlockedExchangeAdd64((int64_t *)p, -((int64_t)x)) - x;
71}
72
74{
75 return InterlockedCompareExchange64((int64_t *)v, _new, old);
76}
77
82
87
89{
90 return InterlockedExchangeAdd64((int64_t *)p, (int64_t)x);
91}
92
94{
95 return InterlockedExchangeAdd64((int64_t *)p, -((int64_t)x));
96}
97
98/* Signed */
100{
101 return InterlockedExchangeAdd64(p, x) + x;
102}
103
105{
106 return InterlockedExchangeAdd64(p, -x) - x;
107}
108
110{
111 return InterlockedCompareExchange64(v, _new, old);
112}
113
118
123
125{
126 return InterlockedExchangeAdd64(p, x);
127}
128
130{
131 return InterlockedExchangeAdd64(p, -x);
132}
133
134/******************************************************************************/
135/* 32-bit operations. */
136/* Unsigned */
137ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x)
138{
139 return InterlockedExchangeAdd(p, x) + x;
140}
141
142ATOMIC_INLINE uint32_t atomic_sub_and_fetch_uint32(uint32_t *p, uint32_t x)
143{
144 return InterlockedExchangeAdd(p, -((int32_t)x)) - x;
145}
146
147ATOMIC_INLINE uint32_t atomic_cas_uint32(uint32_t *v, uint32_t old, uint32_t _new)
148{
149 return InterlockedCompareExchange((long *)v, _new, old);
150}
151
152ATOMIC_INLINE uint32_t atomic_load_uint32(const uint32_t *v)
153{
155}
156
157ATOMIC_INLINE void atomic_store_uint32(uint32_t *p, uint32_t v)
158{
160}
161
162ATOMIC_INLINE uint32_t atomic_fetch_and_add_uint32(uint32_t *p, uint32_t x)
163{
164 return InterlockedExchangeAdd(p, x);
165}
166
167ATOMIC_INLINE uint32_t atomic_fetch_and_or_uint32(uint32_t *p, uint32_t x)
168{
169 return InterlockedOr((long *)p, x);
170}
171
172ATOMIC_INLINE uint32_t atomic_fetch_and_and_uint32(uint32_t *p, uint32_t x)
173{
174 return InterlockedAnd((long *)p, x);
175}
176
177/* Signed */
179{
180 return InterlockedExchangeAdd((long *)p, x) + x;
181}
182
184{
185 return InterlockedExchangeAdd((long *)p, -x) - x;
186}
187
189{
190 return InterlockedCompareExchange((long *)v, _new, old);
191}
192
197
202
204{
205 return InterlockedExchangeAdd((long *)p, x);
206}
207
209{
210 return InterlockedOr((long *)p, x);
211}
212
214{
215 return InterlockedAnd((long *)p, x);
216}
217
218/******************************************************************************/
219/* 16-bit operations. */
220
221/* Signed */
222ATOMIC_INLINE int16_t atomic_fetch_and_or_int16(int16_t *p, int16_t x)
223{
224 return InterlockedOr16((short *)p, x);
225}
226
227ATOMIC_INLINE int16_t atomic_fetch_and_and_int16(int16_t *p, int16_t x)
228{
229 return InterlockedAnd16((short *)p, x);
230}
231
232/******************************************************************************/
233/* 8-bit operations. */
234
235/* Unsigned */
236#pragma intrinsic(_InterlockedAnd8)
237ATOMIC_INLINE uint8_t atomic_fetch_and_and_uint8(uint8_t *p, uint8_t b)
238{
239#if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
240 return InterlockedAnd8((char *)p, (char)b);
241#else
242 return _InterlockedAnd8((char *)p, (char)b);
243#endif
244}
245
246#pragma intrinsic(_InterlockedOr8)
247ATOMIC_INLINE uint8_t atomic_fetch_and_or_uint8(uint8_t *p, uint8_t b)
248{
249#if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
250 return InterlockedOr8((char *)p, (char)b);
251#else
252 return _InterlockedOr8((char *)p, (char)b);
253#endif
254}
255
256/* Signed */
257#pragma intrinsic(_InterlockedAnd8)
259{
260#if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
261 return InterlockedAnd8((char *)p, (char)b);
262#else
263 return _InterlockedAnd8((char *)p, (char)b);
264#endif
265}
266
267#pragma intrinsic(_InterlockedOr8)
268ATOMIC_INLINE int8_t atomic_fetch_and_or_int8(int8_t *p, int8_t b)
269{
270#if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
271 return InterlockedOr8((char *)p, (char)b);
272#else
273 return _InterlockedOr8((char *)p, (char)b);
274#endif
275}
276
277#undef __atomic_impl_load_generic
278#undef __atomic_impl_store_generic
279
280#if defined(__clang__)
281# pragma GCC diagnostic pop
282#endif
283
284#endif /* __ATOMIC_OPS_MSVC_H__ */
ATOMIC_INLINE uint32_t atomic_fetch_and_or_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE int32_t atomic_add_and_fetch_int32(int32_t *p, int32_t x)
ATOMIC_INLINE int16_t atomic_fetch_and_or_int16(int16_t *p, int16_t b)
ATOMIC_INLINE void atomic_store_uint64(uint64_t *p, uint64_t v)
ATOMIC_INLINE uint64_t atomic_fetch_and_sub_uint64(uint64_t *p, uint64_t x)
ATOMIC_INLINE int64_t atomic_sub_and_fetch_int64(int64_t *p, int64_t x)
ATOMIC_INLINE uint8_t atomic_fetch_and_and_uint8(uint8_t *p, uint8_t b)
ATOMIC_INLINE uint64_t atomic_load_uint64(const uint64_t *v)
ATOMIC_INLINE int64_t atomic_cas_int64(int64_t *v, int64_t old, int64_t _new)
ATOMIC_INLINE uint32_t atomic_fetch_and_add_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE uint8_t atomic_fetch_and_or_uint8(uint8_t *p, uint8_t b)
ATOMIC_INLINE int32_t atomic_load_int32(const int32_t *v)
ATOMIC_INLINE int64_t atomic_load_int64(const int64_t *v)
ATOMIC_INLINE int32_t atomic_fetch_and_or_int32(int32_t *p, int32_t x)
ATOMIC_INLINE uint32_t atomic_fetch_and_and_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE void atomic_store_int64(int64_t *p, int64_t v)
ATOMIC_INLINE int64_t atomic_fetch_and_add_int64(int64_t *p, int64_t x)
ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE void atomic_store_int32(int32_t *p, int32_t v)
ATOMIC_INLINE int32_t atomic_fetch_and_add_int32(int32_t *p, int32_t x)
ATOMIC_INLINE uint64_t atomic_cas_uint64(uint64_t *v, uint64_t old, uint64_t _new)
ATOMIC_INLINE uint64_t atomic_fetch_and_add_uint64(uint64_t *p, uint64_t x)
ATOMIC_INLINE uint32_t atomic_sub_and_fetch_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE int16_t atomic_fetch_and_and_int16(int16_t *p, int16_t b)
ATOMIC_INLINE int64_t atomic_add_and_fetch_int64(int64_t *p, int64_t x)
ATOMIC_INLINE uint64_t atomic_add_and_fetch_uint64(uint64_t *p, uint64_t x)
ATOMIC_INLINE uint32_t atomic_load_uint32(const uint32_t *v)
ATOMIC_INLINE uint64_t atomic_sub_and_fetch_uint64(uint64_t *p, uint64_t x)
ATOMIC_INLINE void atomic_store_uint32(uint32_t *p, uint32_t v)
ATOMIC_INLINE int32_t atomic_cas_int32(int32_t *v, int32_t old, int32_t _new)
ATOMIC_INLINE int32_t atomic_fetch_and_and_int32(int32_t *p, int32_t x)
ATOMIC_INLINE int8_t atomic_fetch_and_or_int8(int8_t *p, int8_t b)
ATOMIC_INLINE int32_t atomic_sub_and_fetch_int32(int32_t *p, int32_t x)
ATOMIC_INLINE int8_t atomic_fetch_and_and_int8(int8_t *p, int8_t b)
ATOMIC_INLINE int64_t atomic_fetch_and_sub_int64(int64_t *p, int64_t x)
ATOMIC_INLINE uint32_t atomic_cas_uint32(uint32_t *v, uint32_t old, uint32_t _new)
#define __atomic_impl_load_generic(v)
#define __atomic_impl_store_generic(p, v)
#define ATOMIC_INLINE
long long int int64_t
unsigned long long int uint64_t