diff --git a/core/iwasm/libraries/lib-wasi-threads/test/update_shared_data_and_alloc_heap.c b/core/iwasm/libraries/lib-wasi-threads/test/update_shared_data_and_alloc_heap.c index b8cf5649..2bfd196d 100644 --- a/core/iwasm/libraries/lib-wasi-threads/test/update_shared_data_and_alloc_heap.c +++ b/core/iwasm/libraries/lib-wasi-threads/test/update_shared_data_and_alloc_heap.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "wasi_thread_start.h" @@ -29,6 +30,7 @@ typedef struct { int *pval; } shared_t; +pthread_mutex_t mutex; int *vals[NUM_THREADS]; void @@ -39,7 +41,9 @@ __wasi_thread_start_C(int thread_id, int *start_arg) for (int i = 0; i < NUM_ITER; i++) __atomic_fetch_add(data->count, 1, __ATOMIC_SEQ_CST); + pthread_mutex_lock(&mutex); /* malloc is not thread-safe in wasi-libc */ vals[data->iteration] = malloc(sizeof(int)); + pthread_mutex_unlock(&mutex); *vals[data->iteration] = data->iteration; __atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST); @@ -53,6 +57,9 @@ main(int argc, char **argv) int thread_ids[NUM_THREADS]; int *count = calloc(1, sizeof(int)); + assert(count != NULL && "Failed to call calloc"); + assert(pthread_mutex_init(&mutex, NULL) == 0 && "Failed to init mutex"); + for (int i = 0; i < NUM_THREADS; i++) { assert(start_args_init(&data[i].base) && "Stack allocation for thread failed"); @@ -82,5 +89,7 @@ main(int argc, char **argv) } free(count); + assert(pthread_mutex_destroy(&mutex) == 0 && "Failed to destroy mutex"); + return EXIT_SUCCESS; }