2020#include "type.h"
2121#include "wasi_impl.h"
2222#include "wasi_threads.h"
23+ #include "wasi_threads_abi.h"
2324#include "xlog.h"
2425
2526#if !defined(TOYWASM_ENABLE_WASM_THREADS )
@@ -253,14 +254,10 @@ runner(void *vp)
253254}
254255
255256static int
256- wasi_thread_spawn (struct exec_context * ctx , struct host_instance * hi ,
257- const struct functype * ft , const struct cell * params ,
258- struct cell * results )
257+ wasi_thread_spawn_common (struct exec_context * ctx ,
258+ struct wasi_threads_instance * wasi , uint32_t user_arg ,
259+ uint32_t * tidp )
259260{
260- WASI_TRACE ;
261- struct wasi_threads_instance * wasi = (void * )hi ;
262- HOST_FUNC_CONVERT_PARAMS (ft , params );
263- uint32_t user_arg = HOST_FUNC_PARAM (ft , params , 0 , i32 );
264261 struct instance * inst = NULL ;
265262 struct thread_arg * arg = NULL ;
266263 uint32_t tid ;
@@ -340,6 +337,24 @@ wasi_thread_spawn(struct exec_context *ctx, struct host_instance *hi,
340337 instance_destroy (inst );
341338 }
342339 free (arg );
340+ if (ret == 0 ) {
341+ * tidp = tid ;
342+ }
343+ return ret ;
344+ }
345+
346+ static int
347+ wasi_thread_spawn_old (struct exec_context * ctx , struct host_instance * hi ,
348+ const struct functype * ft , const struct cell * params ,
349+ struct cell * results )
350+ {
351+ WASI_TRACE ;
352+ struct wasi_threads_instance * wasi = (void * )hi ;
353+ HOST_FUNC_CONVERT_PARAMS (ft , params );
354+ uint32_t user_arg = HOST_FUNC_PARAM (ft , params , 0 , i32 );
355+ uint32_t tid ;
356+
357+ int ret = wasi_thread_spawn_common (ctx , wasi , user_arg , & tid );
343358 int32_t result ;
344359 if (ret != 0 ) {
345360 /* negative errno on error */
@@ -353,6 +368,38 @@ wasi_thread_spawn(struct exec_context *ctx, struct host_instance *hi,
353368 return 0 ;
354369}
355370
371+ static int
372+ wasi_thread_spawn (struct exec_context * ctx , struct host_instance * hi ,
373+ const struct functype * ft , const struct cell * params ,
374+ struct cell * results )
375+ {
376+ WASI_TRACE ;
377+ struct wasi_threads_instance * wasi = (void * )hi ;
378+ HOST_FUNC_CONVERT_PARAMS (ft , params );
379+ uint32_t user_arg = HOST_FUNC_PARAM (ft , params , 0 , i32 );
380+ uint32_t retp = HOST_FUNC_PARAM (ft , params , 1 , i32 );
381+ uint32_t tid ;
382+
383+ int ret = wasi_thread_spawn_common (ctx , wasi , user_arg , & tid );
384+ struct wasi_thread_spawn_result r ;
385+ memset (& r , 0 , sizeof (r ));
386+ if (ret != 0 ) {
387+ xlog_trace ("%s failed with %d" , __func__ , ret );
388+ r .is_error = 1 ;
389+ r .u .error = wasi_convert_errno (ret ); /* XXX */
390+ } else {
391+ xlog_trace ("%s succeeded tid %u" , __func__ , tid );
392+ r .is_error = 0 ;
393+ r .u .tid = tid ;
394+ }
395+ ret = wasi_copyout (ctx , & r , retp , sizeof (r ));
396+ if (ret != 0 ) {
397+ /* XXX what to do? trap? */
398+ xlog_error ("%s: copyout failed with %d" , __func__ , ret );
399+ }
400+ return 0 ;
401+ }
402+
356403static int
357404wasi_thread_exit (struct exec_context * ctx , struct host_instance * hi ,
358405 const struct functype * ft , const struct cell * params ,
@@ -364,7 +411,16 @@ wasi_thread_exit(struct exec_context *ctx, struct host_instance *hi,
364411}
365412
366413const struct host_func wasi_threads_funcs [] = {
367- WASI_HOST_FUNC (thread_spawn , "(i)i" ),
414+ /*
415+ * The thread-spawn API before and after wit definition:
416+ * https://github.com/WebAssembly/wasi-threads/pull/26
417+ * https://github.com/WebAssembly/wasi-libc/pull/385
418+ *
419+ * We will keep the old one for a while to ease
420+ * experiment/migration/testing.
421+ */
422+ WASI_HOST_FUNC2 ("thread-spawn" , wasi_thread_spawn , "(ii)" ),
423+ WASI_HOST_FUNC2 ("thread_spawn" , wasi_thread_spawn_old , "(i)i" ),
368424 /*
369425 * Note: thread_exit is not a part of the current wasi-threads.
370426 * It's implemented here just for my experiments.
0 commit comments