Skip to main content

relibc/header/bits_pthread/
mod.rs

1#![allow(non_camel_case_types)]
2
3use crate::platform::types::{c_int, c_uchar, c_ulong, size_t};
4
5pub use crate::header::bits_pthread_t::pthread_t;
6
7// XXX: https://github.com/eqrion/cbindgen/issues/685
8//
9// We need to write the opaque types ourselves, and apparently cbindgen doesn't even support
10// expanding macros! Instead, we rely on checking that the lengths are correct, when these headers
11// are parsed in the regular compilation phase.
12
13/// The `pthread_attr_t` type provided in [`sys/types.h`](crate::header::sys_types).
14#[repr(C)]
15pub union pthread_attr_t {
16    __relibc_internal_size: [c_uchar; 32],
17    __relibc_internal_align: size_t,
18}
19/// The `pthread_rwlockattr_t` type provided in [`sys/types.h`](crate::header::sys_types).
20#[repr(C)]
21pub union pthread_rwlockattr_t {
22    __relibc_internal_size: [c_uchar; 1],
23    __relibc_internal_align: c_uchar,
24}
25/// The `pthread_rwlock_t` type provided in [`sys/types.h`](crate::header::sys_types).
26#[repr(C)]
27pub union pthread_rwlock_t {
28    __relibc_internal_size: [c_uchar; 4],
29    __relibc_internal_align: c_int,
30}
31/// The `pthread_barrier_t` type provided in [`sys/types.h`](crate::header::sys_types).
32#[repr(C)]
33pub union pthread_barrier_t {
34    __relibc_internal_size: [c_uchar; 24],
35    __relibc_internal_align: c_int,
36}
37/// The `pthread_barrierattr_t` type provided in [`sys/types.h`](crate::header::sys_types).
38#[repr(C)]
39pub union pthread_barrierattr_t {
40    __relibc_internal_size: [c_uchar; 4],
41    __relibc_internal_align: c_int,
42}
43/// The `pthread_mutex_t` type provided in [`sys/types.h`](crate::header::sys_types).
44#[repr(C)]
45pub union pthread_mutex_t {
46    __relibc_internal_size: [c_uchar; 12],
47    __relibc_internal_align: c_int,
48}
49/// The `pthread_mutexattr_t` type provided in [`sys/types.h`](crate::header::sys_types).
50#[repr(C)]
51pub union pthread_mutexattr_t {
52    __relibc_internal_size: [c_uchar; 20],
53    __relibc_internal_align: c_int,
54}
55/// The `pthread_cond_t` type provided in [`sys/types.h`](crate::header::sys_types).
56#[repr(C)]
57pub union pthread_cond_t {
58    __relibc_internal_size: [c_uchar; 8],
59    __relibc_internal_align: c_int,
60}
61/// The `pthread_condattr_t` type provided in [`sys/types.h`](crate::header::sys_types).
62#[repr(C)]
63pub union pthread_condattr_t {
64    __relibc_internal_size: [c_uchar; 8],
65    __relibc_internal_align: c_int,
66}
67/// The `pthread_spinlock_t` type provided in [`sys/types.h`](crate::header::sys_types).
68#[repr(C)]
69pub union pthread_spinlock_t {
70    __relibc_internal_size: [c_uchar; 4],
71    __relibc_internal_align: c_int,
72}
73/// The `pthread_once_t` type provided in [`sys/types.h`](crate::header::sys_types).
74#[repr(C)]
75pub union pthread_once_t {
76    __relibc_internal_size: [c_uchar; 4],
77    __relibc_internal_align: c_int,
78}
79
80macro_rules! assert_equal_size(
81    ($export:ident, $wrapped:ident) => {
82        const _: () = unsafe {
83            type Wrapped = crate::header::pthread::$wrapped;
84
85            // Fail at compile-time if sizes differ.
86
87            // TODO: Is this UB?
88            let export = $export { __relibc_internal_align: 0 };
89            let _: Wrapped = core::mem::transmute(export.__relibc_internal_size);
90
91            // Fail at compile-time if alignments differ.
92            let a = [0_u8; core::mem::align_of::<$export>()];
93            #[allow(clippy::useless_transmute)]
94            let b: [u8; core::mem::align_of::<Wrapped>()] = core::mem::transmute(a);
95        };
96        // TODO: Turn into a macro?
97        #[cfg(all(target_os = "redox", feature = "check_against_libc_crate"))]
98        const _: () = unsafe {
99            use ::__libc_only_for_layout_checks as libc;
100
101            let export = $export { __relibc_internal_align: 0 };
102            let _: libc::$export = core::mem::transmute(export.__relibc_internal_size);
103
104            let a = [0_u8; core::mem::align_of::<$export>()];
105            let b: [u8; core::mem::align_of::<libc::$export>()] = core::mem::transmute(a);
106
107        };
108    }
109);
110assert_equal_size!(pthread_attr_t, RlctAttr);
111assert_equal_size!(pthread_rwlock_t, RlctRwlock);
112assert_equal_size!(pthread_rwlockattr_t, RlctRwlockAttr);
113assert_equal_size!(pthread_barrier_t, RlctBarrier);
114assert_equal_size!(pthread_barrierattr_t, RlctBarrierAttr);
115assert_equal_size!(pthread_mutex_t, RlctMutex);
116assert_equal_size!(pthread_mutexattr_t, RlctMutexAttr);
117assert_equal_size!(pthread_cond_t, RlctCond);
118assert_equal_size!(pthread_condattr_t, RlctCondAttr);
119assert_equal_size!(pthread_spinlock_t, RlctSpinlock);
120assert_equal_size!(pthread_once_t, RlctOnce);
121
122/// The `pthread_key_t` type provided in [`sys/types.h`](crate::header::sys_types).
123pub type pthread_key_t = c_ulong;