| 122 | } |
| 123 | |
| 124 | int main() { |
| 125 | // Initialize. |
| 126 | wasm_engine_t *engine = wasm_engine_new(); |
| 127 | |
| 128 | // Load our input file to parse it next |
| 129 | FILE *file = fopen("examples/threads.wat", "r"); |
| 130 | if (!file) { |
| 131 | printf("> Error loading file!\n"); |
| 132 | return 1; |
| 133 | } |
| 134 | fseek(file, 0L, SEEK_END); |
| 135 | size_t file_size = ftell(file); |
| 136 | fseek(file, 0L, SEEK_SET); |
| 137 | wasm_byte_vec_t wat; |
| 138 | wasm_byte_vec_new_uninitialized(&wat, file_size); |
| 139 | if (fread(wat.data, file_size, 1, file) != 1) { |
| 140 | printf("> Error loading module!\n"); |
| 141 | return 1; |
| 142 | } |
| 143 | fclose(file); |
| 144 | |
| 145 | // Parse the wat into the binary wasm format |
| 146 | wasm_byte_vec_t binary; |
| 147 | wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &binary); |
| 148 | if (error != NULL) |
| 149 | exit_with_error("failed to parse wat", error, NULL); |
| 150 | wasm_byte_vec_delete(&wat); |
| 151 | |
| 152 | // Compile and share. |
| 153 | own wasm_store_t *store = wasm_store_new(engine); |
| 154 | own wasm_module_t *module = wasm_module_new(store, &binary); |
| 155 | if (!module) { |
| 156 | printf("> Error compiling module!\n"); |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | wasm_byte_vec_delete(&binary); |
| 161 | |
| 162 | own wasm_shared_module_t *shared = wasm_module_share(module); |
| 163 | |
| 164 | wasm_module_delete(module); |
| 165 | wasm_store_delete(store); |
| 166 | |
| 167 | // Spawn threads. |
| 168 | pthread_t threads[N_THREADS]; |
| 169 | for (int i = 0; i < N_THREADS; i++) { |
| 170 | thread_args *args = malloc(sizeof(thread_args)); |
| 171 | args->engine = engine; |
| 172 | args->module = shared; |
| 173 | printf("Initializing thread %d...\n", i); |
| 174 | |
| 175 | // Guarantee at least 2MB of stack to allow running Cranelift in debug mode |
| 176 | // on CI. |
| 177 | pthread_attr_t attrs; |
| 178 | pthread_attr_init(&attrs); |
| 179 | pthread_attr_setstacksize(&attrs, 2 << 20); |
| 180 | pthread_create(&threads[i], &attrs, &run, args); |
| 181 | pthread_attr_destroy(&attrs); |
nothing calls this directly
no test coverage detected