lib.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #![no_std]
  2. use core::sync::atomic::{compiler_fence, Ordering};
  3. #[arch::define_percpu]
  4. static PREEMPT_COUNT: usize = 0;
  5. #[inline(always)]
  6. pub fn disable() {
  7. PREEMPT_COUNT.add(1);
  8. compiler_fence(Ordering::AcqRel);
  9. }
  10. #[inline(always)]
  11. pub fn enable() {
  12. compiler_fence(Ordering::AcqRel);
  13. PREEMPT_COUNT.sub(1);
  14. }
  15. #[inline(always)]
  16. pub fn count() -> usize {
  17. PREEMPT_COUNT.get()
  18. }
  19. #[macro_export]
  20. macro_rules! assert_preempt_enabled {
  21. () => {{
  22. assert_eq!($crate::count(), 0, "Preemption is not enabled",);
  23. }};
  24. ($msg:literal) => {{
  25. assert_eq!($crate::count(), 0, "{}: Preemption is not enabled", $msg,);
  26. }};
  27. }
  28. #[macro_export]
  29. macro_rules! assert_preempt_disabled {
  30. () => {{
  31. assert_ne!($crate::count(), 0, "Preemption is not disabled",);
  32. }};
  33. ($msg:literal) => {{
  34. assert_ne!($crate::count(), 0, "{}: Preemption is not disabled", $msg,);
  35. }};
  36. }
  37. #[macro_export]
  38. macro_rules! assert_preempt_count_eq {
  39. ($n:expr) => {{
  40. assert_eq!(
  41. $crate::count(),
  42. $n,
  43. "Preemption count does not equal to {}",
  44. $n,
  45. );
  46. }};
  47. ($n:expr, $msg:literal) => {{
  48. assert_eq!(
  49. $crate::count(),
  50. $n,
  51. "{}: Preemption count does not equal to {}",
  52. $msg,
  53. $n,
  54. );
  55. }};
  56. }