Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SigSet::union and SigSet::clear. #347

Merged
merged 1 commit into from
Apr 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,27 @@ impl SigSet {
Errno::result(res).map(drop)
}

pub fn clear(&mut self) -> Result<()> {
let res = unsafe { libc::sigemptyset(&mut self.sigset as *mut libc::sigset_t) };

Errno::result(res).map(drop)
}

pub fn remove(&mut self, signum: SigNum) -> Result<()> {
let res = unsafe { libc::sigdelset(&mut self.sigset as *mut libc::sigset_t, signum) };

Errno::result(res).map(drop)
}

pub fn extend(&mut self, other: &SigSet) -> Result<()> {
for i in 1..NSIG {
if try!(other.contains(i)) {
try!(self.add(i));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this is a loop! I don't know if we want what looks like 1-2 bitwise operations to actually end up being ~50-100. My feeling is that at this level, small code should have small cost.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would think! But even as a user of libc we should be unaware of the implementation details of sigaddset etc.

}
Ok(())
}

pub fn contains(&self, signum: SigNum) -> Result<bool> {
let res = unsafe { libc::sigismember(&self.sigset as *const libc::sigset_t, signum) };

Expand Down Expand Up @@ -260,6 +275,28 @@ mod tests {
assert_eq!(all.contains(SIGUSR2), Ok(true));
}

#[test]
fn test_clear() {
let mut set = SigSet::all();
set.clear().unwrap();
for i in 1..NSIG {
assert_eq!(set.contains(i), Ok(false));
}
}

#[test]
fn test_extend() {
let mut one_signal = SigSet::empty();
one_signal.add(SIGUSR1).unwrap();

let mut two_signals = SigSet::empty();
two_signals.add(SIGUSR2).unwrap();
two_signals.extend(&one_signal).unwrap();

assert_eq!(two_signals.contains(SIGUSR1), Ok(true));
assert_eq!(two_signals.contains(SIGUSR2), Ok(true));
}

#[test]
fn test_thread_signal_block() {
let mut mask = SigSet::empty();
Expand Down