mod.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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. use std::{sync::Arc, time::Duration};
  19. use smol::{future::Future, Executor, Timer};
  20. /// Condition variable which allows a task to block until woken up
  21. pub mod condvar;
  22. pub use condvar::CondVar;
  23. /// Convenient late initialization of `Weak<Foo>`
  24. pub mod lazy_weak;
  25. pub use lazy_weak::LazyWeak;
  26. /// Implementation of async background task spawning which are stoppable
  27. /// using channel signalling.
  28. pub mod stoppable_task;
  29. pub use stoppable_task::{StoppableTask, StoppableTaskPtr};
  30. /// Simple broadcast (publish-subscribe) class
  31. pub mod subscriber;
  32. pub use subscriber::{Subscriber, SubscriberPtr, Subscription};
  33. /// Async timeout implementations
  34. pub mod timeout;
  35. pub use timeout::io_timeout;
  36. pub type ExecutorPtr = Arc<Executor<'static>>;
  37. /// Sleep for any number of seconds.
  38. pub async fn sleep(seconds: u64) {
  39. Timer::after(Duration::from_secs(seconds)).await;
  40. }
  41. pub async fn sleep_forever() {
  42. loop {
  43. sleep(100000000).await
  44. }
  45. }
  46. /// Sleep for any number of milliseconds.
  47. pub async fn msleep(millis: u64) {
  48. Timer::after(Duration::from_millis(millis)).await;
  49. }
  50. /// Run a task until it has fully completed, irrespective of whether the parent task still exists.
  51. pub async fn run_until_completion<'a, R: Send + 'a, F: Future<Output = R> + Send + 'a>(
  52. func: F,
  53. executor: Arc<Executor<'a>>,
  54. ) -> R {
  55. let (sender, recv_queue) = smol::channel::bounded::<R>(1);
  56. executor
  57. .spawn(async move {
  58. let result = func.await;
  59. // We ignore this result: an error would mean the parent task has been cancelled,
  60. // which is valid behavior.
  61. let _ = sender.send(result).await;
  62. })
  63. .detach();
  64. // This should never panic because it would mean the detached task has not completed.
  65. recv_queue.recv().await.expect("Run until completion task failed")
  66. }