Skip to content

Commit 3d5d369

Browse files
committed
Add pthread_kill
1 parent 7033d47 commit 3d5d369

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
@@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
88

99
- Added `IPV6_V6ONLY` sockopt.
1010
(#[1470](https://github.com/nix-rust/nix/pull/1470))
11+
- Added `pthread_kill`.
12+
(#[1472](https://github.com/nix-rust/nix/pull/1472))
1113

1214
### Changed
1315

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)