Skip to content

Commit

Permalink
Add pthread_kill
Browse files Browse the repository at this point in the history
  • Loading branch information
mkroening committed Jul 19, 2021
1 parent 7033d47 commit eac7bf1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

- Added `IPV6_V6ONLY` sockopt.
(#[1470](https://github.com/nix-rust/nix/pull/1470))
- Added `pthread_kill`.
(#[1472](https://github.com/nix-rust/nix/pull/1472))

### Changed

Expand Down
16 changes: 16 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use crate::{Error, Result};
use crate::errno::Errno;
use crate::unistd::Pid;
use crate::sys::pthread::Pthread;
use std::convert::TryFrom;
use std::mem;
use std::fmt;
Expand Down Expand Up @@ -781,6 +782,21 @@ pub fn killpg<T: Into<Option<Signal>>>(pgrp: Pid, signal: T) -> Result<()> {
Errno::result(res).map(drop)
}

/// Send a signal to a thread (see [`pthread_kill(3)`]).
///
/// If `signal` is `None`, `pthread_kill` will only preform error checking and
/// won't send any signal.
///
/// [`pthread_kill(3)`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_kill.html
pub fn pthread_kill<T: Into<Option<Signal>>>(thread: Pthread, signal: T) -> Result<()> {
let sig = match signal.into() {
Some(s) => s as libc::c_int,
None => 0,
};
let res = unsafe { libc::pthread_kill(thread, sig) };
Errno::result(res).map(drop)
}

pub fn raise(signal: Signal) -> Result<()> {
let res = unsafe { libc::raise(signal as libc::c_int) };

Expand Down
7 changes: 7 additions & 0 deletions test/sys/test_signal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(not(target_os = "redox"))]
use nix::errno::Errno;
use nix::sys::pthread::pthread_self;
use nix::sys::signal::*;
use nix::unistd::*;
use std::convert::TryFrom;
Expand All @@ -17,6 +18,12 @@ fn test_killpg_none() {
.expect("Should be able to send signal to my process group.");
}

#[test]
fn test_pthread_kill_none() {
pthread_kill(pthread_self(), None)
.expect("Should be able to send signal to my thread.");
}

#[test]
fn test_old_sigaction_flags() {
let _m = crate::SIGNAL_MTX.lock().expect("Mutex got poisoned by another test");
Expand Down

0 comments on commit eac7bf1

Please sign in to comment.