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