From e850dc8c45636826ebac016d266a326c96d05fc9 Mon Sep 17 00:00:00 2001 From: Zhe Wang <0x1998@gmail.com> Date: Fri, 26 Feb 2016 18:17:18 +0800 Subject: Add sigwait --- src/sys/signal.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/sys/signal.rs b/src/sys/signal.rs index b21a0a96..3822d391 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -295,6 +295,8 @@ mod ffi { pub fn kill(pid: pid_t, signum: c_int) -> c_int; pub fn raise(signum: c_int) -> c_int; + + pub fn sigwait(set: *const sigset_t, sig: *mut c_int) -> c_int; } } @@ -370,6 +372,15 @@ impl SigSet { try!(pthread_sigmask(how, Some(self), Some(&mut oldmask))); Ok(oldmask) } + + /// Suspends execution of the calling thread until one of the signals in the + /// signal mask becomes pending, and returns the accepted signal. + pub fn wait(&self) -> Result { + let mut signum: SigNum = unsafe { mem::uninitialized() }; + let res = unsafe { ffi::sigwait(&self.sigset as *const sigset_t, &mut signum) }; + + Errno::result(res).map(|_| signum) + } } impl AsRef for SigSet { @@ -512,4 +523,15 @@ mod tests { assert!(oldmask.contains(SIGUSR1).unwrap()); assert!(!oldmask.contains(SIGUSR2).unwrap()); } + + #[test] + fn test_sigwait() { + let mut mask = SigSet::empty(); + mask.add(SIGUSR1).unwrap(); + mask.add(SIGUSR2).unwrap(); + mask.thread_block().unwrap(); + + raise(SIGUSR1).unwrap(); + assert_eq!(mask.wait().unwrap(), SIGUSR1); + } } -- cgit v1.2.3