1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// TODO: Switch to interior mutability (e.g. use Mutexes or thread-local
// RefCells) and remove this, since even in single-threaded mode `static mut`
// references can be a hazard due to recursive access.
use crateBoxFuture;
use ;
use Box;
use Future;
use ;
use Vec;
/// Any newly-deferred work queued by calls to the `spawn` function while
/// polling the current task.
static mut SPAWNED: = Vecnew;
/// Spawn the provided `future` to get executed concurrently with the
/// currently-running async computation.
///
/// This API is somewhat similar to `tokio::task::spawn` for example but has a
/// number of limitations to be aware of. If possible it's recommended to avoid
/// this, but it can be convenient if these limitations do not apply to you:
///
/// * Spawned tasks do not work when the version of the `wit-bindgen` crate
/// managing the export bindings is different from the version of this crate.
/// To work correctly the `spawn` function and export executor must be at
/// exactly the same version. Given the major-version-breaking nature of
/// `wit-bindgen` this is not always easy to rely on. This is tracked in
/// [#1305].
///
/// * Spawned tasks do not outlive the scope of the async computation they are
/// spawned within. For example with an async export function spawned tasks
/// will be polled within the context of that component-model async task. For
/// computations executing within a [`block_on`] call, however, the spawned
/// tasks will be executed within that scope. This notably means that for
/// [`block_on`] spawned tasks will prevent the [`block_on`] function from
/// returning, even if a value is available to return.
///
/// * There is no handle returned to the spawned task meaning that it cannot be
/// cancelled or monitored.
///
/// * The task spawned here is executed *concurrently*, not in *parallel*. This
/// means that while one future is being polled no other future can be polled
/// at the same time. This is similar to a single-thread executor in Tokio.
///
/// With these restrictions in mind this can be used to express
/// execution-after-returning in the component model. For example once an
/// exported async function has produced a value this can be used to continue to
/// execute some more code before the component model async task exits.
///
/// [`block_on`]: crate::block_on
/// [#1305]: https://github.com/bytecodealliance/wit-bindgen/issues/1305