From 1990ab35d387b1fa12e2991643cf2fa3f06e1a44 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sat, 15 Sep 2018 07:35:35 +0000 Subject: [PATCH] firmware: implement mutexes. --- artiq/firmware/runtime/sched.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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);