nix/sys/
pthread.rs

1//! Low level threading primitives
2
3#[cfg(not(target_os = "redox"))]
4use crate::errno::Errno;
5#[cfg(not(target_os = "redox"))]
6use crate::Result;
7#[cfg(not(target_os = "redox"))]
8use crate::sys::signal::Signal;
9use libc::{self, pthread_t};
10
11/// Identifies an individual thread.
12pub type Pthread = pthread_t;
13
14/// Obtain ID of the calling thread (see
15/// [`pthread_self(3)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_self.html)
16///
17/// The thread ID returned by `pthread_self()` is not the same thing as
18/// the kernel thread ID returned by a call to `gettid(2)`.
19#[inline]
20pub fn pthread_self() -> Pthread {
21    unsafe { libc::pthread_self() }
22}
23
24/// Send a signal to a thread (see [`pthread_kill(3)`]).
25///
26/// If `signal` is `None`, `pthread_kill` will only preform error checking and
27/// won't send any signal.
28///
29/// [`pthread_kill(3)`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_kill.html
30#[allow(clippy::not_unsafe_ptr_arg_deref)]
31#[cfg(not(target_os = "redox"))]
32pub fn pthread_kill<T: Into<Option<Signal>>>(thread: Pthread, signal: T) -> Result<()> {
33    let sig = match signal.into() {
34        Some(s) => s as libc::c_int,
35        None => 0,
36    };
37    let res = unsafe { libc::pthread_kill(thread, sig) };
38    Errno::result(res).map(drop)
39}