1;;! component_model_async = true 2;;! component_model_threading = true 3 4;; Tests for basic functioning of all threading builtins with the implicit thread + one explicit thread 5;; Switches between threads using all of the different threading intrinsics. 6 7(component 8 ;; Defines the table for the thread start function 9 (core module $libc 10 (table (export "__indirect_function_table") 1 funcref)) 11 ;; Defines the thread start function and a function that calls thread.new-indirect 12 (core module $m 13 ;; Import the threading builtins and the table from libc 14 (import "" "thread.new-indirect" (func $thread-new-indirect (param i32 i32) (result i32))) 15 (import "" "thread.suspend" (func $thread-suspend (result i32))) 16 (import "" "thread.yield-to-suspended" (func $thread-yield-to-suspended (param i32) (result i32))) 17 (import "" "thread.suspend-to-suspended" (func $thread-suspend-to-suspended (param i32) (result i32))) 18 (import "" "thread.yield" (func $thread-yield (result i32))) 19 (import "" "thread.index" (func $thread-index (result i32))) 20 (import "" "thread.unsuspend" (func $thread-unsuspend (param i32))) 21 (import "libc" "__indirect_function_table" (table $indirect-function-table 1 funcref)) 22 23 ;; A global that we will set from the spawned thread 24 (global $g (mut i32) (i32.const 0)) 25 (global $main-thread-index (mut i32) (i32.const 0)) 26 27 ;; The thread entry point, which sets the global to incrementing values starting from the context value 28 (func $thread-start (param i32) 29 ;; Set the global to the context value 30 (global.set $g (local.get 0)) 31 ;; The main thread switched to us, so is no longer scheduled, so we explicitly schedule it 32 (call $thread-unsuspend (global.get $main-thread-index)) 33 ;; Yield back to the main thread (since that is the only other one) 34 (drop (call $thread-yield) 35 ;; Increment the global 36 (global.set $g (i32.add (global.get $g) (i32.const 1))) 37 ;; The main thread will have explicitly requested suspension, so yield to it directly 38 (drop (call $thread-yield-to-suspended (global.get $main-thread-index))) 39 ;; Increment the global again 40 (global.set $g (i32.add (global.get $g) (i32.const 1))) 41 ;; Reschedule the main thread so that it runs after we exit 42 (call $thread-unsuspend (global.get $main-thread-index)))) 43 (export "thread-start" (func $thread-start)) 44 45 ;; Initialize the function table with our thread-start function; this will be 46 ;; used by thread.new-indirect 47 (elem (table $indirect-function-table) (i32.const 0) func $thread-start) 48 49 ;; The main entry point, which spawns a new thread to run `thread-start`, passing 42 50 ;; as the context value, and then yields to it 51 (func (export "run") (result i32) 52 ;; Store the main thread's index for the spawned thread to yield to 53 (global.set $main-thread-index (call $thread-index)) 54 ;; Create a new thread, which starts suspended, and switch to it 55 (drop 56 (call $thread-suspend-to-suspended 57 (call $thread-new-indirect (i32.const 0) (i32.const 42)))) 58 ;; After the thread yields back to us, check that the global was set to 42 59 (if (i32.ne (global.get $g) (i32.const 42)) (then unreachable)) 60 ;; Suspend ourselves, which will cause the spawned thread to run 61 (drop (call $thread-suspend)) 62 ;; The spawned thread will resume us after incrementing the global, so check that it is now 43 63 (if (i32.ne (global.get $g) (i32.const 43)) (then unreachable)) 64 ;; Suspend again, which will cause the spawned thread to run again 65 (drop (call $thread-suspend)) 66 ;; The spawned thread will reschedule us before it exits, so when we resume here the global should be 44 67 (if (i32.ne (global.get $g) (i32.const 44)) (then unreachable)) 68 ;; Return success 69 (i32.const 42))) 70 71 ;; Instantiate the libc module to get the table 72 (core instance $libc (instantiate $libc)) 73 ;; Get access to `thread.new-indirect` that uses the table from libc 74 (core type $start-func-ty (func (param i32))) 75 (alias core export $libc "__indirect_function_table" (core table $indirect-function-table)) 76 77 (core func $thread-new-indirect 78 (canon thread.new-indirect $start-func-ty (table $indirect-function-table))) 79 (core func $thread-yield (canon thread.yield)) 80 (core func $thread-index (canon thread.index)) 81 (core func $thread-yield-to-suspended (canon thread.yield-to-suspended)) 82 (core func $thread-unsuspend (canon thread.unsuspend)) 83 (core func $thread-suspend-to-suspended (canon thread.suspend-to-suspended)) 84 (core func $thread-suspend (canon thread.suspend)) 85 86 ;; Instantiate the main module 87 (core instance $i ( 88 instantiate $m 89 (with "" (instance 90 (export "thread.new-indirect" (func $thread-new-indirect)) 91 (export "thread.index" (func $thread-index)) 92 (export "thread.yield-to-suspended" (func $thread-yield-to-suspended)) 93 (export "thread.yield" (func $thread-yield)) 94 (export "thread.suspend-to-suspended" (func $thread-suspend-to-suspended)) 95 (export "thread.suspend" (func $thread-suspend)) 96 (export "thread.unsuspend" (func $thread-unsuspend)))) 97 (with "libc" (instance $libc)))) 98 99 ;; Export the main entry point 100 (func (export "run") async (result u32) (canon lift (core func $i "run")))) 101 102(assert_return (invoke "run") (u32.const 42)) 103 104;; Test that `thread.index` is exempt from may-leave checks 105(component 106 (core func $thread.index (canon thread.index)) 107 108 (core module $DM 109 (import "" "thread.index" (func $thread.index (result i32))) 110 111 (func (export "run")) 112 (func (export "post-return") call $thread.index drop) 113 ) 114 (core instance $dm (instantiate $DM (with "" (instance 115 (export "thread.index" (func $thread.index)) 116 )))) 117 (func (export "run") 118 (canon lift (core func $dm "run") (post-return (func $dm "post-return")))) 119) 120 121(assert_return (invoke "run")) 122