diff --git a/src/header/bits_pthread/cbindgen.toml b/src/header/bits_pthread/cbindgen.toml index 9fc60c2f24..3865ccc94c 100644 --- a/src/header/bits_pthread/cbindgen.toml +++ b/src/header/bits_pthread/cbindgen.toml @@ -12,12 +12,11 @@ language = "C" style = "type" no_includes = true cpp_compat = true -# TODO split out PTHREAD_ONCE_INIT for threads.h after_includes = """ #include // for pthread_t from sys/types.h #include // for pthread_attr_t from sys/types.h - -#define PTHREAD_ONCE_INIT ((pthread_once_t){0}) +#include // for pthread_once_t from sys/types.h +#include // for pthread_{key|mutex|cond}_t from sys/types.h """ @@ -27,13 +26,9 @@ include = [ "pthread_rwlock_t", "pthread_barrier_t", "pthread_barrierattr_t", - "pthread_mutex_t", "pthread_mutexattr_t", "pthread_condattr_t", - "pthread_cond_t", "pthread_spinlock_t", - "pthread_once_t", - "pthread_key_t", ] [enum] diff --git a/src/header/bits_pthread/mod.rs b/src/header/bits_pthread/mod.rs index 615e964425..e0a2cf4483 100644 --- a/src/header/bits_pthread/mod.rs +++ b/src/header/bits_pthread/mod.rs @@ -1,8 +1,17 @@ +//! pthread types for `sys/types.h` implementation. +//! +//! See . + #![allow(non_camel_case_types)] -use crate::platform::types::{c_int, c_uchar, c_ulong}; +use crate::platform::types::{c_int, c_uchar}; -pub use crate::header::{bits_pthread_t::pthread_t, bits_pthreadattr_t::pthread_attr_t}; +pub use crate::header::{ + bits_pthread_t::pthread_t, + bits_pthreadattr_t::pthread_attr_t, + bits_pthreadonce_t::pthread_once_t, + bits_threads::{pthread_cond_t, pthread_key_t, pthread_mutex_t}, +}; // XXX: https://github.com/eqrion/cbindgen/issues/685 // @@ -10,66 +19,48 @@ pub use crate::header::{bits_pthread_t::pthread_t, bits_pthreadattr_t::pthread_a // expanding macros! Instead, we rely on checking that the lengths are correct, when these headers // are parsed in the regular compilation phase. -/// The `pthread_rwlockattr_t` type provided in [`sys/types.h`](crate::header::sys_types). +/// Used for read/write lock attributes. #[repr(C)] pub union pthread_rwlockattr_t { __relibc_internal_size: [c_uchar; 1], __relibc_internal_align: c_uchar, } -/// The `pthread_rwlock_t` type provided in [`sys/types.h`](crate::header::sys_types). +/// Used for read-write locks. #[repr(C)] pub union pthread_rwlock_t { __relibc_internal_size: [c_uchar; 4], __relibc_internal_align: c_int, } -/// The `pthread_barrier_t` type provided in [`sys/types.h`](crate::header::sys_types). +/// Used to identify a barrier. #[repr(C)] pub union pthread_barrier_t { __relibc_internal_size: [c_uchar; 24], __relibc_internal_align: c_int, } -/// The `pthread_barrierattr_t` type provided in [`sys/types.h`](crate::header::sys_types). +/// Used to define a barrier attributes object. #[repr(C)] pub union pthread_barrierattr_t { __relibc_internal_size: [c_uchar; 4], __relibc_internal_align: c_int, } -/// The `pthread_mutex_t` type provided in [`sys/types.h`](crate::header::sys_types). -#[repr(C)] -pub union pthread_mutex_t { - __relibc_internal_size: [c_uchar; 12], - __relibc_internal_align: c_int, -} -/// The `pthread_mutexattr_t` type provided in [`sys/types.h`](crate::header::sys_types). +/// Used to identify a mutex attribute object. #[repr(C)] pub union pthread_mutexattr_t { __relibc_internal_size: [c_uchar; 20], __relibc_internal_align: c_int, } -/// The `pthread_cond_t` type provided in [`sys/types.h`](crate::header::sys_types). -#[repr(C)] -pub union pthread_cond_t { - __relibc_internal_size: [c_uchar; 8], - __relibc_internal_align: c_int, -} -/// The `pthread_condattr_t` type provided in [`sys/types.h`](crate::header::sys_types). +/// Used to identify a condition attribute object. #[repr(C)] pub union pthread_condattr_t { __relibc_internal_size: [c_uchar; 8], __relibc_internal_align: c_int, } -/// The `pthread_spinlock_t` type provided in [`sys/types.h`](crate::header::sys_types). +/// Used to identify a spin lock. #[repr(C)] pub union pthread_spinlock_t { __relibc_internal_size: [c_uchar; 4], __relibc_internal_align: c_int, } -/// The `pthread_once_t` type provided in [`sys/types.h`](crate::header::sys_types). -#[repr(C)] -pub union pthread_once_t { - __relibc_internal_size: [c_uchar; 4], - __relibc_internal_align: c_int, -} #[macro_export] macro_rules! pthread_assert_equal_size( @@ -106,12 +97,6 @@ pthread_assert_equal_size!(pthread_rwlock_t, RlctRwlock); pthread_assert_equal_size!(pthread_rwlockattr_t, RlctRwlockAttr); pthread_assert_equal_size!(pthread_barrier_t, RlctBarrier); pthread_assert_equal_size!(pthread_barrierattr_t, RlctBarrierAttr); -pthread_assert_equal_size!(pthread_mutex_t, RlctMutex); pthread_assert_equal_size!(pthread_mutexattr_t, RlctMutexAttr); -pthread_assert_equal_size!(pthread_cond_t, RlctCond); pthread_assert_equal_size!(pthread_condattr_t, RlctCondAttr); pthread_assert_equal_size!(pthread_spinlock_t, RlctSpinlock); -pthread_assert_equal_size!(pthread_once_t, RlctOnce); - -/// The `pthread_key_t` type provided in [`sys/types.h`](crate::header::sys_types). -pub type pthread_key_t = c_ulong; diff --git a/src/header/bits_pthreadoi/cbindgen.toml b/src/header/bits_pthreadoi/cbindgen.toml new file mode 100644 index 0000000000..6de4d076bd --- /dev/null +++ b/src/header/bits_pthreadoi/cbindgen.toml @@ -0,0 +1,18 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/pthread.h.html +# +# This type is split out to avoid importing all of pthread.h into other headers. +# +# POSIX headers that require PTHREAD_ONCE_INIT: +# - pthread.h (where it should be defined) +# - threads.h (for ONCE_FLAG_INIT) +include_guard = "_RELIBC_BITS_PTHREAD_ONCE_INIT_H" +after_includes = """ +#include // for pthread_once_t from sys/types.h + +#define PTHREAD_ONCE_INIT ((pthread_once_t){0}) +""" +language = "C" +no_includes = true +cpp_compat = true +[enum] +prefix_with_name = true diff --git a/src/header/bits_pthreadoi/mod.rs b/src/header/bits_pthreadoi/mod.rs new file mode 100644 index 0000000000..b7016a90a5 --- /dev/null +++ b/src/header/bits_pthreadoi/mod.rs @@ -0,0 +1,5 @@ +//! `PTHREAD_ONCE_INIT` for `pthread.h` implementation. +//! +//! See . +//! +//! Implemented via C define in cbindgen. diff --git a/src/header/bits_pthreadonce-t/cbindgen.toml b/src/header/bits_pthreadonce-t/cbindgen.toml new file mode 100644 index 0000000000..8f6112aca7 --- /dev/null +++ b/src/header/bits_pthreadonce-t/cbindgen.toml @@ -0,0 +1,16 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_types.h.html +# +# This type is split out to prevent importing all of sys/types.h into other headers. +# +# POSIX headers that require pthread_once_t: +# - pthread.h +# - sys/types.h (where it should be defined) +# - threads.h (not in spec but POSIX admits most implementations share definitions with pthread.h) +include_guard = "_RELIBC_BITS_PTHREAD_ONCE_T_H" +language = "C" +no_includes = true +cpp_compat = true +[export] +include = ["pthread_once_t"] +[enum] +prefix_with_name = true diff --git a/src/header/bits_pthreadonce-t/mod.rs b/src/header/bits_pthreadonce-t/mod.rs new file mode 100644 index 0000000000..ce3a8605f5 --- /dev/null +++ b/src/header/bits_pthreadonce-t/mod.rs @@ -0,0 +1,17 @@ +//! `pthread_once_t` for `sys/types.h` implementation. +//! +//! See . + +use crate::{ + platform::types::{c_int, c_uchar}, + pthread_assert_equal_size, +}; + +/// Used for dynamic package initialization. +#[repr(C)] +pub union pthread_once_t { + __relibc_internal_size: [c_uchar; 4], + __relibc_internal_align: c_int, +} + +pthread_assert_equal_size!(pthread_once_t, RlctOnce); diff --git a/src/header/bits_threads/cbindgen.toml b/src/header/bits_threads/cbindgen.toml new file mode 100644 index 0000000000..c7fb9138fc --- /dev/null +++ b/src/header/bits_threads/cbindgen.toml @@ -0,0 +1,17 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_types.h.html +# +# These types are split out to prevent importing all of sys/types.h into other headers. +# +# These particular pthread types are shared between both pthread.h and threads.h. +include_guard = "_RELIBC_BITS_THREADS_H" +language = "C" +no_includes = true +cpp_compat = true +[export] +include = [ + "pthread_cond_t", + "pthread_key_t", + "pthread_mutex_t", +] +[enum] +prefix_with_name = true diff --git a/src/header/bits_threads/mod.rs b/src/header/bits_threads/mod.rs new file mode 100644 index 0000000000..ffc0e16aec --- /dev/null +++ b/src/header/bits_threads/mod.rs @@ -0,0 +1,27 @@ +//! pthread types shared between `threads.h` and `pthread.h`. +//! +//! See . + +use crate::{ + platform::types::{c_int, c_uchar, c_ulong}, + pthread_assert_equal_size, +}; + +/// Used for thread-specific data keys. +pub type pthread_key_t = c_ulong; + +/// Used for mutexes. +#[repr(C)] +pub union pthread_mutex_t { + __relibc_internal_size: [c_uchar; 12], + __relibc_internal_align: c_int, +} +/// Used for condition variables. +#[repr(C)] +pub union pthread_cond_t { + __relibc_internal_size: [c_uchar; 8], + __relibc_internal_align: c_int, +} + +pthread_assert_equal_size!(pthread_mutex_t, RlctMutex); +pthread_assert_equal_size!(pthread_cond_t, RlctCond); diff --git a/src/header/mod.rs b/src/header/mod.rs index 666f173066..6223dad705 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -42,6 +42,9 @@ pub mod bits_pthread; pub mod bits_pthread_t; #[path = "bits_pthreadattr-t/mod.rs"] pub mod bits_pthreadattr_t; +pub mod bits_pthreadoi; +#[path = "bits_pthreadonce-t/mod.rs"] +pub mod bits_pthreadonce_t; #[path = "bits_reclen-t/mod.rs"] pub mod bits_reclen_t; #[path = "bits_safamily-t/mod.rs"] @@ -58,6 +61,7 @@ pub mod bits_ssize_t; pub mod bits_suseconds_t; pub mod bits_sys_stat; pub mod bits_sys_statvfs; +pub mod bits_threads; #[path = "bits_time-t/mod.rs"] pub mod bits_time_t; #[path = "bits_timer-t/mod.rs"] diff --git a/src/header/pthread/cbindgen.toml b/src/header/pthread/cbindgen.toml index 1419ad0b6e..54b578f5be 100644 --- a/src/header/pthread/cbindgen.toml +++ b/src/header/pthread/cbindgen.toml @@ -4,11 +4,14 @@ # - "The header shall define the pthread_attr_t, pthread_barrier_t, pthread_barrierattr_t, pthread_cond_t, pthread_condattr_t, pthread_key_t, pthread_mutex_t, pthread_mutexattr_t, pthread_once_t, pthread_rwlock_t, pthread_rwlockattr_t, pthread_spinlock_t, and pthread_t types as described in ." # - "Inclusion of the header shall make symbols defined in the headers and visible." # -# bits/pthread.h bring in the types from sys/types.h +# time.h brings in NULL # features.h needed for no return (Rust never type) -sys_includes = ["sched.h", "time.h", "bits/pthread.h", "features.h"] +sys_includes = ["sched.h", "time.h", "features.h"] # TODO: Any better way to implement pthread_cleanup_push/pthread_cleanup_pop? after_includes = """ +#include // for pthread types from sys/types.h +#include // for PTHREAD_ONCE_INIT + // A value that shall not compare equal to any exiting thread. #define PTHREAD_NULL (pthread_t(NULL)) diff --git a/src/header/threads/cbindgen.toml b/src/header/threads/cbindgen.toml index af983fffdb..d01af4b585 100644 --- a/src/header/threads/cbindgen.toml +++ b/src/header/threads/cbindgen.toml @@ -3,8 +3,7 @@ # Spec quotations relating to includes: # - "Inclusion of the header shall make symbols defined in the header visible." # -# TODO split out required pthread types -# +# bits/pthreadoi.h brings in pthread_once_t # time.h brings in features.h # features.h required for Rust never type (no return) sys_includes = ["time.h"] @@ -14,8 +13,10 @@ style = "tag" no_includes = true cpp_compat = true after_includes = """ -#include // for pthread-related types -#include // for PTHREAD_DESTRUCTOR_ITERATIONS +#include // for PTHREAD_DESTRUCTOR_ITERATIONS from limits.h +#include // for PTHREAD_ONCE_INIT from pthread.h +#include // for pthread_t from sys/types.h +#include // for pthread_{key|mutex|cond}_t from sys/types.h #ifndef __cplusplus #define thread_local _Thread_local