1414
1515#include <errno.h>
1616#include <limits.h>
17+ #include <stdatomic.h>
1718#include <stddef.h>
1819#include <stdint.h>
1920#ifdef __EMSCRIPTEN_SHARED_MEMORY__
@@ -45,25 +46,19 @@ uintptr_t* emscripten_get_sbrk_ptr() {
4546// Enforce preserving a minimal alignof(maxalign_t) alignment for sbrk.
4647#define SBRK_ALIGNMENT (__alignof__(max_align_t))
4748
48- #ifdef __EMSCRIPTEN_SHARED_MEMORY__
49- #define READ_SBRK_PTR (sbrk_ptr ) (__c11_atomic_load((_Atomic(uintptr_t)*)(sbrk_ptr), __ATOMIC_SEQ_CST))
50- #else
51- #define READ_SBRK_PTR (sbrk_ptr ) (*(sbrk_ptr))
52- #endif
53-
5449void * _sbrk64 (int64_t increment ) {
5550 if (increment >= 0 ) {
5651 increment = (increment + (SBRK_ALIGNMENT - 1 )) & ~((int64_t )SBRK_ALIGNMENT - 1 );
5752 } else {
5853 increment = - (- increment & ~((int64_t )SBRK_ALIGNMENT - 1 ));
5954 }
6055
61- uintptr_t * sbrk_ptr = (uintptr_t * )emscripten_get_sbrk_ptr ();
56+ _Atomic uintptr_t * sbrk_ptr = (_Atomic uintptr_t * )emscripten_get_sbrk_ptr ();
6257
6358 // To make sbrk thread-safe, implement a CAS loop to update the
6459 // value of sbrk_ptr.
6560 while (1 ) {
66- uintptr_t old_brk = READ_SBRK_PTR ( sbrk_ptr ) ;
61+ uintptr_t old_brk = * sbrk_ptr ;
6762 int64_t new_brk64 = (int64_t )old_brk + increment ;
6863 uintptr_t new_brk = (uintptr_t )new_brk64 ;
6964 // Check for a) an over/underflow, which would indicate that we are
@@ -80,8 +75,7 @@ void *_sbrk64(int64_t increment) {
8075 // by iterating the loop body again.
8176 uintptr_t expected = old_brk ;
8277
83- __c11_atomic_compare_exchange_strong ((_Atomic (uintptr_t )* )sbrk_ptr ,
84- & expected , new_brk , __ATOMIC_SEQ_CST , __ATOMIC_SEQ_CST );
78+ atomic_compare_exchange_strong (sbrk_ptr , & expected , new_brk );
8579
8680 if (expected != old_brk ) continue ; // CAS failed, another thread raced in between.
8781#else
0 commit comments