20
20
#include "type.h"
21
21
#include "wasi_impl.h"
22
22
#include "wasi_threads.h"
23
+ #include "wasi_threads_abi.h"
23
24
#include "xlog.h"
24
25
25
26
#if !defined(TOYWASM_ENABLE_WASM_THREADS )
@@ -253,14 +254,10 @@ runner(void *vp)
253
254
}
254
255
255
256
static 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 )
259
260
{
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 );
264
261
struct instance * inst = NULL ;
265
262
struct thread_arg * arg = NULL ;
266
263
uint32_t tid ;
@@ -340,6 +337,24 @@ wasi_thread_spawn(struct exec_context *ctx, struct host_instance *hi,
340
337
instance_destroy (inst );
341
338
}
342
339
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 );
343
358
int32_t result ;
344
359
if (ret != 0 ) {
345
360
/* negative errno on error */
@@ -353,6 +368,38 @@ wasi_thread_spawn(struct exec_context *ctx, struct host_instance *hi,
353
368
return 0 ;
354
369
}
355
370
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
+
356
403
static int
357
404
wasi_thread_exit (struct exec_context * ctx , struct host_instance * hi ,
358
405
const struct functype * ft , const struct cell * params ,
@@ -364,7 +411,16 @@ wasi_thread_exit(struct exec_context *ctx, struct host_instance *hi,
364
411
}
365
412
366
413
const 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" ),
368
424
/*
369
425
* Note: thread_exit is not a part of the current wasi-threads.
370
426
* It's implemented here just for my experiments.
0 commit comments