Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/lib/emmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ typedef struct RootRegion
uint8_t* endPtr;
} RootRegion;

#if defined(__EMSCRIPTEN_PTHREADS__)
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
// In multithreaded builds, use a simple global spinlock strategy to acquire/release access to the memory allocator.
static volatile uint8_t multithreadingLock = 0;
#define MALLOC_ACQUIRE() while(__sync_lock_test_and_set(&multithreadingLock, 1)) { while(multithreadingLock) { /*nop*/ } }
Expand Down
20 changes: 10 additions & 10 deletions system/lib/sbrk.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#if __EMSCRIPTEN_PTHREADS__ // for error handling, see below
#ifdef __EMSCRIPTEN_SHARED_MEMORY__ // for error handling, see below
#include <stdio.h>
#include <stdlib.h>
#endif
Expand Down Expand Up @@ -54,16 +54,16 @@ void *sbrk(intptr_t increment_) {
uintptr_t old_size;
uintptr_t increment = (uintptr_t)increment_;
increment = (increment + (SBRK_ALIGNMENT-1)) & ~(SBRK_ALIGNMENT-1);
#if __EMSCRIPTEN_PTHREADS__
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
// Our default dlmalloc uses locks around each malloc/free, so no additional
// work is necessary to keep things threadsafe, but we also make sure sbrk
// itself is threadsafe so alternative allocators work. We do that by looping
// and retrying if we hit interference with another thread.
uintptr_t expected;
while (1) {
#endif // __EMSCRIPTEN_PTHREADS__
#endif // __EMSCRIPTEN_SHARED_MEMORY__
uintptr_t* sbrk_ptr = emscripten_get_sbrk_ptr();
#if __EMSCRIPTEN_PTHREADS__
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
uintptr_t old_brk = __c11_atomic_load((_Atomic(uintptr_t)*)sbrk_ptr, __ATOMIC_SEQ_CST);
#else
uintptr_t old_brk = *sbrk_ptr;
Expand All @@ -81,7 +81,7 @@ void *sbrk(intptr_t increment_) {
goto Error;
}
}
#if __EMSCRIPTEN_PTHREADS__
#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.
Expand All @@ -93,26 +93,26 @@ void *sbrk(intptr_t increment_) {
if (expected != old_brk) {
continue;
}
#else // __EMSCRIPTEN_PTHREADS__
#else // __EMSCRIPTEN_SHARED_MEMORY__
*sbrk_ptr = new_brk;
#endif // __EMSCRIPTEN_PTHREADS__
#endif // __EMSCRIPTEN_SHARED_MEMORY__

#ifdef __EMSCRIPTEN_TRACING__
emscripten_memprof_sbrk_grow(old_brk, new_brk);
#endif
return (void*)old_brk;

#if __EMSCRIPTEN_PTHREADS__
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
}
#endif // __EMSCRIPTEN_PTHREADS__
#endif // __EMSCRIPTEN_SHARED_MEMORY__

Error:
SET_ERRNO();
return (void*)-1;
}

int brk(void* ptr) {
#if __EMSCRIPTEN_PTHREADS__
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
// FIXME
printf("brk() is not theadsafe yet, https://github.com/emscripten-core/emscripten/issues/10006");
abort();
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/hello_wasm_worker_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 433,
"a.js": 733,
"a.js.gz": 463,
"a.wasm": 1805,
"a.wasm.gz": 1002,
"a.wasm": 1861,
"a.wasm.gz": 1031,
"total": 3275,
"total_gz": 1898
}