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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Creating, configuring, and stopping a runtime.
//!
//! Use [`Runtime`] to run async tasks from synchronous code and control
//! when workers stop. [`Runtime::new`] starts the default configuration.
//! Each async task stays on one worker thread until it finishes or is cancelled.
//! Arty can pass values between workers through
//! [`ThreadAware`](crate::core::ThreadAware), which lets values update their state
//! for the receiving worker.
//!
//! A blocking pool is a set of threads for blocking functions.
//! [`Runtime::builder`] lets you choose workers, blocking pools, and clocks
//! before they start.
//!
//! Each task receives [`Context`](crate::task::Context) with its worker's
//! scheduler and clock. [`Runtime::scheduler`] lets the runtime place new
//! tasks, while [`Context::scheduler`](crate::task::Context::scheduler)
//! keeps children on their parent's worker.
//! [`Context::control`](crate::task::Context::control) lists the workers and
//! can request shutdown without calling `Runtime::stop`.
//!
//! Keep the `Runtime` value alive until required tasks finish.
//! Shutdown cancels pending tasks and waits for blocking functions that have
//! already started.
//! Keeping a scheduler does not keep the runtime running.
//!
//! # Configuration
//!
//! [`RuntimeBuilder`] collects configuration before any worker starts.
//! [`RuntimeBuilder::build`] rejects invalid configuration and starts the runtime.
//! The policy types describe each choice and its use of threads and memory.
//!
//! ## Defaults
//!
//! [`Runtime::new`] and an unchanged [`RuntimeBuilder`] use these defaults:
//!
//! - [`WorkersPolicy::default`] starts one async worker.
//! - Each async worker uses a 2 MiB stack. A larger `RUST_MIN_STACK` value
//! overrides this default.
//! - [`BlockingPoolPolicy::default`] creates one shared blocking pool. Its
//! default thread limits can change. Use [`BlockingPoolPolicy::max`] to set
//! a maximum.
//! - The clock follows real time.
//! - [`MonitorPolicy::default`] enables monitoring in the runtime configuration.
//! This choice does not start a monitor or add task measurements.
//! - [`PanicPolicy::default`] stops the runtime after Arty catches a panic
//! while it creates or polls a task, or runs a blocking function.
//!
//! The [`test`](crate::test) attribute disables monitoring by default.
//! See [`main`](crate::main) and [`test`](crate::test) for their other defaults.
//!
//! ## Policy types
//!
//! - [`WorkersPolicy`] selects how many async workers run and which processors they use.
//! - [`BlockingPoolPolicy`] selects a shared or per-worker blocking pool and
//! sets its thread limit.
//! - [`PanicPolicy`] selects whether the runtime stops after Arty catches a panic.
//! - [`MonitorPolicy`] selects the monitoring configuration.
//!
//! [`RuntimeBuilder::stack_size`] sets the async worker stack size.
//! [`RuntimeBuilder::clock`] selects the runtime clock. See each linked API for
//! configuration requirements, resource limits, and examples.
//!
//! This example uses at most two async workers and one shared blocking pool
//! with up to four threads:
//!
//! ```
//! use arty::runtime::{BlockingPoolPolicy, Runtime, WorkersPolicy};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let runtime = Runtime::builder()
//! .workers(WorkersPolicy::at_most(2))
//! .blocking_pool(BlockingPoolPolicy::shared().max(4))
//! .build()?;
//! assert_eq!(runtime.scheduler().block_on(async |_| 42)?, 42);
//! runtime.stop()?;
//! Ok(())
//! }
//! ```
//!
//! # Entry-point attributes
//!
//! With `macros` enabled, [`arty::main`](crate::main) and
//! [`arty::test`](crate::test) create and stop the runtime. Their arguments set
//! the worker count, monitoring, and response to task panics. See [`main`](crate::main)
//! and [`test`](crate::test) for the supported syntax and defaults.
//!
//! Use [`RuntimeBuilder`] for custom clocks, blocking pools, stack sizes,
//! or [`WorkersPolicy::at_most`].
//! Use the builder to receive configuration errors and choose when to stop the runtime.
//!
//! # Runtime services
//!
//! The runtime provides three service groups:
//!
//! - [`task`](crate::task) explains worker selection, blocking functions, and
//! task results.
//! - [`time`](crate::time) explains clocks, timers, and controlled test time.
//! - [`io`](crate::io) explains how to connect I/O libraries.
//!
//! # Shutdown
//!
//! [`Runtime`] owns the workers.
//! Schedulers and [`Context`](crate::task::Context) values do not keep the runtime running.
//! [`RuntimeControl::request_stop`]
//! requests shutdown without waiting. [`Runtime::stop`] takes ownership of the runtime,
//! waits for every worker, and reports shutdown failures. Dropping the `Runtime` value
//! also requests shutdown and waits, but it cannot return an error.
//!
//! Shutdown:
//!
//! - Rejects new tasks.
//! - Cancels pending async tasks.
//! - Discards queued blocking functions.
//!
//! Blocking functions that already started can finish.
//! Their completion can delay shutdown. [`JoinError`](crate::task::JoinError)
//! reports task cancellation. If a runtime failure started shutdown, the error
//! also gives access to that failure.
//!
//! Keep the `Runtime` value on a thread that is not an Arty worker.
//! Stopping or dropping it in an Arty task or on a blocking worker panics.
//! Worker code must use
//! [`RuntimeControl::request_stop`].
pub
pub
pub
pub use RuntimeBuilder;
pub use ;
pub use RuntimeControl;
pub use Error;
pub use Runtime;
pub use ;
pub use Workers;
/// Implementation details for the runtime entry-point macros.