Skip to content

Commit d76536a

Browse files
committed
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.
1 parent db261e8 commit d76536a

File tree

6 files changed

+21
-37
lines changed

6 files changed

+21
-37
lines changed

src/lib/libatomic.js

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ addToLibrary({
149149
return numCancelled;
150150
},
151151

152+
emscripten_atomics_is_lock_free: (width) => Atomics.isLockFree(width),
153+
152154
emscripten_has_threading_support: () => typeof SharedArrayBuffer != 'undefined',
153155

154156
emscripten_num_logical_cores: () =>

src/lib/libsigs.js

-1
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,6 @@ sigs = {
710710
emscripten_math_sqrt__sig: 'dd',
711711
emscripten_math_tan__sig: 'dd',
712712
emscripten_math_tanh__sig: 'dd',
713-
emscripten_navigator_hardware_concurrency__sig: 'i',
714713
emscripten_notify_memory_growth__sig: 'vp',
715714
emscripten_num_logical_cores__sig: 'i',
716715
emscripten_out__sig: 'vp',

src/lib/libwasm_worker.js

-11
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,6 @@ if (ENVIRONMENT_IS_WASM_WORKER
284284
_wasmWorkers[id].postMessage({'_wsc': funcPtr, 'x': readEmAsmArgs(sigPtr, varargs) });
285285
},
286286

287-
emscripten_navigator_hardware_concurrency: () => {
288-
#if ENVIRONMENT_MAY_BE_NODE
289-
if (ENVIRONMENT_IS_NODE) return require('os').cpus().length;
290-
#endif
291-
return navigator['hardwareConcurrency'];
292-
},
293-
294-
emscripten_atomics_is_lock_free: (width) => {
295-
return Atomics.isLockFree(width);
296-
},
297-
298287
emscripten_lock_async_acquire__deps: ['$polyfillWaitAsync'],
299288
emscripten_lock_async_acquire: (lock, asyncWaitFinished, userData, maxWaitMilliseconds) => {
300289
let dispatch = (val, ret) => {

system/include/emscripten/atomic.h

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ extern "C" {
1717

1818
#define _EM_INLINE static __inline__ __attribute__((always_inline, nodebug))
1919

20+
// Returns true if the current environment is able to spawn threads and
21+
// the program was built with threading support enabled. If this returns 0,
22+
// calls to pthread_create() or the creation of wasm workers will fail.
23+
int emscripten_has_threading_support(void);
24+
25+
// Returns the number of logical cores on the system.
26+
int emscripten_num_logical_cores(void);
27+
28+
// Alias for emscripten_num_logical_cores
29+
#define emscripten_navigator_hardware_concurrency emscripten_num_logical_cores
30+
31+
// Returns the value of the expression "Atomics.isLockFree(byteWidth)": true if
32+
// the given memory access width can be accessed atomically, and false
33+
// otherwise. Generally will return true on 1, 2 and 4 byte accesses. On 8 byte
34+
// accesses, behavior differs across browsers, see
35+
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1246139
36+
// - https://bugs.chromium.org/p/chromium/issues/detail?id=1167449
37+
bool emscripten_atomics_is_lock_free(int byteWidth);
38+
2039
// Note on 64bit atomics ops: All 64-bit atomic ops defined here, while single
2140
// instruction under wasm, will be emulated by using locks in wasm2js mode.
2241
// This is also true for C/C++ native atomics as well as intrinsics.

system/include/emscripten/threading.h

-9
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
extern "C" {
2121
#endif
2222

23-
// Returns true if the current browser is able to spawn threads with
24-
// pthread_create(), and the compiled page was built with threading support
25-
// enabled. If this returns 0, calls to pthread_create() will fail with return
26-
// code EAGAIN.
27-
int emscripten_has_threading_support(void);
28-
29-
// Returns the number of logical cores on the system.
30-
int emscripten_num_logical_cores(void);
31-
3223
// If the given memory address contains value val, puts the calling thread to
3324
// sleep waiting for that address to be notified.
3425
// Returns -EINVAL if addr is null.

system/include/emscripten/wasm_worker.h

-16
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,6 @@ void emscripten_wasm_worker_post_function_sig(emscripten_wasm_worker_t id, void
101101
// https://github.com/WebAssembly/threads/issues/174
102102
void emscripten_wasm_worker_sleep(int64_t nanoseconds);
103103

104-
// Returns the value of navigator.hardwareConcurrency, i.e. the number of
105-
// logical threads available for the user agent. NOTE: If the execution
106-
// environment does not support navigator.hardwareConcurrency, this function
107-
// will return zero to signal no support. (If the value 1 is returned, then it
108-
// means that navigator.hardwareConcurrency is supported, but there is only one
109-
// logical thread of concurrency available)
110-
int emscripten_navigator_hardware_concurrency(void);
111-
112-
// Returns the value of the expression "Atomics.isLockFree(byteWidth)": true if
113-
// the given memory access width can be accessed atomically, and false
114-
// otherwise. Generally will return true on 1, 2 and 4 byte accesses. On 8 byte
115-
// accesses, behavior differs across browsers, see
116-
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1246139
117-
// - https://bugs.chromium.org/p/chromium/issues/detail?id=1167449
118-
int emscripten_atomics_is_lock_free(int byteWidth);
119-
120104
#define emscripten_lock_t volatile uint32_t
121105

122106
// Use with syntax "emscripten_lock_t l = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER;"

0 commit comments

Comments
 (0)