spin.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. use super::strategy::LockStrategy;
  2. use core::{
  3. arch::asm,
  4. marker::PhantomData,
  5. sync::atomic::{AtomicBool, Ordering},
  6. };
  7. pub struct SpinStrategy;
  8. pub struct IrqStrategy<Strategy: LockStrategy>(PhantomData<Strategy>);
  9. impl SpinStrategy {
  10. fn is_locked(data: &<Self as LockStrategy>::StrategyData) -> bool {
  11. data.load(Ordering::Relaxed)
  12. }
  13. }
  14. unsafe impl LockStrategy for SpinStrategy {
  15. type StrategyData = AtomicBool;
  16. type GuardContext = ();
  17. fn new_data() -> Self::StrategyData {
  18. AtomicBool::new(false)
  19. }
  20. unsafe fn is_locked(data: &Self::StrategyData) -> bool {
  21. data.load(Ordering::Relaxed)
  22. }
  23. unsafe fn try_lock(data: &Self::StrategyData) -> Option<Self::GuardContext> {
  24. use Ordering::{Acquire, Relaxed};
  25. eonix_preempt::disable();
  26. if data.compare_exchange(false, true, Acquire, Relaxed).is_ok() {
  27. Some(())
  28. } else {
  29. None
  30. }
  31. }
  32. unsafe fn do_lock(data: &Self::StrategyData) -> Self::GuardContext {
  33. use Ordering::{Acquire, Relaxed};
  34. eonix_preempt::disable();
  35. while data
  36. .compare_exchange_weak(false, true, Acquire, Relaxed)
  37. .is_err()
  38. {
  39. while Self::is_locked(data) {
  40. core::hint::spin_loop();
  41. }
  42. }
  43. }
  44. unsafe fn do_unlock(data: &Self::StrategyData, _: &mut Self::GuardContext) {
  45. data.store(false, Ordering::Release);
  46. eonix_preempt::enable();
  47. }
  48. }
  49. unsafe impl<Strategy: LockStrategy> LockStrategy for IrqStrategy<Strategy> {
  50. type StrategyData = Strategy::StrategyData;
  51. type GuardContext = (Strategy::GuardContext, usize);
  52. fn new_data() -> Self::StrategyData {
  53. Strategy::new_data()
  54. }
  55. unsafe fn do_lock(data: &Self::StrategyData) -> Self::GuardContext {
  56. let mut context: usize;
  57. unsafe {
  58. asm!(
  59. "pushf",
  60. "pop {context}",
  61. "cli",
  62. context = out(reg) context,
  63. );
  64. }
  65. unsafe { (Strategy::do_lock(data), context) }
  66. }
  67. unsafe fn do_unlock(data: &Self::StrategyData, context: &mut Self::GuardContext) {
  68. unsafe {
  69. Strategy::do_unlock(data, &mut context.0);
  70. asm!(
  71. "push {context}",
  72. "popf",
  73. context = in(reg) context.1,
  74. options(nomem),
  75. )
  76. }
  77. }
  78. unsafe fn do_temporary_unlock(data: &Self::StrategyData, context: &mut Self::GuardContext) {
  79. unsafe { Strategy::do_unlock(data, &mut context.0) }
  80. }
  81. unsafe fn do_relock(data: &Self::StrategyData, context: &mut Self::GuardContext) {
  82. unsafe { Strategy::do_relock(data, &mut context.0) }
  83. }
  84. unsafe fn is_locked(data: &Self::StrategyData) -> bool {
  85. unsafe { Strategy::is_locked(data) }
  86. }
  87. unsafe fn try_lock(data: &Self::StrategyData) -> Option<Self::GuardContext> {
  88. let mut irq_context: usize;
  89. unsafe {
  90. asm!(
  91. "pushf",
  92. "pop {context}",
  93. "cli",
  94. context = out(reg) irq_context,
  95. );
  96. }
  97. let lock_context = unsafe { Strategy::try_lock(data) };
  98. lock_context.map(|lock_context| (lock_context, irq_context))
  99. }
  100. }