From d76536a1a0b6f82b1aa70c2cd32a027d46bdcf3b Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 26 Feb 2025 17:16:23 -0800 Subject: [PATCH] Move `emscripten_atomics_is_lock_free` to atomic.h. NFC This function is generally useful for either pthreads or wasm worker builds. Putting it in `atomic.h` and `libatomic.js` make sense. Also remove the duplicate `emscripten_navigator_hardware_concurrency` function which duplicates the existing `emscripten_num_logical_cores`. Also, change the return type of `emscripten_atomics_is_lock_free` to be `bool`, which better represents its type. --- src/lib/libatomic.js | 2 ++ src/lib/libsigs.js | 1 - src/lib/libwasm_worker.js | 11 ----------- system/include/emscripten/atomic.h | 19 +++++++++++++++++++ system/include/emscripten/threading.h | 9 --------- system/include/emscripten/wasm_worker.h | 16 ---------------- 6 files changed, 21 insertions(+), 37 deletions(-) diff --git a/src/lib/libatomic.js b/src/lib/libatomic.js index c4e89706f7dc4..ac8c7a9355a8c 100644 --- a/src/lib/libatomic.js +++ b/src/lib/libatomic.js @@ -149,6 +149,8 @@ addToLibrary({ return numCancelled; }, + emscripten_atomics_is_lock_free: (width) => Atomics.isLockFree(width), + emscripten_has_threading_support: () => typeof SharedArrayBuffer != 'undefined', emscripten_num_logical_cores: () => diff --git a/src/lib/libsigs.js b/src/lib/libsigs.js index c5474f4847718..f358da71056df 100644 --- a/src/lib/libsigs.js +++ b/src/lib/libsigs.js @@ -710,7 +710,6 @@ sigs = { emscripten_math_sqrt__sig: 'dd', emscripten_math_tan__sig: 'dd', emscripten_math_tanh__sig: 'dd', - emscripten_navigator_hardware_concurrency__sig: 'i', emscripten_notify_memory_growth__sig: 'vp', emscripten_num_logical_cores__sig: 'i', emscripten_out__sig: 'vp', diff --git a/src/lib/libwasm_worker.js b/src/lib/libwasm_worker.js index b4a832bfdd2cf..890b7867dada6 100644 --- a/src/lib/libwasm_worker.js +++ b/src/lib/libwasm_worker.js @@ -284,17 +284,6 @@ if (ENVIRONMENT_IS_WASM_WORKER _wasmWorkers[id].postMessage({'_wsc': funcPtr, 'x': readEmAsmArgs(sigPtr, varargs) }); }, - emscripten_navigator_hardware_concurrency: () => { -#if ENVIRONMENT_MAY_BE_NODE - if (ENVIRONMENT_IS_NODE) return require('os').cpus().length; -#endif - return navigator['hardwareConcurrency']; - }, - - emscripten_atomics_is_lock_free: (width) => { - return Atomics.isLockFree(width); - }, - emscripten_lock_async_acquire__deps: ['$polyfillWaitAsync'], emscripten_lock_async_acquire: (lock, asyncWaitFinished, userData, maxWaitMilliseconds) => { let dispatch = (val, ret) => { diff --git a/system/include/emscripten/atomic.h b/system/include/emscripten/atomic.h index 58075fdef45e2..4717ebab19388 100644 --- a/system/include/emscripten/atomic.h +++ b/system/include/emscripten/atomic.h @@ -17,6 +17,25 @@ extern "C" { #define _EM_INLINE static __inline__ __attribute__((always_inline, nodebug)) +// Returns true if the current environment is able to spawn threads and +// the program was built with threading support enabled. If this returns 0, +// calls to pthread_create() or the creation of wasm workers will fail. +int emscripten_has_threading_support(void); + +// Returns the number of logical cores on the system. +int emscripten_num_logical_cores(void); + +// Alias for emscripten_num_logical_cores +#define emscripten_navigator_hardware_concurrency emscripten_num_logical_cores + +// Returns the value of the expression "Atomics.isLockFree(byteWidth)": true if +// the given memory access width can be accessed atomically, and false +// otherwise. Generally will return true on 1, 2 and 4 byte accesses. On 8 byte +// accesses, behavior differs across browsers, see +// - https://bugzilla.mozilla.org/show_bug.cgi?id=1246139 +// - https://bugs.chromium.org/p/chromium/issues/detail?id=1167449 +bool emscripten_atomics_is_lock_free(int byteWidth); + // Note on 64bit atomics ops: All 64-bit atomic ops defined here, while single // instruction under wasm, will be emulated by using locks in wasm2js mode. // This is also true for C/C++ native atomics as well as intrinsics. diff --git a/system/include/emscripten/threading.h b/system/include/emscripten/threading.h index f4f68d288db42..acb998509594a 100644 --- a/system/include/emscripten/threading.h +++ b/system/include/emscripten/threading.h @@ -20,15 +20,6 @@ extern "C" { #endif -// Returns true if the current browser is able to spawn threads with -// pthread_create(), and the compiled page was built with threading support -// enabled. If this returns 0, calls to pthread_create() will fail with return -// code EAGAIN. -int emscripten_has_threading_support(void); - -// Returns the number of logical cores on the system. -int emscripten_num_logical_cores(void); - // If the given memory address contains value val, puts the calling thread to // sleep waiting for that address to be notified. // Returns -EINVAL if addr is null. diff --git a/system/include/emscripten/wasm_worker.h b/system/include/emscripten/wasm_worker.h index d46c809ffb685..a91ca2ae255b8 100644 --- a/system/include/emscripten/wasm_worker.h +++ b/system/include/emscripten/wasm_worker.h @@ -101,22 +101,6 @@ void emscripten_wasm_worker_post_function_sig(emscripten_wasm_worker_t id, void // https://github.com/WebAssembly/threads/issues/174 void emscripten_wasm_worker_sleep(int64_t nanoseconds); -// Returns the value of navigator.hardwareConcurrency, i.e. the number of -// logical threads available for the user agent. NOTE: If the execution -// environment does not support navigator.hardwareConcurrency, this function -// will return zero to signal no support. (If the value 1 is returned, then it -// means that navigator.hardwareConcurrency is supported, but there is only one -// logical thread of concurrency available) -int emscripten_navigator_hardware_concurrency(void); - -// Returns the value of the expression "Atomics.isLockFree(byteWidth)": true if -// the given memory access width can be accessed atomically, and false -// otherwise. Generally will return true on 1, 2 and 4 byte accesses. On 8 byte -// accesses, behavior differs across browsers, see -// - https://bugzilla.mozilla.org/show_bug.cgi?id=1246139 -// - https://bugs.chromium.org/p/chromium/issues/detail?id=1167449 -int emscripten_atomics_is_lock_free(int byteWidth); - #define emscripten_lock_t volatile uint32_t // Use with syntax "emscripten_lock_t l = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER;"