wait.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use crate::util::panic::UnwindSafe;
  2. use core::{fmt, task::Waker};
  3. mod wait_cell;
  4. pub(crate) use self::wait_cell::WaitCell;
  5. feature! {
  6. #![feature = "alloc"]
  7. pub(crate) mod wait_queue;
  8. pub(crate) use self::wait_queue::WaitQueue;
  9. }
  10. #[cfg(feature = "std")]
  11. use crate::loom::thread;
  12. #[derive(Debug, Eq, PartialEq)]
  13. pub(crate) enum WaitResult {
  14. Wait,
  15. TxClosed,
  16. Notified,
  17. }
  18. #[derive(Debug)]
  19. pub(crate) struct NotifyOnDrop<T: Notify>(Option<T>);
  20. pub(crate) trait Notify: UnwindSafe + fmt::Debug {
  21. fn notify(self);
  22. }
  23. #[cfg(feature = "std")]
  24. impl Notify for thread::Thread {
  25. fn notify(self) {
  26. test_println!("NOTIFYING {:?} (from {:?})", self, thread::current());
  27. self.unpark();
  28. }
  29. }
  30. impl Notify for Waker {
  31. fn notify(self) {
  32. test_println!("WAKING TASK {:?} (from {:?})", self, thread::current());
  33. self.wake();
  34. }
  35. }
  36. impl<T: Notify> NotifyOnDrop<T> {
  37. pub(crate) fn new(notify: T) -> Self {
  38. Self(Some(notify))
  39. }
  40. }
  41. impl<T: Notify> Notify for NotifyOnDrop<T> {
  42. fn notify(self) {
  43. drop(self)
  44. }
  45. }
  46. impl<T: Notify> Drop for NotifyOnDrop<T> {
  47. fn drop(&mut self) {
  48. if let Some(notify) = self.0.take() {
  49. notify.notify();
  50. }
  51. }
  52. }