Skip to content

Commit 0f73ce1

Browse files
authored
Update wasi-libc version in CI and implement custom sync primitives (#2028)
Update wasi-libc version to resolve the hang issue when running wasi-threads cases. Implement custom sync primitives as a counterpart of `pthread_barrier_wait` to attempt to replace pthread sync primitives since they seem to cause data races when running with the thread sanitizer.
1 parent 0faec7c commit 0f73ce1

File tree

8 files changed

+109
-153
lines changed

8 files changed

+109
-153
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,9 @@ jobs:
368368
mkdir wasi-libc
369369
cd wasi-libc
370370
git init
371-
# "Rename thread_spawn import" commit on main branch
371+
# "Fix a_store operation in atomic.h" commit on main branch
372372
git fetch https://github.com/WebAssembly/wasi-libc \
373-
8f5275796a82f8ecfd0833a4f3f444fa37ed4546
373+
1dfe5c302d1c5ab621f7abf04620fae92700fd22
374374
git checkout FETCH_HEAD
375375
make -j \
376376
AR=/opt/wasi-sdk/bin/llvm-ar \
@@ -532,9 +532,9 @@ jobs:
532532
mkdir wasi-libc
533533
cd wasi-libc
534534
git init
535-
# "Rename thread_spawn import" commit on main branch
535+
# "Fix a_store operation in atomic.h" commit on main branch
536536
git fetch https://github.com/WebAssembly/wasi-libc \
537-
8f5275796a82f8ecfd0833a4f3f444fa37ed4546
537+
1dfe5c302d1c5ab621f7abf04620fae92700fd22
538538
git checkout FETCH_HEAD
539539
make -j \
540540
AR=/opt/wasi-sdk/bin/llvm-ar \

.github/workflows/compilation_on_sgx.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ jobs:
265265
mkdir wasi-libc
266266
cd wasi-libc
267267
git init
268-
# "Rename thread_spawn import" commit on main branch
268+
# "Fix a_store operation in atomic.h" commit on main branch
269269
git fetch https://github.com/WebAssembly/wasi-libc \
270-
8f5275796a82f8ecfd0833a4f3f444fa37ed4546
270+
1dfe5c302d1c5ab621f7abf04620fae92700fd22
271271
git checkout FETCH_HEAD
272272
make \
273273
AR=/opt/wasi-sdk/bin/llvm-ar \

core/iwasm/libraries/lib-wasi-threads/test/common.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
#include <stdlib.h>
77
#include <stdio.h>
88
#include <assert.h>
9-
#include <pthread.h>
109
#include <stdbool.h>
1110
#include <unistd.h>
1211
#include <limits.h>
1312

13+
#if USE_CUSTOM_SYNC_PRIMITIVES != 0
14+
#include "sync_primitives.h"
15+
#else
16+
#include <pthread.h>
17+
#endif
18+
1419
#include "wasi_thread_start.h"
1520

1621
typedef enum {

core/iwasm/libraries/lib-wasi-threads/test/global_lock.c

+5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
#include <stdio.h>
1212
#include <assert.h>
1313
#include <stdbool.h>
14+
15+
#if USE_CUSTOM_SYNC_PRIMITIVES != 0
16+
#include "sync_primitives.h"
17+
#else
1418
#include <pthread.h>
19+
#endif
1520

1621
#include "wasi_thread_start.h"
1722

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include <stdbool.h>
7+
8+
/* Mutex */
9+
10+
typedef int pthread_mutex_t;
11+
12+
int
13+
pthread_mutex_init(pthread_mutex_t *mutex, void *unused)
14+
{
15+
*mutex = 0;
16+
return 0;
17+
}
18+
19+
int
20+
pthread_mutex_destroy(pthread_mutex_t *mutex)
21+
{
22+
return 0;
23+
}
24+
25+
static bool
26+
try_pthread_mutex_lock(pthread_mutex_t *mutex)
27+
{
28+
int expected = 0;
29+
return __atomic_compare_exchange_n(mutex, &expected, 1, false,
30+
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
31+
}
32+
33+
int
34+
pthread_mutex_lock(pthread_mutex_t *mutex)
35+
{
36+
while (!try_pthread_mutex_lock(mutex))
37+
__builtin_wasm_memory_atomic_wait32(mutex, 1, -1);
38+
return 0;
39+
}
40+
41+
int
42+
pthread_mutex_unlock(pthread_mutex_t *mutex)
43+
{
44+
__atomic_store_n(mutex, 0, __ATOMIC_SEQ_CST);
45+
__builtin_wasm_memory_atomic_notify(mutex, 1);
46+
return 0;
47+
}
48+
49+
/* Barrier */
50+
51+
typedef struct {
52+
int count;
53+
int num_threads;
54+
int mutex;
55+
int ready;
56+
} pthread_barrier_t;
57+
58+
int
59+
pthread_barrier_init(pthread_barrier_t *barrier, void *unused, int num_threads)
60+
{
61+
barrier->count = 0;
62+
barrier->num_threads = num_threads;
63+
barrier->ready = 0;
64+
pthread_mutex_init(&barrier->mutex, NULL);
65+
66+
return 0;
67+
}
68+
69+
int
70+
pthread_barrier_wait(pthread_barrier_t *barrier)
71+
{
72+
bool no_wait = false;
73+
int count;
74+
75+
pthread_mutex_lock(&barrier->mutex);
76+
count = barrier->count++;
77+
if (barrier->count >= barrier->num_threads) {
78+
no_wait = true;
79+
barrier->count = 0;
80+
}
81+
pthread_mutex_unlock(&barrier->mutex);
82+
83+
if (no_wait) {
84+
__atomic_store_n(&barrier->ready, 1, __ATOMIC_SEQ_CST);
85+
__builtin_wasm_memory_atomic_notify(&barrier->ready, count);
86+
return 0;
87+
}
88+
89+
__builtin_wasm_memory_atomic_wait32(&barrier->ready, 0, -1);
90+
return 0;
91+
}

samples/wasi-threads/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ $ cmake ..
1111
$ make
1212
...
1313
$ ./iwasm wasm-apps/no_pthread.wasm
14-
...
15-
$ ./iwasm wasm-apps/exception_propagation.wasm
1614
```
1715

1816
## Run samples in AOT mode

samples/wasi-threads/wasm-apps/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,4 @@ function (compile_sample SOURCE_FILE)
4343
)
4444
endfunction ()
4545

46-
compile_sample(no_pthread.c wasi_thread_start.S)
47-
compile_sample(thread_termination.c wasi_thread_start.S)
46+
compile_sample(no_pthread.c wasi_thread_start.S)

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

-142
This file was deleted.

0 commit comments

Comments
 (0)