Skip to content

Move emscripten_atomics_is_lock_free to atomic.h. NFC #23778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions src/lib/libatomic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: () =>
Expand Down
1 change: 0 additions & 1 deletion src/lib/libsigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 0 additions & 11 deletions src/lib/libwasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
19 changes: 19 additions & 0 deletions system/include/emscripten/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions system/include/emscripten/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 0 additions & 16 deletions system/include/emscripten/wasm_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
Expand Down