darkfi/system/
mod.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2026 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
40/// Thread priority setting
41pub mod thread_priority;
42
43pub type ExecutorPtr = Arc<Executor<'static>>;
44
45/// Sleep for any number of seconds.
46pub async fn sleep(seconds: u64) {
47    Timer::after(Duration::from_secs(seconds)).await;
48}
49
50pub async fn sleep_forever() {
51    loop {
52        sleep(100000000).await
53    }
54}
55
56/// Sleep for any number of milliseconds.
57pub async fn msleep(millis: u64) {
58    Timer::after(Duration::from_millis(millis)).await;
59}
60
61/// Run a task until it has fully completed, irrespective of whether the parent task still exists.
62pub async fn run_until_completion<'a, R: Send + 'a, F: Future<Output = R> + Send + 'a>(
63    func: F,
64    executor: Arc<Executor<'a>>,
65) -> R {
66    let (sender, recv_queue) = smol::channel::bounded::<R>(1);
67    executor
68        .spawn(async move {
69            let result = func.await;
70            // We ignore this result: an error would mean the parent task has been cancelled,
71            // which is valid behavior.
72            let _ = sender.send(result).await;
73        })
74        .detach();
75    // This should never panic because it would mean the detached task has not completed.
76    recv_queue.recv().await.expect("Run until completion task failed")
77}