Skip to content

Commit

Permalink
Address review and rebaseline wasm2js tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
juj committed Nov 29, 2023
1 parent ed71e39 commit 8a37be9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 25 deletions.
19 changes: 6 additions & 13 deletions system/lib/libc/sbrk.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,15 @@ uintptr_t* emscripten_get_sbrk_ptr() {
// Enforce preserving a minimal alignof(maxalign_t) alignment for sbrk.
#define SBRK_ALIGNMENT (__alignof__(max_align_t))

#ifdef __EMSCRIPTEN_SHARED_MEMORY__
#define READ_SBRK_PTR(sbrk_ptr) (__c11_atomic_load((_Atomic(uintptr_t)*)(sbrk_ptr), __ATOMIC_SEQ_CST))
#else
#define READ_SBRK_PTR(sbrk_ptr) (*(sbrk_ptr))
#endif

void *sbrk(intptr_t increment_) {
uintptr_t increment = (uintptr_t)increment_;
increment = (increment + (SBRK_ALIGNMENT-1)) & ~(SBRK_ALIGNMENT-1);
uintptr_t* sbrk_ptr = emscripten_get_sbrk_ptr();
_Atomic(uintptr_t)* sbrk_ptr = (_Atomic(uintptr_t)*)emscripten_get_sbrk_ptr();

// To make sbrk thread-safe, implement a CAS loop to update the
// value of sbrk_ptr.
while (1) {
uintptr_t old_brk = READ_SBRK_PTR(sbrk_ptr);
uintptr_t old_brk = __c11_atomic_load(sbrk_ptr, __ATOMIC_SEQ_CST);
uintptr_t new_brk = old_brk + increment;
// Check for a) an overflow, which would indicate that we are trying to
// allocate over maximum addressable memory. and b) if necessary,
Expand All @@ -82,16 +76,15 @@ void *sbrk(intptr_t increment_) {
SET_ERRNO();
return (void*)-1;
}
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
// Attempt to update the dynamic top to new value. Another thread may have
// beat this one to the update, in which case we will need to start over
// by iterating the loop body again.
uintptr_t expected = old_brk;

#ifdef __EMSCRIPTEN_SHARED_MEMORY__
__c11_atomic_compare_exchange_strong(
(_Atomic(uintptr_t)*)sbrk_ptr,
&expected, new_brk,
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
__c11_atomic_compare_exchange_strong(sbrk_ptr,
&expected, new_brk, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);

if (expected != old_brk) continue; // CAS failed, another thread raced in between.
#else
*sbrk_ptr = new_brk;
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"a.js": 4699,
"a.js.gz": 2419,
"a.wasm": 10482,
"a.wasm.gz": 6726,
"a.wasm.gz": 6728,
"total": 15750,
"total_gz": 9524
"total_gz": 9526
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 18004,
"a.js.gz": 8135,
"a.js": 18025,
"a.js.gz": 8138,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21694,
"total_gz": 11207
"total": 21715,
"total_gz": 11210
}
4 changes: 2 additions & 2 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"a.js": 4185,
"a.js.gz": 2243,
"a.wasm": 10482,
"a.wasm.gz": 6726,
"a.wasm.gz": 6728,
"total": 15236,
"total_gz": 9348
"total_gz": 9350
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17481,
"a.js.gz": 7960,
"a.js": 17502,
"a.js.gz": 7961,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21171,
"total_gz": 11032
"total": 21192,
"total_gz": 11033
}

0 comments on commit 8a37be9

Please sign in to comment.