Skip to content

Commit

Permalink
Merge #1472
Browse files Browse the repository at this point in the history
1472: Add pthread_kill r=asomers a=mkroening

This adds `pthread_kill`, following the design of `killpg`.

What do you think?

Co-authored-by: Martin Kröning <mkroening@posteo.net>
  • Loading branch information
bors[bot] and mkroening committed Jul 24, 2021
2 parents e88a6cf + 3d5d369 commit e1351ab
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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 e1351ab

Please sign in to comment.