Skip to content

Commit ce5dc81

Browse files
committed
apple: Move pthread API to the new module
This sets up the basic structure but does not yet move all `pthread_*` API.
1 parent dc66e54 commit ce5dc81

File tree

21 files changed

+438
-290
lines changed

21 files changed

+438
-290
lines changed

src/new/apple/libpthread/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Source from libpthread <https://github.com/apple-oss-distributions/libpthread/tree/main>
2+
3+
/// Directory: `pthread/`
4+
///
5+
/// Note that this module has a trailing underscore to avoid conflicting with its child `pthread`
6+
/// module.
7+
///
8+
/// <https://github.com/apple-oss-distributions/libpthread/tree/main/include/pthread>
9+
pub(crate) mod pthread_ {
10+
pub(crate) mod introspection;
11+
pub(crate) mod pthread;
12+
pub(crate) mod pthread_impl;
13+
pub(crate) mod pthread_spis;
14+
pub(crate) mod qos;
15+
pub(crate) mod sched;
16+
pub(crate) mod spawn;
17+
pub(crate) mod stack_np;
18+
}
19+
20+
pub(crate) mod sys;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Header: `pthread/introspection.h`
2+
//!
3+
//! <https://github.com/apple-oss-distributions/libpthread/blob/main/include/pthread/introspection.h>
4+
5+
use crate::prelude::*;
6+
pub use crate::pthread_::pthread::*;
7+
8+
c_enum! {
9+
#[repr(c_uint)]
10+
pub enum #anon {
11+
PTHREAD_INTROSPECTION_THREAD_CREATE = 1,
12+
PTHREAD_INTROSPECTION_THREAD_START,
13+
PTHREAD_INTROSPECTION_THREAD_TERMINATE,
14+
PTHREAD_INTROSPECTION_THREAD_DESTROY,
15+
}
16+
}
17+
18+
pub type pthread_introspection_hook_t =
19+
extern "C" fn(event: c_uint, thread: pthread_t, addr: *mut c_void, size: size_t);
20+
21+
extern "C" {
22+
// Available from Big Sur
23+
pub fn pthread_introspection_hook_install(
24+
hook: pthread_introspection_hook_t,
25+
) -> pthread_introspection_hook_t;
26+
pub fn pthread_introspection_setspecific_np(
27+
thread: pthread_t,
28+
key: pthread_key_t,
29+
value: *const c_void,
30+
) -> c_int;
31+
32+
pub fn pthread_introspection_getspecific_np(
33+
thread: pthread_t,
34+
key: pthread_key_t,
35+
) -> *mut c_void;
36+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! Header: `pthread.h` or `pthread/pthread.h`
2+
//!
3+
//! <https://github.com/apple-oss-distributions/libpthread/blob/main/include/pthread/pthread.h>
4+
5+
use crate::prelude::*;
6+
pub use crate::pthread_::qos::*;
7+
pub use crate::pthread_::sched::*;
8+
// No need to import from the `_pthread_attr_t` and similar modules since `_pthread_types` has
9+
// everything we need.
10+
pub use crate::sys::_pthread::_pthread_types::*;
11+
12+
pub const PTHREAD_CREATE_JOINABLE: c_int = 1;
13+
pub const PTHREAD_CREATE_DETACHED: c_int = 2;
14+
15+
pub const PTHREAD_INHERIT_SCHED: c_int = 1;
16+
pub const PTHREAD_EXPLICIT_SCHED: c_int = 2;
17+
18+
pub const PTHREAD_CANCEL_ENABLE: c_int = 0x01;
19+
pub const PTHREAD_CANCEL_DISABLE: c_int = 0x00;
20+
pub const PTHREAD_CANCEL_DEFERRED: c_int = 0x02;
21+
pub const PTHREAD_CANCEL_ASYNCHRONOUS: c_int = 0x00;
22+
23+
pub const PTHREAD_CANCELED: *mut c_void = 1 as *mut c_void;
24+
25+
pub const PTHREAD_SCOPE_SYSTEM: c_int = 1;
26+
pub const PTHREAD_SCOPE_PROCESS: c_int = 2;
27+
28+
pub const PTHREAD_PROCESS_SHARED: c_int = 1;
29+
pub const PTHREAD_PROCESS_PRIVATE: c_int = 2;
30+
31+
pub const PTHREAD_PRIO_NONE: c_int = 0;
32+
pub const PTHREAD_PRIO_INHERIT: c_int = 1;
33+
pub const PTHREAD_PRIO_PROTECT: c_int = 2;
34+
35+
pub const PTHREAD_MUTEX_NORMAL: c_int = 0;
36+
pub const PTHREAD_MUTEX_ERRORCHECK: c_int = 1;
37+
pub const PTHREAD_MUTEX_RECURSIVE: c_int = 2;
38+
pub const PTHREAD_MUTEX_DEFAULT: c_int = PTHREAD_MUTEX_NORMAL;
39+
40+
pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
41+
__sig: _PTHREAD_RWLOCK_SIG_init,
42+
__opaque: [0; __PTHREAD_RWLOCK_SIZE__],
43+
};
44+
45+
pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
46+
__sig: _PTHREAD_MUTEX_SIG_init,
47+
__opaque: [0; __PTHREAD_MUTEX_SIZE__],
48+
};
49+
50+
pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
51+
__sig: _PTHREAD_COND_SIG_init,
52+
__opaque: [0; __PTHREAD_COND_SIZE__],
53+
};
54+
55+
pub const PTHREAD_ONCE_INIT: crate::pthread_once_t = crate::pthread_once_t {
56+
__sig: _PTHREAD_ONCE_SIG_INIT,
57+
__opaque: [0; __PTHREAD_ONCE_SIZE__],
58+
};
59+
60+
pub use crate::new::common::posix::pthread::{
61+
pthread_attr_getinheritsched,
62+
pthread_attr_getschedparam,
63+
pthread_attr_getschedpolicy,
64+
pthread_attr_setinheritsched,
65+
pthread_attr_setschedparam,
66+
pthread_attr_setschedpolicy,
67+
pthread_condattr_getpshared,
68+
pthread_condattr_setpshared,
69+
pthread_getschedparam,
70+
pthread_mutexattr_getpshared,
71+
pthread_mutexattr_setpshared,
72+
pthread_once,
73+
pthread_rwlockattr_getpshared,
74+
pthread_rwlockattr_setpshared,
75+
pthread_setschedparam,
76+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use crate::prelude::*;
2+
3+
// FIXME(apple): these should all be `pub(crate)`
4+
pub const _PTHREAD_MUTEX_SIG_init: c_long = 0x32AAABA7;
5+
6+
pub const _PTHREAD_COND_SIG_init: c_long = 0x3CB0B1BB;
7+
pub(crate) const _PTHREAD_ONCE_SIG_INIT: c_long = 0x30B1BCBA;
8+
pub const _PTHREAD_RWLOCK_SIG_init: c_long = 0x2DA8B3B4;
9+
10+
pub const SCHED_OTHER: c_int = 1;
11+
pub const SCHED_FIFO: c_int = 4;
12+
pub const SCHED_RR: c_int = 2;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Header: `pthread/pthread_spis.h`
2+
//!
3+
//! <https://github.com/apple-oss-distributions/libpthread/blob/main/include/pthread/pthread_spis.h>
4+
5+
use crate::prelude::*;
6+
7+
extern "C" {
8+
pub fn pthread_create_from_mach_thread(
9+
thread: *mut crate::pthread_t,
10+
attr: *const crate::pthread_attr_t,
11+
f: extern "C" fn(*mut c_void) -> *mut c_void,
12+
value: *mut c_void,
13+
) -> c_int;
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! Header: `pthread/qos.h`
2+
//!
3+
//! <https://github.com/apple-oss-distributions/libpthread/blob/main/include/pthread/qos.h>
4+
5+
use crate::prelude::*;
6+
pub use crate::sys::qos::*;
7+
8+
extern "C" {
9+
pub fn pthread_attr_set_qos_class_np(
10+
attr: *mut crate::pthread_attr_t,
11+
class: qos_class_t,
12+
priority: c_int,
13+
) -> c_int;
14+
pub fn pthread_attr_get_qos_class_np(
15+
attr: *mut crate::pthread_attr_t,
16+
class: *mut qos_class_t,
17+
priority: *mut c_int,
18+
) -> c_int;
19+
pub fn pthread_set_qos_class_self_np(class: qos_class_t, priority: c_int) -> c_int;
20+
pub fn pthread_get_qos_class_np(
21+
thread: crate::pthread_t,
22+
class: *mut qos_class_t,
23+
priority: *mut c_int,
24+
) -> c_int;
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub use crate::new::pthread_::pthread_impl::*;
2+
use crate::prelude::*;
3+
4+
s! {
5+
pub struct sched_param {
6+
pub sched_priority: c_int,
7+
__opaque: [c_char; 4],
8+
}
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Header: `pthread/spawn.h`
2+
//!
3+
//! <https://github.com/apple-oss-distributions/libpthread/blob/main/include/pthread/spawn.h>
4+
5+
use crate::prelude::*;
6+
7+
extern "C" {
8+
pub fn posix_spawnattr_set_qos_class_np(
9+
attr: *mut crate::posix_spawnattr_t,
10+
qos_class: crate::qos_class_t,
11+
) -> c_int;
12+
pub fn posix_spawnattr_get_qos_class_np(
13+
attr: *const crate::posix_spawnattr_t,
14+
qos_class: *mut crate::qos_class_t,
15+
) -> c_int;
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! Header: `pthread/stack_np.h`
2+
//!
3+
//! <https://github.com/apple-oss-distributions/libpthread/blob/main/include/pthread/stack_np.h>
4+
5+
use crate::prelude::*;
6+
7+
extern "C" {
8+
pub fn pthread_stack_frame_decode_np(
9+
frame_addr: uintptr_t,
10+
return_addr: *mut uintptr_t,
11+
) -> uintptr_t;
12+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! Header: `sys/_pthread/_pthread_types.h`
2+
//!
3+
//! <https://github.com/apple-oss-distributions/libpthread/blob/main/include/sys/_pthread/_pthread_types.h>
4+
//!
5+
//! Note that the actual header defines `_opaque_pthread_*` structs which are typedefed to
6+
//! `__darwin_pthread*` structs, and typedefed again in separate `_pthread_*.h` files to their final
7+
//! `pthread_` name. This isn't useful for us so we simplify a bit and just define everything here.
8+
9+
use crate::prelude::*;
10+
11+
cfg_if! {
12+
if #[cfg(target_pointer_width = "64")] {
13+
pub const __PTHREAD_SIZE__: usize = 8176;
14+
pub const __PTHREAD_ATTR_SIZE__: usize = 56;
15+
pub const __PTHREAD_MUTEXATTR_SIZE__: usize = 8;
16+
pub const __PTHREAD_MUTEX_SIZE__: usize = 56;
17+
pub const __PTHREAD_CONDATTR_SIZE__: usize = 8;
18+
pub const __PTHREAD_COND_SIZE__: usize = 40;
19+
pub const __PTHREAD_ONCE_SIZE__: usize = 8;
20+
pub const __PTHREAD_RWLOCK_SIZE__: usize = 192;
21+
pub const __PTHREAD_RWLOCKATTR_SIZE__: usize = 16;
22+
} else {
23+
pub const __PTHREAD_SIZE__: usize = 4088;
24+
pub const __PTHREAD_ATTR_SIZE__: usize = 36;
25+
pub const __PTHREAD_MUTEXATTR_SIZE__: usize = 8;
26+
pub const __PTHREAD_MUTEX_SIZE__: usize = 40;
27+
pub const __PTHREAD_CONDATTR_SIZE__: usize = 4;
28+
pub const __PTHREAD_COND_SIZE__: usize = 24;
29+
pub const __PTHREAD_ONCE_SIZE__: usize = 4;
30+
pub const __PTHREAD_RWLOCK_SIZE__: usize = 124;
31+
pub const __PTHREAD_RWLOCKATTR_SIZE__: usize = 12;
32+
}
33+
}
34+
35+
s! {
36+
pub struct pthread_attr_t {
37+
pub(crate) __sig: c_long,
38+
pub(crate) __opaque: [c_char; __PTHREAD_ATTR_SIZE__],
39+
}
40+
41+
pub struct pthread_cond_t {
42+
pub(crate) __sig: c_long,
43+
pub(crate) __opaque: [u8; __PTHREAD_COND_SIZE__],
44+
}
45+
46+
pub struct pthread_condattr_t {
47+
pub(crate) __sig: c_long,
48+
pub(crate) __opaque: [u8; __PTHREAD_CONDATTR_SIZE__],
49+
}
50+
51+
pub struct pthread_mutex_t {
52+
pub(crate) __sig: c_long,
53+
pub(crate) __opaque: [u8; __PTHREAD_MUTEX_SIZE__],
54+
}
55+
56+
pub struct pthread_mutexattr_t {
57+
pub(crate) __sig: c_long,
58+
pub(crate) __opaque: [u8; __PTHREAD_MUTEXATTR_SIZE__],
59+
}
60+
61+
pub struct pthread_once_t {
62+
pub(crate) __sig: c_long,
63+
pub(crate) __opaque: [c_char; __PTHREAD_ONCE_SIZE__],
64+
}
65+
66+
pub struct pthread_rwlock_t {
67+
pub(crate) __sig: c_long,
68+
pub(crate) __opaque: [u8; __PTHREAD_RWLOCK_SIZE__],
69+
}
70+
71+
pub struct pthread_rwlockattr_t {
72+
pub(crate) __sig: c_long,
73+
pub(crate) __opaque: [u8; __PTHREAD_RWLOCKATTR_SIZE__],
74+
}
75+
}
76+
77+
pub type pthread_key_t = c_ulong;
78+
79+
pub use crate::pthread_t;

0 commit comments

Comments
 (0)