|
| 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 | +#ifndef __wasi__ |
| 7 | +#error This example only compiles to WASM/WASI target |
| 8 | +#endif |
| 9 | + |
| 10 | +#include <stdlib.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <assert.h> |
| 13 | +#include <stdbool.h> |
| 14 | + |
| 15 | +#include "wasi_thread_start.h" |
| 16 | + |
| 17 | +enum CONSTANTS { |
| 18 | + MAX_NUM_THREADS = 4, /* Should be the same as "--max-threads" */ |
| 19 | + NUM_RETRY = 5, |
| 20 | + SECOND = 1000 * 1000 * 1000, /* 1 second */ |
| 21 | + TIMEOUT = 10LL * SECOND |
| 22 | +}; |
| 23 | + |
| 24 | +int g_count = 0; |
| 25 | + |
| 26 | +typedef struct { |
| 27 | + start_args_t base; |
| 28 | + int th_ready; |
| 29 | + int th_continue; |
| 30 | + int th_done; |
| 31 | + bool no_ops; |
| 32 | +} shared_t; |
| 33 | + |
| 34 | +void |
| 35 | +__wasi_thread_start_C(int thread_id, int *start_arg) |
| 36 | +{ |
| 37 | + shared_t *data = (shared_t *)start_arg; |
| 38 | + |
| 39 | + if (data->no_ops) { |
| 40 | + __builtin_wasm_memory_atomic_wait32(NULL, 0, 2 * SECOND); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + __atomic_store_n(&data->th_ready, 1, __ATOMIC_SEQ_CST); |
| 45 | + __builtin_wasm_memory_atomic_notify(&data->th_ready, 1); |
| 46 | + |
| 47 | + if (__builtin_wasm_memory_atomic_wait32(&data->th_continue, 0, TIMEOUT) |
| 48 | + == 2) { |
| 49 | + assert(false && "Wait should not time out"); |
| 50 | + } |
| 51 | + |
| 52 | + __atomic_fetch_add(&g_count, 1, __ATOMIC_SEQ_CST); |
| 53 | + |
| 54 | + __atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST); |
| 55 | + __builtin_wasm_memory_atomic_notify(&data->th_done, 1); |
| 56 | +} |
| 57 | + |
| 58 | +int |
| 59 | +main(int argc, char **argv) |
| 60 | +{ |
| 61 | + shared_t data[MAX_NUM_THREADS] = { 0 }; |
| 62 | + int thread_ids[MAX_NUM_THREADS]; |
| 63 | + |
| 64 | + for (int i = 0; i < MAX_NUM_THREADS; i++) { |
| 65 | + assert(start_args_init(&data[i].base)); |
| 66 | + thread_ids[i] = __wasi_thread_spawn(&data[i]); |
| 67 | + printf("Thread created with id=%d\n", thread_ids[i]); |
| 68 | + assert(thread_ids[i] > 0 && "Thread creation failed"); |
| 69 | + |
| 70 | + for (int j = 0; j < i; j++) { |
| 71 | + assert(thread_ids[i] != thread_ids[j] && "Duplicated TIDs"); |
| 72 | + } |
| 73 | + |
| 74 | + if (__builtin_wasm_memory_atomic_wait32(&data[i].th_ready, 0, TIMEOUT) |
| 75 | + == 2) { |
| 76 | + assert(false && "Wait should not time out"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + printf("Attempt to create thread when not possible\n"); |
| 81 | + shared_t data_fail = { 0 }; |
| 82 | + assert(start_args_init(&data_fail.base)); |
| 83 | + int thread_id = __wasi_thread_spawn(&data_fail); |
| 84 | + start_args_deinit(&data_fail.base); |
| 85 | + assert(thread_id < 0 && "Thread creation should fail"); |
| 86 | + |
| 87 | + printf("Unlock created threads\n"); |
| 88 | + for (int i = 0; i < MAX_NUM_THREADS; i++) { |
| 89 | + __atomic_store_n(&data[i].th_continue, 1, __ATOMIC_SEQ_CST); |
| 90 | + __builtin_wasm_memory_atomic_notify(&data[i].th_continue, 1); |
| 91 | + } |
| 92 | + |
| 93 | + printf("Wait for threads to finish\n"); |
| 94 | + for (int i = 0; i < MAX_NUM_THREADS; i++) { |
| 95 | + if (__builtin_wasm_memory_atomic_wait32(&data[i].th_done, 0, TIMEOUT) |
| 96 | + == 2) { |
| 97 | + assert(false && "Wait should not time out"); |
| 98 | + } |
| 99 | + |
| 100 | + start_args_deinit(&data[i].base); |
| 101 | + } |
| 102 | + |
| 103 | + printf("Value of count after update: %d\n", g_count); |
| 104 | + assert(g_count == (MAX_NUM_THREADS) |
| 105 | + && "Global count not updated correctly"); |
| 106 | + |
| 107 | + /* --------------------------------------------------- */ |
| 108 | + |
| 109 | + printf("Create new threads without waiting from them to finish\n"); |
| 110 | + shared_t data_no_join[MAX_NUM_THREADS] = { 0 }; |
| 111 | + for (int i = 0; i < MAX_NUM_THREADS; i++) { |
| 112 | + /* No graceful memory free to simplify the test */ |
| 113 | + assert(start_args_init(&data_no_join[i].base)); |
| 114 | + data_no_join[i].no_ops = true; |
| 115 | + |
| 116 | + int thread_id = -1; |
| 117 | + for (int j = 0; j < NUM_RETRY && thread_id < 0; j++) { |
| 118 | + thread_id = __wasi_thread_spawn(&data_no_join[i]); |
| 119 | + if (thread_id < 0) |
| 120 | + __builtin_wasm_memory_atomic_wait32(NULL, 0, SECOND); |
| 121 | + } |
| 122 | + |
| 123 | + printf("Thread created with id=%d\n", thread_id); |
| 124 | + assert(thread_id > 0 && "Thread creation should succeed"); |
| 125 | + } |
| 126 | + |
| 127 | + return EXIT_SUCCESS; |
| 128 | +} |
0 commit comments