xous-rs: add TryReceiveMessage

This non-blocking function call will retrieve a message if one exists,
or will immediately return None if the queue is empty.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-11-25 20:20:59 +08:00
parent d583e20b18
commit 6604bfcdb0
2 changed files with 48 additions and 1 deletions

View file

@ -475,6 +475,9 @@ pub enum Result {
/// functions such as `try_connect()` and `try_send()` that may block.
WouldBlock,
/// The message was successful but no value was returned.
None,
UnknownResult(usize, usize, usize, usize, usize, usize, usize),
}
@ -511,6 +514,7 @@ impl Result {
[15, s.0 as _, s.1 as _, s.2 as _, s.3 as _, *cid, 0, 0]
}
Result::WouldBlock => [16, 0, 0, 0, 0, 0, 0, 0],
Result::None => [17, 0, 0, 0, 0, 0, 0, 0],
Result::UnknownResult(arg1, arg2, arg3, arg4, arg5, arg6, arg7) => {
[usize::MAX, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6, *arg7]
}
@ -585,6 +589,7 @@ impl Result {
src[5] as _,
),
16 => Result::WouldBlock,
17 => Result::None,
_ => Result::UnknownResult(src[0], src[1], src[2], src[3], src[4], src[5], src[6]),
}
}

View file

@ -115,11 +115,16 @@ pub enum SysCall {
/// This process will now wait for an event such as an IRQ or Message.
WaitEvent,
/// This context will now wait for a message with the given server ID. You
/// This thread will now wait for a message with the given server ID. You
/// can set up a pool by having multiple threads call `ReceiveMessage` with
/// the same SID.
ReceiveMessage(SID),
/// If a message is available for the specified server, return that message
/// and resume execution. If no message is available, return `Result::None`
/// immediately without blocking.
TryReceiveMessage(SID),
/// Stop running the given process and return control to the parent. This
/// will force a Yield on the process currently running on the target CPU.
/// This can be run during an Interrupt context.
@ -301,6 +306,7 @@ pub enum SysCallNumber {
TryConnect = 25,
ReturnScalar1 = 26,
ReturnScalar2 = 27,
TryReceiveMessage = 28,
Invalid,
}
@ -334,6 +340,7 @@ impl SysCallNumber {
25 => TryConnect,
26 => ReturnScalar1,
27 => ReturnScalar2,
28 => TryReceiveMessage,
_ => Invalid,
}
}
@ -386,6 +393,19 @@ impl SysCall {
0,
]
}
SysCall::TryReceiveMessage(sid) => {
let s = sid.to_u32();
[
SysCallNumber::TryReceiveMessage as usize,
s.0 as _,
s.1 as _,
s.2 as _,
s.3 as _,
0,
0,
0,
]
}
SysCall::ReturnToParent(a1, a2) => [
SysCallNumber::ReturnToParent as usize,
a1.get() as usize,
@ -639,6 +659,9 @@ impl SysCall {
SysCallNumber::ReceiveMessage => {
SysCall::ReceiveMessage(SID::from_u32(a1 as _, a2 as _, a3 as _, a4 as _))
}
SysCallNumber::TryReceiveMessage => {
SysCall::TryReceiveMessage(SID::from_u32(a1 as _, a2 as _, a3 as _, a4 as _))
}
SysCallNumber::ReturnToParent => SysCall::ReturnToParent(pid_from_usize(a1)?, a2),
SysCallNumber::ClaimInterrupt => SysCall::ClaimInterrupt(
a1,
@ -852,6 +875,7 @@ impl SysCall {
pub fn can_call_from_interrupt(&self) -> bool {
matches!(self, SysCall::TrySendMessage(_, _)
| SysCall::TryConnect(_)
| SysCall::TryReceiveMessage(_)
| SysCall::ReturnToParent(_, _)
| SysCall::ReturnScalar2(_, _, _)
| SysCall::ReturnScalar1(_, _)
@ -1051,6 +1075,24 @@ pub fn receive_message(server: SID) -> core::result::Result<MessageEnvelope, Err
}
}
/// Retrieve a message from the message queue for the provided server. If no message
/// is available, returns `Ok(None)` without blocking
///
/// # Errors
///
pub fn try_receive_message(server: SID) -> core::result::Result<Option<MessageEnvelope>, Error> {
let result = rsyscall(SysCall::TryReceiveMessage(server)).expect("Couldn't call ReceiveMessage");
if let Result::Message(envelope) = result {
Ok(Some(envelope))
} else if result == Result::None {
Ok(None)
} else if let Result::Error(e) = result {
Err(e)
} else {
Err(Error::InternalError)
}
}
/// Send a message to a server. Depending on the mesage type (move or borrow), it
/// will either block (borrow) or return immediately (move).
/// If the message type is `borrow`, then the memory addresses pointed to will be