Skip to content

Commit 317c35d

Browse files
authored
Add testing for compiler built-in atomic_is_lock_free API. NFC (#26646)
1 parent f2ad693 commit 317c35d

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

test/pthread/is_lock_free.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
#include <emscripten/atomic.h>
2-
#include <pthread.h>
31
#include <assert.h>
2+
#include <pthread.h>
3+
#include <stdatomic.h>
44
#include <stdio.h>
55

6-
// Test emscripten_atomics_is_lock_free() functions
6+
#include <emscripten/atomic.h>
7+
8+
// Test the various `is_lock_free` functions:
9+
//
10+
// - emscripten_atomics_is_lock_free
11+
// - __atomic_always_lock_free
12+
// - atomic_is_lock_free
713

814
void test() {
915
assert(emscripten_atomics_is_lock_free(1));
@@ -12,7 +18,30 @@ void test() {
1218
// Chrome is buggy, see
1319
// https://bugs.chromium.org/p/chromium/issues/detail?id=1167449
1420
//assert(emscripten_atomics_is_lock_free(8));
21+
assert(!emscripten_atomics_is_lock_free(16));
1522
assert(!emscripten_atomics_is_lock_free(31));
23+
24+
// Test compiler buildin __atomic_always_lock_free version
25+
assert(__atomic_always_lock_free(1, 0));
26+
assert(__atomic_always_lock_free(2, 0));
27+
assert(__atomic_always_lock_free(4, 0));
28+
assert(__atomic_always_lock_free(8, 0));
29+
assert(!__atomic_always_lock_free(16, 0));
30+
assert(!__atomic_always_lock_free(31, 0));
31+
32+
// Test C11 atomic_is_lock_free
33+
struct { char a[1]; } one;
34+
struct { char a[2]; } two;
35+
struct { char a[4]; } four;
36+
struct { char a[8]; } eight;
37+
struct { char a[16]; } sixteen;
38+
struct { char a[31]; } thirty_one;
39+
assert(atomic_is_lock_free(&one));
40+
assert(atomic_is_lock_free(&two));
41+
assert(atomic_is_lock_free(&four));
42+
assert(atomic_is_lock_free(&eight));
43+
assert(!atomic_is_lock_free(&sixteen));
44+
assert(!atomic_is_lock_free(&thirty_one));
1645
}
1746

1847
void* thread_main(void* arg) {

test/wasm_worker/hardware_concurrency_is_lock_free.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <emscripten.h>
22
#include <emscripten/wasm_worker.h>
33
#include <assert.h>
4+
#include <stdatomic.h>
45

56
// Test emscripten_navigator_hardware_concurrency() and emscripten_atomics_is_lock_free() functions
67

@@ -13,7 +14,30 @@ void test() {
1314
// Chrome is buggy, see
1415
// https://bugs.chromium.org/p/chromium/issues/detail?id=1167449
1516
//assert(emscripten_atomics_is_lock_free(8));
17+
assert(!emscripten_atomics_is_lock_free(16));
1618
assert(!emscripten_atomics_is_lock_free(31));
19+
20+
// Test compiler buildin __atomic_always_lock_free version
21+
assert(__atomic_always_lock_free(1, 0));
22+
assert(__atomic_always_lock_free(2, 0));
23+
assert(__atomic_always_lock_free(4, 0));
24+
assert(__atomic_always_lock_free(8, 0));
25+
assert(!__atomic_always_lock_free(16, 0));
26+
assert(!__atomic_always_lock_free(31, 0));
27+
28+
// Test C11 atomic_is_lock_free
29+
struct { char a[1]; } one;
30+
struct { char a[2]; } two;
31+
struct { char a[4]; } four;
32+
struct { char a[8]; } eight;
33+
struct { char a[16]; } sixteen;
34+
struct { char a[31]; } thirty_one;
35+
assert(atomic_is_lock_free(&one));
36+
assert(atomic_is_lock_free(&two));
37+
assert(atomic_is_lock_free(&four));
38+
assert(atomic_is_lock_free(&eight));
39+
assert(!atomic_is_lock_free(&sixteen));
40+
assert(!atomic_is_lock_free(&thirty_one));
1741
}
1842

1943
void worker_main() {

0 commit comments

Comments
 (0)