1
1
#include <assert.h>
2
+ #include <stdlib.h>
2
3
#include <wasi/api.h>
3
4
4
5
static const int64_t SECOND = 1000 * 1000 * 1000 ;
5
6
6
- typedef struct {
7
+ typedef struct
8
+ {
9
+ char * stack ;
7
10
int th_ready ;
8
11
int th_continue ;
9
12
int th_done ;
@@ -12,16 +15,16 @@ typedef struct {
12
15
int value ;
13
16
} shared_t ;
14
17
15
- __attribute__((export_name ("wasi_thread_start" ))) void
16
- wasi_thread_start (int thread_id , int * start_arg )
18
+ void __wasi_thread_start_C (int thread_id , int * start_arg )
17
19
{
18
20
shared_t * data = (shared_t * )start_arg ;
19
21
20
22
data -> th_ready = 1 ;
21
23
__builtin_wasm_memory_atomic_notify (& data -> th_ready , 1 );
22
24
23
25
// so we can have all the threads alive at the same time
24
- if (__builtin_wasm_memory_atomic_wait32 (& data -> th_continue , 0 , SECOND ) == 2 ) {
26
+ if (__builtin_wasm_memory_atomic_wait32 (& data -> th_continue , 0 , SECOND ) == 2 )
27
+ {
25
28
data -> failed = 1 ;
26
29
return ;
27
30
}
@@ -35,36 +38,42 @@ wasi_thread_start(int thread_id, int *start_arg)
35
38
__builtin_wasm_memory_atomic_notify (& data -> th_done , 1 );
36
39
}
37
40
38
- int
39
- main (int argc , char * * argv )
41
+ int main (int argc , char * * argv )
40
42
{
41
- shared_t data [3 ] = { 0 };
43
+ shared_t data [3 ] = {0 };
44
+ int tid [3 ];
42
45
int data_count = sizeof (data ) / sizeof (data [0 ]);
43
46
int i , j ;
44
47
45
- for (i = 0 ; i < data_count ; i ++ ) {
48
+ for (i = 0 ; i < data_count ; i ++ )
49
+ {
50
+ data [i ].stack = malloc (128 );
46
51
data [i ].value = 52 ;
47
- assert (__wasi_thread_spawn (& data [i ]) == 0 );
52
+ tid [i ] = __wasi_thread_spawn (& data [i ]);
53
+ assert (tid [i ] > 0 );
48
54
assert (__builtin_wasm_memory_atomic_wait32 (& data [i ].th_ready , 0 ,
49
- SECOND )
50
- != 2 ); // not a timeout
55
+ SECOND ) != 2 ); // not a timeout
51
56
}
52
57
53
- for (i = 0 ; i < data_count ; i ++ ) {
58
+ for (i = 0 ; i < data_count ; i ++ )
59
+ {
54
60
__builtin_wasm_memory_atomic_notify (& data [i ].th_continue , 1 );
55
61
}
56
62
57
- for (i = 0 ; i < data_count ; i ++ ) {
63
+ for (i = 0 ; i < data_count ; i ++ )
64
+ {
58
65
assert (__builtin_wasm_memory_atomic_wait32 (& data [i ].th_done , 0 ,
59
- SECOND )
60
- != 2 ); // not a timeout
66
+ SECOND ) != 2 ); // not a timeout
67
+ assert ( data [ i ]. tid == tid [ i ]);
61
68
assert (data [i ].value == 60 );
62
69
63
- for (j = i + 1 ; j < data_count ; j ++ ) {
70
+ for (j = i + 1 ; j < data_count ; j ++ )
71
+ {
64
72
assert (data [i ].tid != data [j ].tid );
65
73
}
66
74
67
75
assert (data [i ].failed == 0 );
76
+ free (data [i ].stack );
68
77
}
69
78
70
79
return 0 ;
0 commit comments