Blender V4.3
atomic_ops_ext.h
Go to the documentation of this file.
1/*
2 * Original code 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 * This program is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU General Public License
29 * as published by the Free Software Foundation; either version 2
30 * of the License, or (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software Foundation,
39 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
40 *
41 * The Original Code is adapted from jemalloc.
42 * Modifications Copyright (C) 2016 Blender Foundation
43 */
44
49#ifndef __ATOMIC_OPS_EXT_H__
50#define __ATOMIC_OPS_EXT_H__
51
52#include "atomic_ops_utils.h"
53
54/******************************************************************************/
55/* size_t operations. */
56ATOMIC_STATIC_ASSERT(sizeof(size_t) == LG_SIZEOF_PTR, "sizeof(size_t) != LG_SIZEOF_PTR");
57
58ATOMIC_INLINE size_t atomic_add_and_fetch_z(size_t *p, size_t x)
59{
60#if (LG_SIZEOF_PTR == 8)
61 return (size_t)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t)x);
62#elif (LG_SIZEOF_PTR == 4)
63 return (size_t)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t)x);
64#endif
65}
66
67ATOMIC_INLINE size_t atomic_sub_and_fetch_z(size_t *p, size_t x)
68{
69#if (LG_SIZEOF_PTR == 8)
70 return (size_t)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t) - ((int64_t)x));
71#elif (LG_SIZEOF_PTR == 4)
72 return (size_t)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t) - ((int32_t)x));
73#endif
74}
75
76ATOMIC_INLINE size_t atomic_fetch_and_add_z(size_t *p, size_t x)
77{
78#if (LG_SIZEOF_PTR == 8)
79 return (size_t)atomic_fetch_and_add_uint64((uint64_t *)p, (uint64_t)x);
80#elif (LG_SIZEOF_PTR == 4)
81 return (size_t)atomic_fetch_and_add_uint32((uint32_t *)p, (uint32_t)x);
82#endif
83}
84
85ATOMIC_INLINE size_t atomic_fetch_and_sub_z(size_t *p, size_t x)
86{
87#if (LG_SIZEOF_PTR == 8)
88 return (size_t)atomic_fetch_and_add_uint64((uint64_t *)p, (uint64_t) - ((int64_t)x));
89#elif (LG_SIZEOF_PTR == 4)
90 return (size_t)atomic_fetch_and_add_uint32((uint32_t *)p, (uint32_t) - ((int32_t)x));
91#endif
92}
93
94ATOMIC_INLINE size_t atomic_cas_z(size_t *v, size_t old, size_t _new)
95{
96#if (LG_SIZEOF_PTR == 8)
97 return (size_t)atomic_cas_uint64((uint64_t *)v, (uint64_t)old, (uint64_t)_new);
98#elif (LG_SIZEOF_PTR == 4)
99 return (size_t)atomic_cas_uint32((uint32_t *)v, (uint32_t)old, (uint32_t)_new);
100#endif
101}
102
103ATOMIC_INLINE size_t atomic_load_z(const size_t *v)
104{
105#if (LG_SIZEOF_PTR == 8)
106 return (size_t)atomic_load_uint64((const uint64_t *)v);
107#elif (LG_SIZEOF_PTR == 4)
108 return (size_t)atomic_load_uint32((const uint32_t *)v);
109#endif
110}
111
112ATOMIC_INLINE void atomic_store_z(size_t *p, size_t v)
113{
114#if (LG_SIZEOF_PTR == 8)
116#elif (LG_SIZEOF_PTR == 4)
118#endif
119}
120
122{
123 size_t prev_value;
124 while ((prev_value = *p) < x) {
125 if (atomic_cas_z(p, prev_value, x) == prev_value) {
126 break;
127 }
128 }
129 return prev_value;
130}
131
132/******************************************************************************/
133/* unsigned operations. */
134ATOMIC_STATIC_ASSERT(sizeof(unsigned int) == LG_SIZEOF_INT,
135 "sizeof(unsigned int) != LG_SIZEOF_INT");
136
137ATOMIC_INLINE unsigned int atomic_add_and_fetch_u(unsigned int *p, unsigned int x)
138{
139#if (LG_SIZEOF_INT == 8)
140 return (unsigned int)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t)x);
141#elif (LG_SIZEOF_INT == 4)
142 return (unsigned int)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t)x);
143#endif
144}
145
146ATOMIC_INLINE unsigned int atomic_sub_and_fetch_u(unsigned int *p, unsigned int x)
147{
148#if (LG_SIZEOF_INT == 8)
149 return (unsigned int)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t) - ((int64_t)x));
150#elif (LG_SIZEOF_INT == 4)
151 return (unsigned int)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t) - ((int32_t)x));
152#endif
153}
154
155ATOMIC_INLINE unsigned int atomic_fetch_and_add_u(unsigned int *p, unsigned int x)
156{
157#if (LG_SIZEOF_INT == 8)
158 return (unsigned int)atomic_fetch_and_add_uint64((uint64_t *)p, (uint64_t)x);
159#elif (LG_SIZEOF_INT == 4)
160 return (unsigned int)atomic_fetch_and_add_uint32((uint32_t *)p, (uint32_t)x);
161#endif
162}
163
164ATOMIC_INLINE unsigned int atomic_fetch_and_sub_u(unsigned int *p, unsigned int x)
165{
166#if (LG_SIZEOF_INT == 8)
167 return (unsigned int)atomic_fetch_and_add_uint64((uint64_t *)p, (uint64_t) - ((int64_t)x));
168#elif (LG_SIZEOF_INT == 4)
169 return (unsigned int)atomic_fetch_and_add_uint32((uint32_t *)p, (uint32_t) - ((int32_t)x));
170#endif
171}
172
173ATOMIC_INLINE unsigned int atomic_cas_u(unsigned int *v, unsigned int old, unsigned int _new)
174{
175#if (LG_SIZEOF_INT == 8)
176 return (unsigned int)atomic_cas_uint64((uint64_t *)v, (uint64_t)old, (uint64_t)_new);
177#elif (LG_SIZEOF_INT == 4)
178 return (unsigned int)atomic_cas_uint32((uint32_t *)v, (uint32_t)old, (uint32_t)_new);
179#endif
180}
181
182/******************************************************************************/
183/* Char operations. */
185{
186 return (char)atomic_fetch_and_or_uint8((uint8_t *)p, (uint8_t)b);
187}
188
190{
191 return (char)atomic_fetch_and_and_uint8((uint8_t *)p, (uint8_t)b);
192}
193
194/******************************************************************************/
195/* Pointer operations. */
196
197ATOMIC_INLINE void *atomic_cas_ptr(void **v, void *old, void *_new)
198{
199#if (LG_SIZEOF_PTR == 8)
200 return (void *)atomic_cas_uint64((uint64_t *)v, *(uint64_t *)&old, *(uint64_t *)&_new);
201#elif (LG_SIZEOF_PTR == 4)
202 return (void *)atomic_cas_uint32((uint32_t *)v, *(uint32_t *)&old, *(uint32_t *)&_new);
203#endif
204}
205
206ATOMIC_INLINE void *atomic_load_ptr(void *const *v)
207{
208#if (LG_SIZEOF_PTR == 8)
209 return (void *)atomic_load_uint64((const uint64_t *)v);
210#elif (LG_SIZEOF_PTR == 4)
211 return (void *)atomic_load_uint32((const uint32_t *)v);
212#endif
213}
214
215ATOMIC_INLINE void atomic_store_ptr(void **p, void *v)
216{
217#if (LG_SIZEOF_PTR == 8)
219#elif (LG_SIZEOF_PTR == 4)
221#endif
222}
223
224/******************************************************************************/
225/* float operations. */
226ATOMIC_STATIC_ASSERT(sizeof(float) == sizeof(uint32_t), "sizeof(float) != sizeof(uint32_t)");
227
228ATOMIC_INLINE float atomic_cas_float(float *v, float old, float _new)
229{
230 uint32_t ret = atomic_cas_uint32((uint32_t *)v, *(uint32_t *)&old, *(uint32_t *)&_new);
231 return *(float *)&ret;
232}
233
234ATOMIC_INLINE float atomic_add_and_fetch_fl(float *p, const float x)
235{
236 float oldval, newval;
237 uint32_t prevval;
238
239 do { /* Note that since collisions are unlikely, loop will nearly always run once. */
240 oldval = *p;
241 newval = oldval + x;
242 prevval = atomic_cas_uint32((uint32_t *)p, *(uint32_t *)(&oldval), *(uint32_t *)(&newval));
243 } while (_ATOMIC_UNLIKELY(prevval != *(uint32_t *)(&oldval)));
244
245 return newval;
246}
247
248#endif /* __ATOMIC_OPS_EXT_H__ */
ATOMIC_INLINE void atomic_store_uint64(uint64_t *p, uint64_t v)
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 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 uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_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 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 void atomic_store_uint32(uint32_t *p, uint32_t v)
ATOMIC_INLINE uint32_t atomic_cas_uint32(uint32_t *v, uint32_t old, uint32_t _new)
ATOMIC_INLINE unsigned int atomic_fetch_and_sub_u(unsigned int *p, unsigned int x)
ATOMIC_INLINE size_t atomic_load_z(const size_t *v)
ATOMIC_INLINE size_t atomic_add_and_fetch_z(size_t *p, size_t x)
ATOMIC_INLINE void * atomic_load_ptr(void *const *v)
ATOMIC_INLINE size_t atomic_sub_and_fetch_z(size_t *p, size_t x)
ATOMIC_INLINE void * atomic_cas_ptr(void **v, void *old, void *_new)
ATOMIC_INLINE size_t atomic_fetch_and_sub_z(size_t *p, size_t x)
ATOMIC_INLINE float atomic_add_and_fetch_fl(float *p, const float x)
ATOMIC_INLINE char atomic_fetch_and_or_char(char *p, char b)
ATOMIC_INLINE unsigned int atomic_add_and_fetch_u(unsigned int *p, unsigned int x)
ATOMIC_INLINE char atomic_fetch_and_and_char(char *p, char b)
ATOMIC_INLINE unsigned int atomic_cas_u(unsigned int *v, unsigned int old, unsigned int _new)
ATOMIC_INLINE size_t atomic_cas_z(size_t *v, size_t old, size_t _new)
ATOMIC_INLINE void atomic_store_z(size_t *p, size_t v)
ATOMIC_INLINE float atomic_cas_float(float *v, float old, float _new)
ATOMIC_INLINE size_t atomic_fetch_and_add_z(size_t *p, size_t x)
ATOMIC_INLINE size_t atomic_fetch_and_update_max_z(size_t *p, size_t x)
ATOMIC_INLINE void atomic_store_ptr(void **p, void *v)
ATOMIC_INLINE unsigned int atomic_sub_and_fetch_u(unsigned int *p, unsigned int x)
ATOMIC_INLINE unsigned int atomic_fetch_and_add_u(unsigned int *p, unsigned int x)
#define ATOMIC_INLINE
#define _ATOMIC_UNLIKELY(x)
#define ATOMIC_STATIC_ASSERT(a, msg)
ATTR_WARN_UNUSED_RESULT const BMVert * v
local_group_size(16, 16) .push_constant(Type b
return ret
unsigned int uint32_t
Definition stdint.h:80
__int64 int64_t
Definition stdint.h:89
signed int int32_t
Definition stdint.h:77
unsigned char uint8_t
Definition stdint.h:78
unsigned __int64 uint64_t
Definition stdint.h:90