Skip to content

Commit e1351ab

Browse files
bors[bot]mkroening
andauthored
Merge #1472
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 <[email protected]>
2 parents e88a6cf + 3d5d369 commit e1351ab

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1111

1212
- Added `IPV6_V6ONLY` sockopt.
1313
(#[1470](https://github.com/nix-rust/nix/pull/1470))
14+
- Added `pthread_kill`.
15+
(#[1472](https://github.com/nix-rust/nix/pull/1472))
1416

1517
### Changed
1618

src/sys/pthread.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#[cfg(not(target_os = "redox"))]
2+
use crate::errno::Errno;
3+
#[cfg(not(target_os = "redox"))]
4+
use crate::Result;
5+
#[cfg(not(target_os = "redox"))]
6+
use crate::sys::signal::Signal;
17
use libc::{self, pthread_t};
28

39
pub type Pthread = pthread_t;
@@ -11,3 +17,19 @@ pub type Pthread = pthread_t;
1117
pub fn pthread_self() -> Pthread {
1218
unsafe { libc::pthread_self() }
1319
}
20+
21+
/// Send a signal to a thread (see [`pthread_kill(3)`]).
22+
///
23+
/// If `signal` is `None`, `pthread_kill` will only preform error checking and
24+
/// won't send any signal.
25+
///
26+
/// [`pthread_kill(3)`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_kill.html
27+
#[cfg(not(target_os = "redox"))]
28+
pub fn pthread_kill<T: Into<Option<Signal>>>(thread: Pthread, signal: T) -> Result<()> {
29+
let sig = match signal.into() {
30+
Some(s) => s as libc::c_int,
31+
None => 0,
32+
};
33+
let res = unsafe { libc::pthread_kill(thread, sig) };
34+
Errno::result(res).map(drop)
35+
}

test/sys/test_pthread.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ fn test_pthread_self() {
1313
let tid = pthread_self();
1414
assert!(tid != 0);
1515
}
16+
17+
#[test]
18+
#[cfg(not(target_os = "redox"))]
19+
fn test_pthread_kill_none() {
20+
pthread_kill(pthread_self(), None)
21+
.expect("Should be able to send signal to my thread.");
22+
}

0 commit comments

Comments
 (0)