Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions test/core/test_stdio_locking.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,69 @@
* musl/src/stdio/fwrite.c
*/
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>

#ifndef __wasm_atomics__
#error Expected to be compiled with -matomics.
#endif

#ifndef __wasm_bulk_memory__
#error Expected to be compiled with -mbulk-memory.
#endif

#ifdef __EMSCRIPTEN_WASM_WORKERS__
#include <emscripten/eventloop.h>
#include <emscripten/wasm_worker.h>

emscripten_wasm_worker_t worker[2];

void terminate_worker(void* userData) {
emscripten_terminate_all_wasm_workers();
printf("main done\n");
}
#elif defined(__EMSCRIPTEN_PTHREADS__)
#include <pthread.h>

pthread_t thread[2];

char *char_repeat(int n, char c) {
char *dest = malloc(n + 1);
void thread_func(void);

void* thread_main(void* arg) {
thread_func();
return 0;
}
#else
#error Expected to be compiled with either -sWASM_WORKERS or -pthread.
#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(void *arg) {
char *msg = char_repeat(100, 'a');
void thread_func(void) {
char* msg = char_repeat(100, 'a');
for (int i = 0; i < 10; ++i)
printf("%s\n", msg);
printf("%s\n", msg);
free(msg);
return 0;
}

int main() {
printf("in main\n");
void *thread_rtn;
#ifdef __EMSCRIPTEN_WASM_WORKERS__
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], thread_func);
emscripten_wasm_worker_post_function_v(worker[1], thread_func);

// Terminate both workers after a small delay
emscripten_set_timeout(terminate_worker, 1000, 0);
#else
void* thread_rtn;
int rc;

rc = pthread_create(&thread[0], NULL, thread_main, NULL);
Expand All @@ -51,5 +89,6 @@ int main() {
assert(thread_rtn == 0);

printf("main done\n");
#endif
return 0;
}
1 change: 1 addition & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9242,6 +9242,7 @@ def test_emscripten_futexes(self):
self.do_core_test('pthread/emscripten_futexes.c', cflags=['-pthread', '-Wno-nonnull'])

@requires_pthreads
@also_with_wasm_workers
def test_stdio_locking(self):
self.set_setting('PTHREAD_POOL_SIZE', '2')
self.do_core_test('test_stdio_locking.c')
Expand Down
Loading