darkfi/system/
mod.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use std::{sync::Arc, time::Duration};
20
21use smol::{future::Future, Executor, Timer};
22
23/// Condition variable which allows a task to block until woken up
24pub mod condvar;
25pub use condvar::CondVar;
26
27/// Implementation of async background task spawning which are stoppable
28/// using channel signalling.
29pub mod stoppable_task;
30pub use stoppable_task::{StoppableTask, StoppableTaskPtr};
31
32/// Simple broadcast (publish-subscribe) class
33pub mod publisher;
34pub use publisher::{Publisher, PublisherPtr, Subscription};
35
36/// Async timeout implementations
37pub mod timeout;
38pub use timeout::io_timeout;
39
40pub type ExecutorPtr = Arc<Executor<'static>>;
41
42/// Sleep for any number of seconds.
43pub async fn sleep(seconds: u64) {
44    Timer::after(Duration::from_secs(seconds)).await;
45}
46
47pub async fn sleep_forever() {
48    loop {
49        sleep(100000000).await
50    }
51}
52
53/// Sleep for any number of milliseconds.
54pub async fn msleep(millis: u64) {
55    Timer::after(Duration::from_millis(millis)).await;
56}
57
58/// Run a task until it has fully completed, irrespective of whether the parent task still exists.
59pub async fn run_until_completion<'a, R: Send + 'a, F: Future<Output = R> + Send + 'a>(
60    func: F,
61    executor: Arc<Executor<'a>>,
62) -> R {
63    let (sender, recv_queue) = smol::channel::bounded::<R>(1);
64    executor
65        .spawn(async move {
66            let result = func.await;
67            // We ignore this result: an error would mean the parent task has been cancelled,
68            // which is valid behavior.
69            let _ = sender.send(result).await;
70        })
71        .detach();
72    // This should never panic because it would mean the detached task has not completed.
73    recv_queue.recv().await.expect("Run until completion task failed")
74}