Skip to content

Commit 2d87749

Browse files
committed
fix(wasi-threads): restore no_pthread sample and update thread_termination sample to use blocking operations
1 parent 669d7f1 commit 2d87749

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

core/shared/platform/include/platform_api_extension.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ extern "C" {
2424
* A list is used to handles stack contexts created in nested functions (for the
2525
* same thread). Stack contexts are used by the signal handler to restore a
2626
* previously saved state.
27-
*
2827
*/
2928
typedef struct {
3029
void *contexts;

samples/wasi-threads/wasm-apps/no_pthread.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#include "wasi_thread_start.h"
1414

15-
#define STOP_FROM_MAIN 1
16-
1715
static const int64_t SECOND = 1000 * 1000 * 1000;
1816

1917
typedef struct {
@@ -31,11 +29,12 @@ __wasi_thread_start_C(int thread_id, int *start_arg)
3129
printf("New thread ID: %d, starting parameter: %d\n", thread_id,
3230
data->value);
3331

34-
#if STOP_FROM_MAIN == 1
35-
__builtin_wasm_memory_atomic_wait32(0, 0, -1);
36-
#else
37-
__wasi_proc_exit(55);
38-
#endif
32+
data->thread_id = thread_id;
33+
data->value += 8;
34+
printf("Updated value: %d\n", data->value);
35+
36+
data->th_ready = 1;
37+
__builtin_wasm_memory_atomic_notify(&data->th_ready, 1);
3938
}
4039

4140
int
@@ -57,11 +56,16 @@ main(int argc, char **argv)
5756
goto final;
5857
}
5958

60-
#if STOP_FROM_MAIN == 1
61-
__wasi_proc_exit(56);
62-
#else
63-
__builtin_wasm_memory_atomic_wait32(0, 0, -1);
64-
#endif
59+
if (__builtin_wasm_memory_atomic_wait32(&data.th_ready, 0, SECOND) == 2) {
60+
printf("Timeout\n");
61+
ret = EXIT_FAILURE;
62+
goto final;
63+
}
64+
65+
printf("Thread completed, new value: %d, thread id: %d\n", data.value,
66+
data.thread_id);
67+
68+
assert(thread_id == data.thread_id);
6569

6670
final:
6771
start_args_deinit(&data.base);

samples/wasi-threads/wasm-apps/thread_termination.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#define TEST_TERMINATION_BY_TRAP 0 // Otherwise test `proc_exit` termination
1919
#define TEST_TERMINATION_IN_MAIN_THREAD 1
20+
#define TEST_ATOMIC_WAIT 1 // Otherwise test `sleep`
2021

2122
#define TIMEOUT_SECONDS 10
2223
#define NUM_THREADS 3
@@ -30,9 +31,11 @@ typedef struct {
3031
void
3132
run_long_task()
3233
{
33-
// Busy waiting to be interruptible by trap or `proc_exit`
34-
for (int i = 0; i < TIMEOUT_SECONDS; i++)
35-
sleep(1);
34+
#if TEST_ATOMIC_WAIT == 1
35+
__builtin_wasm_memory_atomic_wait32(0, 0, -1);
36+
#else
37+
sleep(TIMEOUT_SECONDS);
38+
#endif
3639
}
3740

3841
void

0 commit comments

Comments
 (0)