/
test_stdio_locking.c
89 lines (74 loc) · 2.16 KB
/
test_stdio_locking.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Regression test for stdio locking. If file locking is not enabled the
* threads will race to write the file output buffer and we will see lines
* that are longer or shorter then 100 characters. When locking is
* working/enabled each 100 character line will be printed separately.
*
* See:
* musl/src/stdio/__lockfile.c
* musl/src/stdio/fwrite.c
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef __EMSCRIPTEN_PTHREADS__
#include <pthread.h>
pthread_t thread[2];
#elif defined(__EMSCRIPTEN_WASM_WORKERS__)
#include <emscripten/wasm_worker.h>
emscripten_wasm_worker_t worker[2];
#else
#error Expected to be compiled with either -sWASM_WORKERS or -pthread.
#endif
#ifndef __wasm_atomics__
#error Expected to be compiled with -matomics.
#endif
#ifndef __wasm_bulk_memory__
#error Expected to be compiled with -mbulk-memory.
#endif
char *char_repeat(int n, char c) {
char *dest = malloc(n + 1);
memset(dest, c, n);
dest[n] = '\0';
return dest;
}
void thread_main() {
char *msg = char_repeat(100, 'a');
for (int i = 0; i < 10; ++i)
printf("%s\n", msg);
free(msg);
}
#ifdef __EMSCRIPTEN_WASM_WORKERS__
void terminate_worker(void *userData)
{
emscripten_terminate_all_wasm_workers();
printf("main done\n");
}
#endif
int main() {
printf("in main\n");
#ifdef __EMSCRIPTEN_PTHREADS__
void *thread_rtn;
int rc;
rc = pthread_create(&thread[0], NULL, (void* (*)(void*))thread_main, NULL);
assert(rc == 0);
rc = pthread_create(&thread[1], NULL, (void* (*)(void*))thread_main, NULL);
assert(rc == 0);
rc = pthread_join(thread[0], &thread_rtn);
assert(rc == 0);
assert(thread_rtn == 0);
rc = pthread_join(thread[1], &thread_rtn);
assert(rc == 0);
assert(thread_rtn == 0);
printf("main done\n");
#else
worker[0] = emscripten_malloc_wasm_worker(/*stack size: */1024);
worker[1] = emscripten_malloc_wasm_worker(/*stack size: */1024);
emscripten_wasm_worker_post_function_v(worker[0], (void (*))thread_main);
emscripten_wasm_worker_post_function_v(worker[1], (void (*))thread_main);
// Terminate both workers after a small delay
emscripten_set_timeout(terminate_worker, 1000, 0);
#endif
return 0;
}