Skip to content

Commit 112812d

Browse files
authored
enable thread safety for sbrk and emmalloc, when using WASM_WORKERS (#18174)
* enable thread safety for sbrk and emmalloc, when using WASM_WORKERS * test/code_size/hello_wasm_worker_wasm.json: rebaseline * enable thread safety for sbrk and emmalloc, when using WASM_WORKERS - use __EMSCRIPTEN_SHARED_MEMORY__ for shared pthread/wasm workers cases * enable thread safety for sbrk and emmalloc, when using WASM_WORKERS - remove redundant RETRY_SBRK Co-authored-by: kme <none>
1 parent dd6774a commit 112812d

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

system/lib/emmalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ typedef struct RootRegion
107107
uint8_t* endPtr;
108108
} RootRegion;
109109

110-
#if defined(__EMSCRIPTEN_PTHREADS__)
110+
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
111111
// In multithreaded builds, use a simple global spinlock strategy to acquire/release access to the memory allocator.
112112
static volatile uint8_t multithreadingLock = 0;
113113
#define MALLOC_ACQUIRE() while(__sync_lock_test_and_set(&multithreadingLock, 1)) { while(multithreadingLock) { /*nop*/ } }

system/lib/sbrk.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <limits.h>
1313
#include <stddef.h>
1414
#include <stdint.h>
15-
#if __EMSCRIPTEN_PTHREADS__ // for error handling, see below
15+
#ifdef __EMSCRIPTEN_SHARED_MEMORY__ // for error handling, see below
1616
#include <stdio.h>
1717
#include <stdlib.h>
1818
#endif
@@ -54,16 +54,16 @@ void *sbrk(intptr_t increment_) {
5454
uintptr_t old_size;
5555
uintptr_t increment = (uintptr_t)increment_;
5656
increment = (increment + (SBRK_ALIGNMENT-1)) & ~(SBRK_ALIGNMENT-1);
57-
#if __EMSCRIPTEN_PTHREADS__
57+
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
5858
// Our default dlmalloc uses locks around each malloc/free, so no additional
5959
// work is necessary to keep things threadsafe, but we also make sure sbrk
6060
// itself is threadsafe so alternative allocators work. We do that by looping
6161
// and retrying if we hit interference with another thread.
6262
uintptr_t expected;
6363
while (1) {
64-
#endif // __EMSCRIPTEN_PTHREADS__
64+
#endif // __EMSCRIPTEN_SHARED_MEMORY__
6565
uintptr_t* sbrk_ptr = emscripten_get_sbrk_ptr();
66-
#if __EMSCRIPTEN_PTHREADS__
66+
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
6767
uintptr_t old_brk = __c11_atomic_load((_Atomic(uintptr_t)*)sbrk_ptr, __ATOMIC_SEQ_CST);
6868
#else
6969
uintptr_t old_brk = *sbrk_ptr;
@@ -81,7 +81,7 @@ void *sbrk(intptr_t increment_) {
8181
goto Error;
8282
}
8383
}
84-
#if __EMSCRIPTEN_PTHREADS__
84+
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
8585
// Attempt to update the dynamic top to new value. Another thread may have
8686
// beat this one to the update, in which case we will need to start over
8787
// by iterating the loop body again.
@@ -93,26 +93,26 @@ void *sbrk(intptr_t increment_) {
9393
if (expected != old_brk) {
9494
continue;
9595
}
96-
#else // __EMSCRIPTEN_PTHREADS__
96+
#else // __EMSCRIPTEN_SHARED_MEMORY__
9797
*sbrk_ptr = new_brk;
98-
#endif // __EMSCRIPTEN_PTHREADS__
98+
#endif // __EMSCRIPTEN_SHARED_MEMORY__
9999

100100
#ifdef __EMSCRIPTEN_TRACING__
101101
emscripten_memprof_sbrk_grow(old_brk, new_brk);
102102
#endif
103103
return (void*)old_brk;
104104

105-
#if __EMSCRIPTEN_PTHREADS__
105+
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
106106
}
107-
#endif // __EMSCRIPTEN_PTHREADS__
107+
#endif // __EMSCRIPTEN_SHARED_MEMORY__
108108

109109
Error:
110110
SET_ERRNO();
111111
return (void*)-1;
112112
}
113113

114114
int brk(void* ptr) {
115-
#if __EMSCRIPTEN_PTHREADS__
115+
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
116116
// FIXME
117117
printf("brk() is not theadsafe yet, https://github.com/emscripten-core/emscripten/issues/10006");
118118
abort();

test/code_size/hello_wasm_worker_wasm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"a.html.gz": 433,
44
"a.js": 733,
55
"a.js.gz": 463,
6-
"a.wasm": 1805,
7-
"a.wasm.gz": 1002,
6+
"a.wasm": 1861,
7+
"a.wasm.gz": 1031,
88
"total": 3275,
99
"total_gz": 1898
1010
}

0 commit comments

Comments
 (0)