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 24, 2021
1 parent 7033d47 commit 3d5d369
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
22 changes: 22 additions & 0 deletions src/sys/pthread.rs
@@ -1,3 +1,9 @@
#[cfg(not(target_os = "redox"))]
use crate::errno::Errno;
#[cfg(not(target_os = "redox"))]
use crate::Result;
#[cfg(not(target_os = "redox"))]
use crate::sys::signal::Signal;
use libc::{self, pthread_t};

pub type Pthread = pthread_t;
Expand All @@ -11,3 +17,19 @@ pub type Pthread = pthread_t;
pub fn pthread_self() -> Pthread {
unsafe { libc::pthread_self() }
}

/// 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
#[cfg(not(target_os = "redox"))]
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)
}
7 changes: 7 additions & 0 deletions test/sys/test_pthread.rs
Expand Up @@ -13,3 +13,10 @@ fn test_pthread_self() {
let tid = pthread_self();
assert!(tid != 0);
}

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

0 comments on commit 3d5d369

Please sign in to comment.