MCPcopy Create free account
hub / github.com/WebAssembly/wasm-c-api / main

Function main

example/threads.c:99–155  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

97}
98
99int main(int argc, const char *argv[]) {
100 // Initialize.
101 wasm_engine_t* engine = wasm_engine_new();
102
103 // Load binary.
104 FILE* file = fopen("threads.wasm", "rb");
105 if (!file) {
106 printf("> Error loading module!\n");
107 return 1;
108 }
109 fseek(file, 0L, SEEK_END);
110 size_t file_size = ftell(file);
111 fseek(file, 0L, SEEK_SET);
112 wasm_byte_vec_t binary;
113 wasm_byte_vec_new_uninitialized(&binary, file_size);
114 if (fread(binary.data, file_size, 1, file) != 1) {
115 printf("> Error loading module!\n");
116 return 1;
117 }
118 fclose(file);
119
120 // Compile and share.
121 own wasm_store_t* store = wasm_store_new(engine);
122 own wasm_module_t* module = wasm_module_new(store, &binary);
123 if (!module) {
124 printf("> Error compiling module!\n");
125 return 1;
126 }
127
128 wasm_byte_vec_delete(&binary);
129
130 own wasm_shared_module_t* shared = wasm_module_share(module);
131
132 wasm_module_delete(module);
133 wasm_store_delete(store);
134
135 // Spawn threads.
136 pthread_t threads[N_THREADS];
137 for (int i = 0; i < N_THREADS; i++) {
138 thread_args* args = malloc(sizeof(thread_args));
139 args->id = i;
140 args->engine = engine;
141 args->module = shared;
142 printf("Initializing thread %d...\n", i);
143 pthread_create(&threads[i], NULL, &run, args);
144 }
145
146 for (int i = 0; i < N_THREADS; i++) {
147 printf("Waiting for thread: %d\n", i);
148 pthread_join(threads[i], NULL);
149 }
150
151 wasm_shared_module_delete(shared);
152 wasm_engine_delete(engine);
153
154 return 0;
155}

Callers

nothing calls this directly

Calls 4

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_newFunction · 0.85
wasm_module_shareFunction · 0.85

Tested by

no test coverage detected