Blender  V2.93
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 Copyright (C) 2016 Blender Foundation.
42  * All rights reserved.
43  *
44  * The Original Code is: adapted from jemalloc.
45  */
46 
47 #ifndef __ATOMIC_OPS_EXT_H__
48 #define __ATOMIC_OPS_EXT_H__
49 
50 #include "atomic_ops_utils.h"
51 
52 /******************************************************************************/
53 /* size_t operations. */
54 ATOMIC_STATIC_ASSERT(sizeof(size_t) == LG_SIZEOF_PTR, "sizeof(size_t) != LG_SIZEOF_PTR");
55 
56 ATOMIC_INLINE size_t atomic_add_and_fetch_z(size_t *p, size_t x)
57 {
58 #if (LG_SIZEOF_PTR == 8)
59  return (size_t)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t)x);
60 #elif (LG_SIZEOF_PTR == 4)
61  return (size_t)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t)x);
62 #endif
63 }
64 
65 ATOMIC_INLINE size_t atomic_sub_and_fetch_z(size_t *p, size_t x)
66 {
67 #if (LG_SIZEOF_PTR == 8)
68  return (size_t)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t) - ((int64_t)x));
69 #elif (LG_SIZEOF_PTR == 4)
70  return (size_t)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t) - ((int32_t)x));
71 #endif
72 }
73 
74 ATOMIC_INLINE size_t atomic_fetch_and_add_z(size_t *p, size_t x)
75 {
76 #if (LG_SIZEOF_PTR == 8)
77  return (size_t)atomic_fetch_and_add_uint64((uint64_t *)p, (uint64_t)x);
78 #elif (LG_SIZEOF_PTR == 4)
79  return (size_t)atomic_fetch_and_add_uint32((uint32_t *)p, (uint32_t)x);
80 #endif
81 }
82 
83 ATOMIC_INLINE size_t atomic_fetch_and_sub_z(size_t *p, size_t x)
84 {
85 #if (LG_SIZEOF_PTR == 8)
86  return (size_t)atomic_fetch_and_add_uint64((uint64_t *)p, (uint64_t) - ((int64_t)x));
87 #elif (LG_SIZEOF_PTR == 4)
88  return (size_t)atomic_fetch_and_add_uint32((uint32_t *)p, (uint32_t) - ((int32_t)x));
89 #endif
90 }
91 
92 ATOMIC_INLINE size_t atomic_cas_z(size_t *v, size_t old, size_t _new)
93 {
94 #if (LG_SIZEOF_PTR == 8)
95  return (size_t)atomic_cas_uint64((uint64_t *)v, (uint64_t)old, (uint64_t)_new);
96 #elif (LG_SIZEOF_PTR == 4)
97  return (size_t)atomic_cas_uint32((uint32_t *)v, (uint32_t)old, (uint32_t)_new);
98 #endif
99 }
100 
102 {
103  size_t prev_value;
104  while ((prev_value = *p) < x) {
105  if (atomic_cas_z(p, prev_value, x) == prev_value) {
106  break;
107  }
108  }
109  return prev_value;
110 }
111 
112 /******************************************************************************/
113 /* unsigned operations. */
114 ATOMIC_STATIC_ASSERT(sizeof(unsigned int) == LG_SIZEOF_INT,
115  "sizeof(unsigned int) != LG_SIZEOF_INT");
116 
117 ATOMIC_INLINE unsigned int atomic_add_and_fetch_u(unsigned int *p, unsigned int x)
118 {
119 #if (LG_SIZEOF_INT == 8)
120  return (unsigned int)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t)x);
121 #elif (LG_SIZEOF_INT == 4)
122  return (unsigned int)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t)x);
123 #endif
124 }
125 
126 ATOMIC_INLINE unsigned int atomic_sub_and_fetch_u(unsigned int *p, unsigned int x)
127 {
128 #if (LG_SIZEOF_INT == 8)
129  return (unsigned int)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t) - ((int64_t)x));
130 #elif (LG_SIZEOF_INT == 4)
131  return (unsigned int)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t) - ((int32_t)x));
132 #endif
133 }
134 
135 ATOMIC_INLINE unsigned int atomic_fetch_and_add_u(unsigned int *p, unsigned int x)
136 {
137 #if (LG_SIZEOF_INT == 8)
138  return (unsigned int)atomic_fetch_and_add_uint64((uint64_t *)p, (uint64_t)x);
139 #elif (LG_SIZEOF_INT == 4)
140  return (unsigned int)atomic_fetch_and_add_uint32((uint32_t *)p, (uint32_t)x);
141 #endif
142 }
143 
144 ATOMIC_INLINE unsigned int atomic_fetch_and_sub_u(unsigned int *p, unsigned int x)
145 {
146 #if (LG_SIZEOF_INT == 8)
147  return (unsigned int)atomic_fetch_and_add_uint64((uint64_t *)p, (uint64_t) - ((int64_t)x));
148 #elif (LG_SIZEOF_INT == 4)
149  return (unsigned int)atomic_fetch_and_add_uint32((uint32_t *)p, (uint32_t) - ((int32_t)x));
150 #endif
151 }
152 
153 ATOMIC_INLINE unsigned int atomic_cas_u(unsigned int *v, unsigned int old, unsigned int _new)
154 {
155 #if (LG_SIZEOF_INT == 8)
156  return (unsigned int)atomic_cas_uint64((uint64_t *)v, (uint64_t)old, (uint64_t)_new);
157 #elif (LG_SIZEOF_INT == 4)
158  return (unsigned int)atomic_cas_uint32((uint32_t *)v, (uint32_t)old, (uint32_t)_new);
159 #endif
160 }
161 
162 /******************************************************************************/
163 /* Char operations. */
165 {
166  return (char)atomic_fetch_and_or_uint8((uint8_t *)p, (uint8_t)b);
167 }
168 
170 {
171  return (char)atomic_fetch_and_and_uint8((uint8_t *)p, (uint8_t)b);
172 }
173 
174 /******************************************************************************/
175 /* Pointer operations. */
176 
177 ATOMIC_INLINE void *atomic_cas_ptr(void **v, void *old, void *_new)
178 {
179 #if (LG_SIZEOF_PTR == 8)
180  return (void *)atomic_cas_uint64((uint64_t *)v, *(uint64_t *)&old, *(uint64_t *)&_new);
181 #elif (LG_SIZEOF_PTR == 4)
182  return (void *)atomic_cas_uint32((uint32_t *)v, *(uint32_t *)&old, *(uint32_t *)&_new);
183 #endif
184 }
185 
186 /******************************************************************************/
187 /* float operations. */
188 ATOMIC_STATIC_ASSERT(sizeof(float) == sizeof(uint32_t), "sizeof(float) != sizeof(uint32_t)");
189 
190 ATOMIC_INLINE float atomic_cas_float(float *v, float old, float _new)
191 {
192  uint32_t ret = atomic_cas_uint32((uint32_t *)v, *(uint32_t *)&old, *(uint32_t *)&_new);
193  return *(float *)&ret;
194 }
195 
196 ATOMIC_INLINE float atomic_add_and_fetch_fl(float *p, const float x)
197 {
198  float oldval, newval;
199  uint32_t prevval;
200 
201  do { /* Note that since collisions are unlikely, loop will nearly always run once. */
202  oldval = *p;
203  newval = oldval + x;
204  prevval = atomic_cas_uint32((uint32_t *)p, *(uint32_t *)(&oldval), *(uint32_t *)(&newval));
205  } while (_ATOMIC_UNLIKELY(prevval != *(uint32_t *)(&oldval)));
206 
207  return newval;
208 }
209 
210 #endif /* __ATOMIC_OPS_EXT_H__ */
ATOMIC_INLINE uint8_t atomic_fetch_and_and_uint8(uint8_t *p, uint8_t b)
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_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_add_and_fetch_z(size_t *p, size_t x)
ATOMIC_INLINE size_t atomic_sub_and_fetch_z(size_t *p, size_t x)
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 void * atomic_cas_ptr(void **v, void *old, void *_new)
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_STATIC_ASSERT(sizeof(size_t)==LG_SIZEOF_PTR, "sizeof(size_t) != LG_SIZEOF_PTR")
ATOMIC_INLINE size_t atomic_cas_z(size_t *v, size_t old, size_t _new)
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 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)
ATTR_WARN_UNUSED_RESULT const BMVert * v
return ret
unsigned int uint32_t
Definition: stdint.h:83
__int64 int64_t
Definition: stdint.h:92
signed int int32_t
Definition: stdint.h:80
unsigned char uint8_t
Definition: stdint.h:81
unsigned __int64 uint64_t
Definition: stdint.h:93