axlibc/
pthread.rs

1use crate::{ctypes, utils::e};
2use arceos_posix_api as api;
3use core::ffi::{c_int, c_void};
4
5/// Returns the `pthread` struct of current thread.
6#[unsafe(no_mangle)]
7pub unsafe extern "C" fn pthread_self() -> ctypes::pthread_t {
8    api::sys_pthread_self()
9}
10
11/// Create a new thread with the given entry point and argument.
12///
13/// If successful, it stores the pointer to the newly created `struct __pthread`
14/// in `res` and returns 0.
15#[unsafe(no_mangle)]
16pub unsafe extern "C" fn pthread_create(
17    res: *mut ctypes::pthread_t,
18    attr: *const ctypes::pthread_attr_t,
19    start_routine: extern "C" fn(arg: *mut c_void) -> *mut c_void,
20    arg: *mut c_void,
21) -> c_int {
22    e(api::sys_pthread_create(res, attr, start_routine, arg))
23}
24
25/// Exits the current thread. The value `retval` will be returned to the joiner.
26#[unsafe(no_mangle)]
27pub unsafe extern "C" fn pthread_exit(retval: *mut c_void) -> ! {
28    api::sys_pthread_exit(retval)
29}
30
31/// Waits for the given thread to exit, and stores the return value in `retval`.
32#[unsafe(no_mangle)]
33pub unsafe extern "C" fn pthread_join(
34    thread: ctypes::pthread_t,
35    retval: *mut *mut c_void,
36) -> c_int {
37    e(api::sys_pthread_join(thread, retval))
38}
39
40/// Initialize a mutex.
41#[unsafe(no_mangle)]
42pub unsafe extern "C" fn pthread_mutex_init(
43    mutex: *mut ctypes::pthread_mutex_t,
44    attr: *const ctypes::pthread_mutexattr_t,
45) -> c_int {
46    e(api::sys_pthread_mutex_init(mutex, attr))
47}
48
49/// Lock the given mutex.
50#[unsafe(no_mangle)]
51pub unsafe extern "C" fn pthread_mutex_lock(mutex: *mut ctypes::pthread_mutex_t) -> c_int {
52    e(api::sys_pthread_mutex_lock(mutex))
53}
54
55/// Unlock the given mutex.
56#[unsafe(no_mangle)]
57pub unsafe extern "C" fn pthread_mutex_unlock(mutex: *mut ctypes::pthread_mutex_t) -> c_int {
58    e(api::sys_pthread_mutex_unlock(mutex))
59}