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
//! Test support: a per-root ledger of live tasks and pool entries.
//!
//! Enabled by the `ledger` cargo feature. Every token made by
//! [`Exec::new`](crate::Exec::new) is a *root*; tokens produced from it by
//! [`fork`](crate::Exec::fork) or [`reborrow`](crate::Exec::reborrow) report to
//! the same root. A task that hands its slots to children (by entering the
//! token again) stops counting while it waits, so the number of live tasks per
//! root is the number of slots actually in use. Consumers assert
//! `max_live(root) <= budget` in their own test suites.
//!
//! # Examples
//!
//! ```
//! use threadteam::{ledger, Exec, Pool};
//!
//! let pool = Pool::builder().threads(2).build()?;
//! let exec = Exec::new(&pool, 2)?;
//! let root = exec.ledger_root().expect("a token from `new` is a root");
//! exec.fork(4, |_, _| {});
//! assert!(ledger::max_live(root) <= 2);
//! # Ok::<(), threadteam::Error>(())
//! ```
use HashMap;
use ;
use ;
pub
pub
/// Largest number of tasks of `root` that were live at the same moment.
///
/// Returns 0 for an unknown root.
///
/// # Examples
///
/// ```
/// use threadteam::{ledger, Exec, Pool};
///
/// let pool = Pool::builder().threads(1).build()?;
/// let exec = Exec::new(&pool, 1)?;
/// let root = exec.ledger_root().unwrap();
/// exec.fork(3, |_, _| {});
/// assert_eq!(ledger::max_live(root), 1);
/// # Ok::<(), threadteam::Error>(())
/// ```
/// Number of tasks of `root` live right now.
///
/// # Examples
///
/// ```
/// use threadteam::{ledger, Exec, Pool};
///
/// let pool = Pool::builder().threads(1).build()?;
/// let exec = Exec::new(&pool, 1)?;
/// let root = exec.ledger_root().unwrap();
/// exec.fork(2, |_, _| {});
/// assert_eq!(ledger::live(root), 0);
/// # Ok::<(), threadteam::Error>(())
/// ```
/// Number of times an entry of `root` had to leave its calling thread to
/// install work into the pool (a plain thread or a worker of another pool).
///
/// Entries made from a worker of the target pool never count: they run on
/// the caller's worker without installing. Inline entries never count either.
///
/// # Examples
///
/// ```
/// use threadteam::{ledger, Exec, Pool};
///
/// let pool = Pool::builder().threads(2).build()?;
/// let exec = Exec::new(&pool, 2)?;
/// let root = exec.ledger_root().unwrap();
/// exec.fork(1, |_, _| {});
/// assert_eq!(ledger::installs(root), 0, "one task runs inline");
/// # Ok::<(), threadteam::Error>(())
/// ```
/// Whether the process-global Rayon pool is still unstarted.
///
/// Answers by starting it: the call installs a one-thread global pool, which
/// succeeds only when nothing in the process has touched the global registry
/// before. Call it once, at the end of a test binary that holds a single test,
/// so that no other test in the process can start the global pool first.
/// Without the `parallel` feature nothing can start a pool and it returns
/// `true`.
///
/// # Examples
///
/// ```no_run
/// use threadteam::{ledger, Exec};
///
/// Exec::serial().fork(4, |_, _| {});
/// assert!(ledger::global_pool_unstarted());
/// ```