Compare commits
2 Commits
bb09d25378
...
39f672dde8
Author | SHA1 | Date |
---|---|---|
pca006132 | 39f672dde8 | |
pca006132 | c13ca614ef |
|
@ -81,7 +81,14 @@ impl Sockets {
|
|||
/// TODO: this was called through eg. TcpStream, another poll()
|
||||
/// might want to send packets before sleeping for an interrupt.
|
||||
pub(crate) fn register_waker(waker: Waker) {
|
||||
Self::instance().wakers.borrow_mut()
|
||||
.push(waker);
|
||||
let mut wakers = Self::instance().wakers.borrow_mut();
|
||||
for (i, w) in wakers.iter().enumerate() {
|
||||
if w.will_wake(&waker) {
|
||||
let last = wakers.len() - 1;
|
||||
wakers.swap(i, last);
|
||||
return;
|
||||
}
|
||||
}
|
||||
wakers.push(waker);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use core::sync::atomic::{AtomicU32, Ordering};
|
|||
use core::cell::UnsafeCell;
|
||||
use super::{
|
||||
spin_lock_yield, notify_spin_lock,
|
||||
asm::{dmb, enter_critical, exit_critical}
|
||||
asm::{enter_critical, exit_critical}
|
||||
};
|
||||
|
||||
const LOCKED: u32 = 1;
|
||||
|
@ -32,30 +32,27 @@ impl<T> Mutex<T> {
|
|||
/// Lock the Mutex, blocks when already locked
|
||||
pub fn lock(&self) -> MutexGuard<T> {
|
||||
let mut irq = unsafe { enter_critical() };
|
||||
while self.locked.compare_and_swap(UNLOCKED, LOCKED, Ordering::Acquire) != UNLOCKED {
|
||||
while self.locked.compare_and_swap(UNLOCKED, LOCKED, Ordering::AcqRel) != UNLOCKED {
|
||||
unsafe {
|
||||
exit_critical(irq);
|
||||
spin_lock_yield();
|
||||
irq = enter_critical();
|
||||
}
|
||||
}
|
||||
dmb();
|
||||
MutexGuard { mutex: self, irq }
|
||||
}
|
||||
|
||||
pub fn try_lock(&self) -> Option<MutexGuard<T>> {
|
||||
let irq = unsafe { enter_critical() };
|
||||
if self.locked.compare_and_swap(UNLOCKED, LOCKED, Ordering::Acquire) != UNLOCKED {
|
||||
if self.locked.compare_and_swap(UNLOCKED, LOCKED, Ordering::AcqRel) != UNLOCKED {
|
||||
unsafe { exit_critical(irq) };
|
||||
None
|
||||
} else {
|
||||
dmb();
|
||||
Some(MutexGuard { mutex: self, irq })
|
||||
}
|
||||
}
|
||||
|
||||
fn unlock(&self) {
|
||||
dmb();
|
||||
self.locked.store(UNLOCKED, Ordering::Release);
|
||||
|
||||
notify_spin_lock();
|
||||
|
|
Loading…
Reference in New Issue