Skip to content

Commit f300dcb

Browse files
committed
[Support/rpmalloc] Updated fake atomics with Interlocked functions
Most atomic functions used Interlocked functions in case of MSVC (since MSVC does not do C11 yet). But few load/store functions are dummy. Use Interlocked functions for these atomics to ensure they are thread-safe. This PR fixes #146205.
1 parent 535d691 commit f300dcb

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

llvm/lib/Support/rpmalloc/rpmalloc.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,11 @@ typedef volatile long atomic32_t;
275275
typedef volatile long long atomic64_t;
276276
typedef volatile void *atomicptr_t;
277277

278-
static FORCEINLINE int32_t atomic_load32(atomic32_t *src) { return *src; }
278+
static FORCEINLINE int32_t atomic_load32(atomic32_t *src) {
279+
return (int32_t)InterlockedOr(src, 0);
280+
}
279281
static FORCEINLINE void atomic_store32(atomic32_t *dst, int32_t val) {
280-
*dst = val;
282+
InterlockedExchange(dst, val);
281283
}
282284
static FORCEINLINE int32_t atomic_incr32(atomic32_t *val) {
283285
return (int32_t)InterlockedIncrement(val);
@@ -293,20 +295,22 @@ static FORCEINLINE int atomic_cas32_acquire(atomic32_t *dst, int32_t val,
293295
return (InterlockedCompareExchange(dst, val, ref) == ref) ? 1 : 0;
294296
}
295297
static FORCEINLINE void atomic_store32_release(atomic32_t *dst, int32_t val) {
296-
*dst = val;
298+
InterlockedExchange(dst, val);
299+
}
300+
static FORCEINLINE int64_t atomic_load64(atomic64_t *src) {
301+
return (int64_t)InterlockedOr64(src, 0);
297302
}
298-
static FORCEINLINE int64_t atomic_load64(atomic64_t *src) { return *src; }
299303
static FORCEINLINE int64_t atomic_add64(atomic64_t *val, int64_t add) {
300304
return (int64_t)InterlockedExchangeAdd64(val, add) + add;
301305
}
302306
static FORCEINLINE void *atomic_load_ptr(atomicptr_t *src) {
303-
return (void *)*src;
307+
return InterlockedCompareExchangePointer(src, 0, 0);
304308
}
305309
static FORCEINLINE void atomic_store_ptr(atomicptr_t *dst, void *val) {
306-
*dst = val;
310+
InterlockedExchangePointer(dst, val);
307311
}
308312
static FORCEINLINE void atomic_store_ptr_release(atomicptr_t *dst, void *val) {
309-
*dst = val;
313+
InterlockedExchangePointer(dst, val);
310314
}
311315
static FORCEINLINE void *atomic_exchange_ptr_acquire(atomicptr_t *dst,
312316
void *val) {

0 commit comments

Comments
 (0)