Skip to content

Commit 4e24532

Browse files
authored
Move emscripten_atomics_is_lock_free to atomic.h. (#26427)
This function really belongs alongside the atomics operations and should be available under pthreads too.
1 parent d6cfd73 commit 4e24532

6 files changed

Lines changed: 44 additions & 12 deletions

File tree

src/lib/libatomic.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,6 @@ addToLibrary({
164164
ENVIRONMENT_IS_NODE ? require('node:os').cpus().length :
165165
#endif
166166
navigator['hardwareConcurrency'],
167+
168+
emscripten_atomics_is_lock_free: (width) => Atomics.isLockFree(width),
167169
});

src/lib/libwasm_worker.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,6 @@ if (ENVIRONMENT_IS_WASM_WORKER
296296
return navigator['hardwareConcurrency'];
297297
},
298298

299-
emscripten_atomics_is_lock_free: (width) => {
300-
return Atomics.isLockFree(width);
301-
},
302-
303299
emscripten_lock_async_acquire__deps: ['$polyfillWaitAsync'],
304300
emscripten_lock_async_acquire: (lock, asyncWaitFinished, userData, maxWaitMilliseconds) => {
305301
let tryAcquireLock = () => {

system/include/emscripten/atomic.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ int emscripten_atomic_cancel_all_wait_asyncs(void);
290290
// address. Returns the number of async waits canceled.
291291
int emscripten_atomic_cancel_all_wait_asyncs_at_address(void *addr __attribute__((nonnull)));
292292

293+
// Returns the value of the expression "Atomics.isLockFree(byteWidth)": true if
294+
// the given memory access width can be accessed atomically, and false
295+
// otherwise. Generally will return true on 1, 2 and 4 byte accesses. On 8 byte
296+
// accesses, behavior differs across browsers, see
297+
// - https://bugzil.la/1246139
298+
// - https://bugs.chromium.org/p/chromium/issues/detail?id=1167449
299+
bool emscripten_atomics_is_lock_free(int byteWidth);
300+
293301
#undef _EM_INLINE
294302

295303
#ifdef __cplusplus

system/include/emscripten/wasm_worker.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ void emscripten_wasm_worker_sleep(int64_t nanoseconds);
110110
// logical thread of concurrency available)
111111
int emscripten_navigator_hardware_concurrency(void);
112112

113-
// Returns the value of the expression "Atomics.isLockFree(byteWidth)": true if
114-
// the given memory access width can be accessed atomically, and false
115-
// otherwise. Generally will return true on 1, 2 and 4 byte accesses. On 8 byte
116-
// accesses, behavior differs across browsers, see
117-
// - https://bugzil.la/1246139
118-
// - https://bugs.chromium.org/p/chromium/issues/detail?id=1167449
119-
bool emscripten_atomics_is_lock_free(int byteWidth);
120-
121113
#define emscripten_lock_t volatile uint32_t
122114

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

test/pthread/is_lock_free.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <emscripten/atomic.h>
2+
#include <pthread.h>
3+
#include <assert.h>
4+
#include <stdio.h>
5+
6+
// Test emscripten_atomics_is_lock_free() functions
7+
8+
void test() {
9+
assert(emscripten_atomics_is_lock_free(1));
10+
assert(emscripten_atomics_is_lock_free(2));
11+
assert(emscripten_atomics_is_lock_free(4));
12+
// Chrome is buggy, see
13+
// https://bugs.chromium.org/p/chromium/issues/detail?id=1167449
14+
//assert(emscripten_atomics_is_lock_free(8));
15+
assert(!emscripten_atomics_is_lock_free(31));
16+
}
17+
18+
void* thread_main(void* arg) {
19+
test();
20+
return NULL;
21+
}
22+
23+
int main() {
24+
test();
25+
pthread_t t;
26+
pthread_create(&t, NULL, thread_main, NULL);
27+
pthread_join(t, NULL);
28+
printf("done\n");
29+
return 0;
30+
}

test/test_core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,6 +2753,10 @@ def test_pthread_run_on_main_thread(self):
27532753
self.skipTest('MINIMAL_RUNTIME + threads + asan does not work')
27542754
self.do_run_in_out_file_test('pthread/test_pthread_run_on_main_thread.c')
27552755

2756+
@requires_pthreads
2757+
def test_pthread_is_lock_free(self):
2758+
self.do_runf('pthread/is_lock_free.c', 'done\n', cflags=['-pthread'])
2759+
27562760
def test_tcgetattr(self):
27572761
self.do_runf('termios/test_tcgetattr.c', 'success')
27582762

0 commit comments

Comments
 (0)