Coverage Report

Created: 2026-12-01 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.54.1/src/signal/registry.rs
Line
Count
Source
1
use crate::signal::unix::{OsExtraData, OsStorage};
2
use crate::sync::watch;
3
4
use std::ops;
5
use std::sync::atomic::{AtomicBool, Ordering};
6
use std::sync::OnceLock;
7
8
pub(crate) type EventId = usize;
9
10
/// State for a specific event, whether a notification is pending delivery,
11
/// and what listeners are registered.
12
#[derive(Debug)]
13
pub(crate) struct EventInfo {
14
    pending: AtomicBool,
15
    tx: watch::Sender<()>,
16
}
17
18
impl Default for EventInfo {
19
128
    fn default() -> Self {
20
128
        let (tx, _rx) = watch::channel(());
21
22
128
        Self {
23
128
            pending: AtomicBool::new(false),
24
128
            tx,
25
128
        }
26
128
    }
27
}
28
29
/// An interface for retrieving the `EventInfo` for a particular `eventId`.
30
pub(crate) trait Storage {
31
    /// Gets the `EventInfo` for `id` if it exists.
32
    fn event_info(&self, id: EventId) -> Option<&EventInfo>;
33
34
    /// Returns an iterator over the storage.
35
    fn iter(&self) -> impl Iterator<Item = &EventInfo>;
36
}
37
38
impl Storage for Vec<EventInfo> {
39
0
    fn event_info(&self, id: EventId) -> Option<&EventInfo> {
40
0
        self.get(id)
41
0
    }
42
43
0
    fn iter(&self) -> impl Iterator<Item = &EventInfo> {
44
0
        self.as_slice().iter()
45
0
    }
46
}
47
48
/// Manages and distributes event notifications to any registered listeners.
49
///
50
/// Generic over the underlying storage to allow for domain specific
51
/// optimizations (e.g. `eventIds` may or may not be contiguous).
52
#[derive(Debug)]
53
pub(crate) struct Registry<S> {
54
    storage: S,
55
}
56
57
impl<S> Registry<S> {
58
2
    fn new(storage: S) -> Self {
59
2
        Self { storage }
60
2
    }
61
}
62
63
impl<S: Storage> Registry<S> {
64
    /// Registers a new listener for `event_id`.
65
0
    fn register_listener(&self, event_id: EventId) -> watch::Receiver<()> {
66
0
        self.storage
67
0
            .event_info(event_id)
68
0
            .unwrap_or_else(|| panic!("invalid event_id: {event_id}"))
69
            .tx
70
0
            .subscribe()
71
0
    }
72
73
    /// Marks `event_id` as having been delivered, without broadcasting it to
74
    /// any listeners.
75
0
    fn record_event(&self, event_id: EventId) {
76
0
        if let Some(event_info) = self.storage.event_info(event_id) {
77
0
            event_info.pending.store(true, Ordering::SeqCst);
78
0
        }
79
0
    }
80
81
    /// Broadcasts all previously recorded events to their respective listeners.
82
    ///
83
    /// Returns `true` if an event was delivered to at least one listener.
84
0
    fn broadcast(&self) -> bool {
85
0
        self.storage
86
0
            .iter()
87
            // Any signal of this kind arrived since we checked last?
88
0
            .filter(|event_info| event_info.pending.swap(false, Ordering::SeqCst))
89
            // Ignore errors if there are no listeners
90
0
            .map(|event_info| event_info.tx.send(()).is_ok())
91
0
            .fold(false, |acc, x| acc | x)
92
0
    }
93
}
94
95
pub(crate) struct Globals {
96
    extra: OsExtraData,
97
    registry: Registry<OsStorage>,
98
}
99
100
impl ops::Deref for Globals {
101
    type Target = OsExtraData;
102
103
18.8k
    fn deref(&self) -> &Self::Target {
104
18.8k
        &self.extra
105
18.8k
    }
106
}
107
108
impl Globals {
109
    /// Registers a new listener for `event_id`.
110
0
    pub(crate) fn register_listener(&self, event_id: EventId) -> watch::Receiver<()> {
111
0
        self.registry.register_listener(event_id)
112
0
    }
113
114
    /// Marks `event_id` as having been delivered, without broadcasting it to
115
    /// any listeners.
116
0
    pub(crate) fn record_event(&self, event_id: EventId) {
117
0
        self.registry.record_event(event_id);
118
0
    }
119
120
    /// Broadcasts all previously recorded events to their respective listeners.
121
    ///
122
    /// Returns `true` if an event was delivered to at least one listener.
123
0
    pub(crate) fn broadcast(&self) -> bool {
124
0
        self.registry.broadcast()
125
0
    }
126
127
    #[cfg(unix)]
128
0
    pub(crate) fn storage(&self) -> &OsStorage {
129
0
        &self.registry.storage
130
0
    }
131
}
132
133
2
fn globals_init() -> Globals
134
2
where
135
2
    OsExtraData: 'static + Send + Sync + Default,
136
2
    OsStorage: 'static + Send + Sync + Default,
137
{
138
2
    Globals {
139
2
        extra: OsExtraData::default(),
140
2
        registry: Registry::new(OsStorage::default()),
141
2
    }
142
2
}
143
144
18.8k
pub(crate) fn globals() -> &'static Globals
145
18.8k
where
146
18.8k
    OsExtraData: 'static + Send + Sync + Default,
147
18.8k
    OsStorage: 'static + Send + Sync + Default,
148
{
149
    static GLOBALS: OnceLock<Globals> = OnceLock::new();
150
151
18.8k
    GLOBALS.get_or_init(globals_init)
152
18.8k
}
153
154
#[cfg(all(test, not(loom)))]
155
mod tests {
156
    use super::*;
157
    use crate::runtime::{self, Runtime};
158
    use crate::sync::{oneshot, watch};
159
160
    use futures::future;
161
162
    #[test]
163
    fn smoke() {
164
        let rt = rt();
165
        rt.block_on(async move {
166
            let registry = Registry::new(vec![
167
                EventInfo::default(),
168
                EventInfo::default(),
169
                EventInfo::default(),
170
            ]);
171
172
            let first = registry.register_listener(0);
173
            let second = registry.register_listener(1);
174
            let third = registry.register_listener(2);
175
176
            let (fire, wait) = oneshot::channel();
177
178
            crate::spawn(async {
179
                wait.await.expect("wait failed");
180
181
                // Record some events which should get coalesced
182
                registry.record_event(0);
183
                registry.record_event(0);
184
                registry.record_event(1);
185
                registry.record_event(1);
186
                registry.broadcast();
187
188
                // Yield so the previous broadcast can get received
189
                //
190
                // This yields many times since the block_on task is only polled every 61
191
                // ticks.
192
                for _ in 0..100 {
193
                    crate::task::yield_now().await;
194
                }
195
196
                // Send subsequent signal
197
                registry.record_event(0);
198
                registry.broadcast();
199
200
                drop(registry);
201
            });
202
203
            let _ = fire.send(());
204
            let all = future::join3(collect(first), collect(second), collect(third));
205
206
            let (first_results, second_results, third_results) = all.await;
207
            assert_eq!(2, first_results.len());
208
            assert_eq!(1, second_results.len());
209
            assert_eq!(0, third_results.len());
210
        });
211
    }
212
213
    #[test]
214
    #[should_panic = "invalid event_id: 1"]
215
    fn register_panics_on_invalid_input() {
216
        let registry = Registry::new(vec![EventInfo::default()]);
217
218
        registry.register_listener(1);
219
    }
220
221
    #[test]
222
    fn record_invalid_event_does_nothing() {
223
        let registry = Registry::new(vec![EventInfo::default()]);
224
        registry.record_event(1302);
225
    }
226
227
    #[test]
228
    fn broadcast_returns_if_at_least_one_event_fired() {
229
        let registry = Registry::new(vec![EventInfo::default(), EventInfo::default()]);
230
231
        registry.record_event(0);
232
        assert!(!registry.broadcast());
233
234
        let first = registry.register_listener(0);
235
        let second = registry.register_listener(1);
236
237
        registry.record_event(0);
238
        assert!(registry.broadcast());
239
240
        drop(first);
241
        registry.record_event(0);
242
        assert!(!registry.broadcast());
243
244
        drop(second);
245
    }
246
247
    fn rt() -> Runtime {
248
        runtime::Builder::new_current_thread()
249
            .enable_time()
250
            .build()
251
            .unwrap()
252
    }
253
254
    async fn collect(mut rx: watch::Receiver<()>) -> Vec<()> {
255
        let mut ret = vec![];
256
257
        while let Ok(v) = rx.changed().await {
258
            ret.push(v);
259
        }
260
261
        ret
262
    }
263
}