Skip to content

Commit 0bb48f6

Browse files
authored
Use stdatomic in sbrk.c. NFC (#26509)
1 parent fce11ef commit 0bb48f6

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

system/lib/libc/sbrk.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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-
5449
void *_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

test/codesize/test_codesize_hello_dylink.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 26256,
33
"a.out.js.gz": 11230,
4-
"a.out.nodebug.wasm": 17670,
5-
"a.out.nodebug.wasm.gz": 8926,
6-
"total": 43926,
7-
"total_gz": 20156,
4+
"a.out.nodebug.wasm": 17668,
5+
"a.out.nodebug.wasm.gz": 8921,
6+
"total": 43924,
7+
"total_gz": 20151,
88
"sent": [
99
"__syscall_stat64",
1010
"emscripten_resize_heap",

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"a.out.js": 244367,
3-
"a.out.nodebug.wasm": 577477,
4-
"total": 821844,
3+
"a.out.nodebug.wasm": 577484,
4+
"total": 821851,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

0 commit comments

Comments
 (0)