lib.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #![no_std]
  2. /// Wait for a condition to become true.
  3. ///
  4. /// This macro will wait for a condition to become true.
  5. ///
  6. /// ## Parameters
  7. ///
  8. /// - `$wq`: The wait queue to wait on.
  9. /// - `$condition`: The condition to wait for. (you can pass a function or a boolean expression)
  10. /// - `$cmd`: The command to execute while waiting.
  11. #[macro_export]
  12. macro_rules! wq_wait_event_interruptible {
  13. ($wq:expr, $condition: expr, $cmd: expr) => {{
  14. let mut retval = Ok(());
  15. if !$condition {
  16. retval = wait_queue_macros::_wq_wait_event_interruptible!($wq, $condition, $cmd);
  17. }
  18. retval
  19. }};
  20. }
  21. #[macro_export]
  22. #[allow(clippy::crate_in_macro_def)]
  23. macro_rules! _wq_wait_event_interruptible {
  24. ($wq:expr, $condition: expr, $cmd: expr) => {{
  25. wait_queue_macros::__wq_wait_event!($wq, $condition, true, Ok(()), {
  26. $cmd;
  27. crate::sched::schedule(SchedMode::SM_NONE)
  28. })
  29. }};
  30. }
  31. #[macro_export]
  32. macro_rules! __wq_wait_event(
  33. ($wq:expr, $condition: expr, $interruptible: expr, $ret: expr, $cmd:expr) => {{
  34. let mut retval = $ret;
  35. let mut exec_finish_wait = true;
  36. loop {
  37. let x = $wq.prepare_to_wait_event($interruptible);
  38. if $condition {
  39. break;
  40. }
  41. if $interruptible && !x.is_ok() {
  42. retval = x;
  43. exec_finish_wait = false;
  44. break;
  45. }
  46. $cmd;
  47. }
  48. if exec_finish_wait {
  49. $wq.finish_wait();
  50. }
  51. retval
  52. }};
  53. );