split out pthread types for threads header

This commit is contained in:
auronandace
2026-07-06 16:41:09 +01:00
parent 52bb3bbfe3
commit d7859174b7
11 changed files with 134 additions and 46 deletions
+2 -7
View File
@@ -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 <bits/pthread-t.h> // for pthread_t from sys/types.h
#include <bits/pthreadattr-t.h> // for pthread_attr_t from sys/types.h
#define PTHREAD_ONCE_INIT ((pthread_once_t){0})
#include <bits/pthreadonce-t.h> // for pthread_once_t from sys/types.h
#include <bits/threads.h> // 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]
+18 -33
View File
@@ -1,8 +1,17 @@
//! pthread types for `sys/types.h` implementation.
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_types.h.html>.
#![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;
+18
View File
@@ -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 <bits/pthreadonce-t.h> // 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
+5
View File
@@ -0,0 +1,5 @@
//! `PTHREAD_ONCE_INIT` for `pthread.h` implementation.
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/pthread.h.html>.
//!
//! Implemented via C define in cbindgen.
@@ -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
+17
View File
@@ -0,0 +1,17 @@
//! `pthread_once_t` for `sys/types.h` implementation.
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_types.h.html>.
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);
+17
View File
@@ -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
+27
View File
@@ -0,0 +1,27 @@
//! pthread types shared between `threads.h` and `pthread.h`.
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_types.h.html>.
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);
+4
View File
@@ -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"]
+5 -2
View File
@@ -4,11 +4,14 @@
# - "The <pthread.h> 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 <sys/types.h>."
# - "Inclusion of the <pthread.h> header shall make symbols defined in the headers <sched.h> and <time.h> 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 <bits/pthread.h> // for pthread types from sys/types.h
#include <bits/pthreadoi.h> // for PTHREAD_ONCE_INIT
// A value that shall not compare equal to any exiting thread.
#define PTHREAD_NULL (pthread_t(NULL))
+5 -4
View File
@@ -3,8 +3,7 @@
# Spec quotations relating to includes:
# - "Inclusion of the <threads.h> header shall make symbols defined in the header <time.h> 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 <bits/pthread.h> // for pthread-related types
#include <bits/limits/ptdi.h> // for PTHREAD_DESTRUCTOR_ITERATIONS
#include <bits/limits/ptdi.h> // for PTHREAD_DESTRUCTOR_ITERATIONS from limits.h
#include <bits/pthreadoi.h> // for PTHREAD_ONCE_INIT from pthread.h
#include <bits/pthread-t.h> // for pthread_t from sys/types.h
#include <bits/threads.h> // for pthread_{key|mutex|cond}_t from sys/types.h
#ifndef __cplusplus
#define thread_local _Thread_local