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
//! The paired killswitch is separated into a [`KillTrig`] (used to trigger a
//! shutdown notification) and a [`KillWait`] (used to wait for the killswitch
//! to be triggered).
//!
//! Only the `KillWait` can be cloned, thus there's a guarantee that only one
//! kill trigger exists.
//!
//! ```
//! use std::error::Error;
//! use tokio::time::{sleep, Duration};
//! use killswitch::pair::{self, KillWait};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! let (kt, kw) = pair::create();
//!
//! tokio::spawn(killable(String::from("test1"), kw.clone()));
//! tokio::spawn(killable(String::from("test2"), kw.clone()));
//!
//! sleep(Duration::from_secs(1)).await;
//!
//! println!("Triggering kill switch");
//! kt.trigger();
//!
//! tokio::spawn(killable(String::from("test3"), kw.clone()));
//! tokio::spawn(killable(String::from("test4"), kw));
//!
//! // Wait for all waiters to drop
//! kt.await;
//!
//! Ok(())
//! }
//!
//! async fn killable(s: String, kw: KillWait) {
//! println!("killable({}) entered", s);
//! kw.wait().await;
//! println!("killable({}) leaving", s);
//! }
//! ```
//!
//! The semantics of `.await`:ing the `KillTrig` has a notable semantic
//! difference compared to calling
//! [`KillSwitch::finalize()`](super::KillSwitch::finalize()): The
//! `KillSwitch` finalization future waits for all _active_ waiters to
//! terminate, while awaiting `KillTrig` will return once all associated
//! `KillWait` objects have been dropped.
use ;
use Mutex;
pub use KillTrig;
pub use KillWait;
/// Shared state buffer that is hidden behind a Mutex.
/// Buffer shared among all KillSwitch and Shutdown objects.
/// Create a connected [`KillTrig`] and a [`KillWait`] pair.
// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :