diff --git a/artiq/firmware/runtime/sched.rs b/artiq/firmware/runtime/sched.rs index c28fff956..4c027e895 100644 --- a/artiq/firmware/runtime/sched.rs +++ b/artiq/firmware/runtime/sched.rs @@ -265,6 +265,28 @@ impl<'a> Io<'a> { } } +pub struct Mutex(Urc>); + +impl Mutex { + pub fn new() -> Mutex { + Mutex(Urc::new(Cell::new(false))) + } + + pub fn lock<'a>(&'a self, io: Io) -> Result, Error> { + io.until(|| !self.0.get())?; + self.0.set(true); + Ok(MutexGuard(&*self.0)) + } +} + +pub struct MutexGuard<'a>(&'a Cell); + +impl<'a> Drop for MutexGuard<'a> { + fn drop(&mut self) { + self.0.set(false) + } +} + macro_rules! until { ($socket:expr, $ty:ty, |$var:ident| $cond:expr) => ({ let (sockets, handle) = ($socket.io.sockets.clone(), $socket.handle);