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 
29 #ifndef __ATOMIC_OPS_MSVC_H__
30 #define __ATOMIC_OPS_MSVC_H__
31 
32 #include "atomic_ops_utils.h"
33 
34 #define NOGDI
35 #ifndef NOMINMAX
36 #  define NOMINMAX
37 #endif
38 #define WIN32_LEAN_AND_MEAN
39 
40 #include <intrin.h>
41 #include <windows.h>
42 
43 #if defined(__clang__)
44 #  pragma GCC diagnostic push
45 #  pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
46 #endif
47 
48 /* 64-bit operations. */
49 #if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
50 /* Unsigned */
atomic_add_and_fetch_uint64(uint64_t * p,uint64_t x)51 ATOMIC_INLINE uint64_t atomic_add_and_fetch_uint64(uint64_t *p, uint64_t x)
52 {
53   return InterlockedExchangeAdd64((int64_t *)p, (int64_t)x) + x;
54 }
55 
atomic_sub_and_fetch_uint64(uint64_t * p,uint64_t x)56 ATOMIC_INLINE uint64_t atomic_sub_and_fetch_uint64(uint64_t *p, uint64_t x)
57 {
58   return InterlockedExchangeAdd64((int64_t *)p, -((int64_t)x)) - x;
59 }
60 
atomic_cas_uint64(uint64_t * v,uint64_t old,uint64_t _new)61 ATOMIC_INLINE uint64_t atomic_cas_uint64(uint64_t *v, uint64_t old, uint64_t _new)
62 {
63   return InterlockedCompareExchange64((int64_t *)v, _new, old);
64 }
65 
atomic_fetch_and_add_uint64(uint64_t * p,uint64_t x)66 ATOMIC_INLINE uint64_t atomic_fetch_and_add_uint64(uint64_t *p, uint64_t x)
67 {
68   return InterlockedExchangeAdd64((int64_t *)p, (int64_t)x);
69 }
70 
atomic_fetch_and_sub_uint64(uint64_t * p,uint64_t x)71 ATOMIC_INLINE uint64_t atomic_fetch_and_sub_uint64(uint64_t *p, uint64_t x)
72 {
73   return InterlockedExchangeAdd64((int64_t *)p, -((int64_t)x));
74 }
75 
76 /* Signed */
atomic_add_and_fetch_int64(int64_t * p,int64_t x)77 ATOMIC_INLINE int64_t atomic_add_and_fetch_int64(int64_t *p, int64_t x)
78 {
79   return InterlockedExchangeAdd64(p, x) + x;
80 }
81 
atomic_sub_and_fetch_int64(int64_t * p,int64_t x)82 ATOMIC_INLINE int64_t atomic_sub_and_fetch_int64(int64_t *p, int64_t x)
83 {
84   return InterlockedExchangeAdd64(p, -x) - x;
85 }
86 
atomic_cas_int64(int64_t * v,int64_t old,int64_t _new)87 ATOMIC_INLINE int64_t atomic_cas_int64(int64_t *v, int64_t old, int64_t _new)
88 {
89   return InterlockedCompareExchange64(v, _new, old);
90 }
91 
atomic_fetch_and_add_int64(int64_t * p,int64_t x)92 ATOMIC_INLINE int64_t atomic_fetch_and_add_int64(int64_t *p, int64_t x)
93 {
94   return InterlockedExchangeAdd64(p, x);
95 }
96 
atomic_fetch_and_sub_int64(int64_t * p,int64_t x)97 ATOMIC_INLINE int64_t atomic_fetch_and_sub_int64(int64_t *p, int64_t x)
98 {
99   return InterlockedExchangeAdd64(p, -x);
100 }
101 #endif
102 
103 /******************************************************************************/
104 /* 32-bit operations. */
105 /* Unsigned */
atomic_add_and_fetch_uint32(uint32_t * p,uint32_t x)106 ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x)
107 {
108   return InterlockedExchangeAdd(p, x) + x;
109 }
110 
atomic_sub_and_fetch_uint32(uint32_t * p,uint32_t x)111 ATOMIC_INLINE uint32_t atomic_sub_and_fetch_uint32(uint32_t *p, uint32_t x)
112 {
113   return InterlockedExchangeAdd(p, -((int32_t)x)) - x;
114 }
115 
atomic_cas_uint32(uint32_t * v,uint32_t old,uint32_t _new)116 ATOMIC_INLINE uint32_t atomic_cas_uint32(uint32_t *v, uint32_t old, uint32_t _new)
117 {
118   return InterlockedCompareExchange((long *)v, _new, old);
119 }
120 
atomic_fetch_and_add_uint32(uint32_t * p,uint32_t x)121 ATOMIC_INLINE uint32_t atomic_fetch_and_add_uint32(uint32_t *p, uint32_t x)
122 {
123   return InterlockedExchangeAdd(p, x);
124 }
125 
atomic_fetch_and_or_uint32(uint32_t * p,uint32_t x)126 ATOMIC_INLINE uint32_t atomic_fetch_and_or_uint32(uint32_t *p, uint32_t x)
127 {
128   return InterlockedOr((long *)p, x);
129 }
130 
atomic_fetch_and_and_uint32(uint32_t * p,uint32_t x)131 ATOMIC_INLINE uint32_t atomic_fetch_and_and_uint32(uint32_t *p, uint32_t x)
132 {
133   return InterlockedAnd((long *)p, x);
134 }
135 
136 /* Signed */
atomic_add_and_fetch_int32(int32_t * p,int32_t x)137 ATOMIC_INLINE int32_t atomic_add_and_fetch_int32(int32_t *p, int32_t x)
138 {
139   return InterlockedExchangeAdd((long *)p, x) + x;
140 }
141 
atomic_sub_and_fetch_int32(int32_t * p,int32_t x)142 ATOMIC_INLINE int32_t atomic_sub_and_fetch_int32(int32_t *p, int32_t x)
143 {
144   return InterlockedExchangeAdd((long *)p, -x) - x;
145 }
146 
atomic_cas_int32(int32_t * v,int32_t old,int32_t _new)147 ATOMIC_INLINE int32_t atomic_cas_int32(int32_t *v, int32_t old, int32_t _new)
148 {
149   return InterlockedCompareExchange((long *)v, _new, old);
150 }
151 
atomic_fetch_and_add_int32(int32_t * p,int32_t x)152 ATOMIC_INLINE int32_t atomic_fetch_and_add_int32(int32_t *p, int32_t x)
153 {
154   return InterlockedExchangeAdd((long *)p, x);
155 }
156 
atomic_fetch_and_or_int32(int32_t * p,int32_t x)157 ATOMIC_INLINE int32_t atomic_fetch_and_or_int32(int32_t *p, int32_t x)
158 {
159   return InterlockedOr((long *)p, x);
160 }
161 
atomic_fetch_and_and_int32(int32_t * p,int32_t x)162 ATOMIC_INLINE int32_t atomic_fetch_and_and_int32(int32_t *p, int32_t x)
163 {
164   return InterlockedAnd((long *)p, x);
165 }
166 
167 /******************************************************************************/
168 /* 8-bit operations. */
169 
170 /* Unsigned */
171 #pragma intrinsic(_InterlockedAnd8)
atomic_fetch_and_and_uint8(uint8_t * p,uint8_t b)172 ATOMIC_INLINE uint8_t atomic_fetch_and_and_uint8(uint8_t *p, uint8_t b)
173 {
174 #if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
175   return InterlockedAnd8((char *)p, (char)b);
176 #else
177   return _InterlockedAnd8((char *)p, (char)b);
178 #endif
179 }
180 
181 #pragma intrinsic(_InterlockedOr8)
atomic_fetch_and_or_uint8(uint8_t * p,uint8_t b)182 ATOMIC_INLINE uint8_t atomic_fetch_and_or_uint8(uint8_t *p, uint8_t b)
183 {
184 #if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
185   return InterlockedOr8((char *)p, (char)b);
186 #else
187   return _InterlockedOr8((char *)p, (char)b);
188 #endif
189 }
190 
191 /* Signed */
192 #pragma intrinsic(_InterlockedAnd8)
atomic_fetch_and_and_int8(int8_t * p,int8_t b)193 ATOMIC_INLINE int8_t atomic_fetch_and_and_int8(int8_t *p, int8_t b)
194 {
195 #if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
196   return InterlockedAnd8((char *)p, (char)b);
197 #else
198   return _InterlockedAnd8((char *)p, (char)b);
199 #endif
200 }
201 
202 #pragma intrinsic(_InterlockedOr8)
atomic_fetch_and_or_int8(int8_t * p,int8_t b)203 ATOMIC_INLINE int8_t atomic_fetch_and_or_int8(int8_t *p, int8_t b)
204 {
205 #if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
206   return InterlockedOr8((char *)p, (char)b);
207 #else
208   return _InterlockedOr8((char *)p, (char)b);
209 #endif
210 }
211 
212 #if defined(__clang__)
213 #  pragma GCC diagnostic pop
214 #endif
215 
216 #endif /* __ATOMIC_OPS_MSVC_H__ */
217