Skip to content

Commit ff2025d

Browse files
committed
linux: Switch pthread to the new module
Move `pthread_*` API in `linux_like/linux.rs` to a module in `new` and reexport via `glibc` and `musl`. Eventually other platforms will be ported as well.
1 parent a5e5c0d commit ff2025d

File tree

11 files changed

+383
-118
lines changed

11 files changed

+383
-118
lines changed

src/new/common/posix/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
//! POSIX APIs that are used by a number of platforms
2+
//!
3+
//! These can be found at: <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/contents.html>.
24
5+
// FIXME(pthread): eventually all platforms should use this module
6+
#[cfg(target_os = "linux")]
7+
pub(crate) mod pthread;
38
pub(crate) mod unistd;

src/new/common/posix/pthread.rs

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
//! Header: `pthread.h`
2+
//!
3+
//! <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html>
4+
5+
use crate::prelude::*;
6+
7+
extern "C" {
8+
#[cfg(target_os = "linux")]
9+
pub fn pthread_atfork(
10+
prepare: Option<unsafe extern "C" fn()>,
11+
parent: Option<unsafe extern "C" fn()>,
12+
child: Option<unsafe extern "C" fn()>,
13+
) -> c_int;
14+
15+
#[cfg(target_os = "linux")]
16+
pub fn pthread_attr_getguardsize(
17+
attr: *const crate::pthread_attr_t,
18+
guardsize: *mut size_t,
19+
) -> c_int;
20+
21+
#[cfg(target_os = "linux")]
22+
pub fn pthread_attr_getinheritsched(
23+
attr: *const crate::pthread_attr_t,
24+
inheritsched: *mut c_int,
25+
) -> c_int;
26+
27+
#[cfg(target_os = "linux")]
28+
pub fn pthread_attr_getschedparam(
29+
attr: *const crate::pthread_attr_t,
30+
param: *mut crate::sched_param,
31+
) -> c_int;
32+
33+
#[cfg(target_os = "linux")]
34+
pub fn pthread_attr_getschedpolicy(
35+
attr: *const crate::pthread_attr_t,
36+
policy: *mut c_int,
37+
) -> c_int;
38+
39+
#[cfg(target_os = "linux")]
40+
pub fn pthread_attr_setguardsize(attr: *mut crate::pthread_attr_t, guardsize: size_t) -> c_int;
41+
42+
#[cfg(target_os = "linux")]
43+
pub fn pthread_attr_setinheritsched(
44+
attr: *mut crate::pthread_attr_t,
45+
inheritsched: c_int,
46+
) -> c_int;
47+
48+
#[cfg(target_os = "linux")]
49+
pub fn pthread_attr_setschedparam(
50+
attr: *mut crate::pthread_attr_t,
51+
param: *const crate::sched_param,
52+
) -> c_int;
53+
54+
#[cfg(target_os = "linux")]
55+
pub fn pthread_attr_setschedpolicy(attr: *mut crate::pthread_attr_t, policy: c_int) -> c_int;
56+
57+
#[cfg(target_os = "linux")]
58+
pub fn pthread_barrier_destroy(barrier: *mut crate::pthread_barrier_t) -> c_int;
59+
60+
#[cfg(target_os = "linux")]
61+
pub fn pthread_barrier_init(
62+
barrier: *mut crate::pthread_barrier_t,
63+
attr: *const crate::pthread_barrierattr_t,
64+
count: c_uint,
65+
) -> c_int;
66+
67+
#[cfg(target_os = "linux")]
68+
pub fn pthread_barrier_wait(barrier: *mut crate::pthread_barrier_t) -> c_int;
69+
70+
#[cfg(target_os = "linux")]
71+
pub fn pthread_barrierattr_destroy(attr: *mut crate::pthread_barrierattr_t) -> c_int;
72+
73+
#[cfg(target_os = "linux")]
74+
pub fn pthread_barrierattr_getpshared(
75+
attr: *const crate::pthread_barrierattr_t,
76+
shared: *mut c_int,
77+
) -> c_int;
78+
79+
#[cfg(target_os = "linux")]
80+
pub fn pthread_barrierattr_init(attr: *mut crate::pthread_barrierattr_t) -> c_int;
81+
82+
#[cfg(target_os = "linux")]
83+
pub fn pthread_barrierattr_setpshared(
84+
attr: *mut crate::pthread_barrierattr_t,
85+
shared: c_int,
86+
) -> c_int;
87+
88+
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
89+
pub fn pthread_cancel(thread: crate::pthread_t) -> c_int;
90+
91+
#[cfg(target_os = "linux")]
92+
pub fn pthread_condattr_getpshared(
93+
attr: *const crate::pthread_condattr_t,
94+
pshared: *mut c_int,
95+
) -> c_int;
96+
97+
#[cfg(target_os = "linux")]
98+
pub fn pthread_create(
99+
native: *mut crate::pthread_t,
100+
attr: *const crate::pthread_attr_t,
101+
f: extern "C" fn(*mut c_void) -> *mut c_void,
102+
value: *mut c_void,
103+
) -> c_int;
104+
105+
#[cfg(target_os = "linux")]
106+
pub fn pthread_getaffinity_np(
107+
thread: crate::pthread_t,
108+
cpusetsize: size_t,
109+
cpuset: *mut crate::cpu_set_t,
110+
) -> c_int;
111+
112+
#[cfg(target_os = "linux")]
113+
pub fn pthread_getcpuclockid(thread: crate::pthread_t, clk_id: *mut crate::clockid_t) -> c_int;
114+
115+
#[cfg(target_os = "linux")]
116+
pub fn pthread_getname_np(thread: crate::pthread_t, name: *mut c_char, len: size_t) -> c_int;
117+
118+
#[cfg(target_os = "linux")]
119+
pub fn pthread_getschedparam(
120+
native: crate::pthread_t,
121+
policy: *mut c_int,
122+
param: *mut crate::sched_param,
123+
) -> c_int;
124+
125+
#[cfg(target_os = "linux")]
126+
pub fn pthread_kill(thread: crate::pthread_t, sig: c_int) -> c_int;
127+
128+
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
129+
pub fn pthread_mutex_consistent(mutex: *mut crate::pthread_mutex_t) -> c_int;
130+
131+
#[cfg(target_os = "linux")]
132+
#[cfg_attr(gnu_time_bits64, link_name = "__pthread_mutex_timedlock64")]
133+
pub fn pthread_mutex_timedlock(
134+
lock: *mut crate::pthread_mutex_t,
135+
abstime: *const crate::timespec,
136+
) -> c_int;
137+
138+
#[cfg(target_os = "linux")]
139+
pub fn pthread_mutexattr_getprotocol(
140+
attr: *const crate::pthread_mutexattr_t,
141+
protocol: *mut c_int,
142+
) -> c_int;
143+
144+
#[cfg(target_os = "linux")]
145+
pub fn pthread_mutexattr_getpshared(
146+
attr: *const crate::pthread_mutexattr_t,
147+
pshared: *mut c_int,
148+
) -> c_int;
149+
150+
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
151+
pub fn pthread_mutexattr_getrobust(
152+
attr: *const crate::pthread_mutexattr_t,
153+
robustness: *mut c_int,
154+
) -> c_int;
155+
156+
#[cfg(target_os = "linux")]
157+
pub fn pthread_mutexattr_setprotocol(
158+
attr: *mut crate::pthread_mutexattr_t,
159+
protocol: c_int,
160+
) -> c_int;
161+
162+
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
163+
pub fn pthread_mutexattr_setrobust(
164+
attr: *mut crate::pthread_mutexattr_t,
165+
robustness: c_int,
166+
) -> c_int;
167+
168+
#[cfg(target_os = "linux")]
169+
pub fn pthread_once(control: *mut crate::pthread_once_t, routine: extern "C" fn()) -> c_int;
170+
171+
#[cfg(target_os = "linux")]
172+
pub fn pthread_setaffinity_np(
173+
thread: crate::pthread_t,
174+
cpusetsize: size_t,
175+
cpuset: *const crate::cpu_set_t,
176+
) -> c_int;
177+
178+
#[cfg(target_os = "linux")]
179+
pub fn pthread_setname_np(thread: crate::pthread_t, name: *const c_char) -> c_int;
180+
181+
#[cfg(target_os = "linux")]
182+
pub fn pthread_setschedparam(
183+
native: crate::pthread_t,
184+
policy: c_int,
185+
param: *const crate::sched_param,
186+
) -> c_int;
187+
188+
#[cfg(target_os = "linux")]
189+
pub fn pthread_setschedprio(native: crate::pthread_t, priority: c_int) -> c_int;
190+
191+
#[cfg(target_os = "linux")]
192+
pub fn pthread_sigmask(
193+
how: c_int,
194+
set: *const crate::sigset_t,
195+
oldset: *mut crate::sigset_t,
196+
) -> c_int;
197+
198+
#[cfg(target_os = "linux")]
199+
pub fn pthread_spin_destroy(lock: *mut crate::pthread_spinlock_t) -> c_int;
200+
201+
#[cfg(target_os = "linux")]
202+
pub fn pthread_spin_init(lock: *mut crate::pthread_spinlock_t, pshared: c_int) -> c_int;
203+
204+
#[cfg(target_os = "linux")]
205+
pub fn pthread_spin_lock(lock: *mut crate::pthread_spinlock_t) -> c_int;
206+
207+
#[cfg(target_os = "linux")]
208+
pub fn pthread_spin_trylock(lock: *mut crate::pthread_spinlock_t) -> c_int;
209+
210+
#[cfg(target_os = "linux")]
211+
pub fn pthread_spin_unlock(lock: *mut crate::pthread_spinlock_t) -> c_int;
212+
}

src/new/glibc/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ mod posix {
1717
///
1818
/// <https://github.com/bminor/glibc/tree/master/sysdeps>
1919
mod sysdeps {
20+
pub(crate) mod nptl;
2021
pub(crate) mod unix;
2122
}
2223

2324
pub(crate) use posix::*;
25+
pub(crate) use sysdeps::nptl::*;
2426
#[cfg(target_os = "linux")]
2527
pub(crate) use sysdeps::unix::linux::*;

src/new/glibc/sysdeps/nptl/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Source directory: `sysdeps/nptl/`o
2+
//!
3+
//! Native POSIX threading library.
4+
//!
5+
//! <https://github.com/bminor/glibc/tree/master/sysdeps/nptl>
6+
7+
pub(crate) mod pthread;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Source header: `sysdeps/nptl/pthread.h`
2+
//!
3+
//! <https://github.com/bminor/glibc/blob/master/sysdeps/nptl/pthread.h>
4+
5+
pub use crate::new::common::posix::pthread::{
6+
pthread_atfork,
7+
pthread_attr_getguardsize,
8+
pthread_attr_getinheritsched,
9+
pthread_attr_getschedparam,
10+
pthread_attr_getschedpolicy,
11+
pthread_attr_setguardsize,
12+
pthread_attr_setinheritsched,
13+
pthread_attr_setschedparam,
14+
pthread_attr_setschedpolicy,
15+
pthread_barrier_destroy,
16+
pthread_barrier_init,
17+
pthread_barrier_wait,
18+
pthread_barrierattr_destroy,
19+
pthread_barrierattr_getpshared,
20+
pthread_barrierattr_init,
21+
pthread_barrierattr_setpshared,
22+
pthread_cancel,
23+
pthread_condattr_getpshared,
24+
pthread_create,
25+
pthread_getaffinity_np,
26+
pthread_getcpuclockid,
27+
pthread_getname_np,
28+
pthread_getschedparam,
29+
pthread_kill,
30+
pthread_mutex_consistent,
31+
pthread_mutex_timedlock,
32+
pthread_mutexattr_getprotocol,
33+
pthread_mutexattr_getpshared,
34+
pthread_mutexattr_getrobust,
35+
pthread_mutexattr_setprotocol,
36+
pthread_mutexattr_setrobust,
37+
pthread_once,
38+
pthread_setaffinity_np,
39+
pthread_setname_np,
40+
pthread_setschedparam,
41+
pthread_setschedprio,
42+
pthread_sigmask,
43+
pthread_spin_destroy,
44+
pthread_spin_init,
45+
pthread_spin_lock,
46+
pthread_spin_trylock,
47+
pthread_spin_unlock,
48+
};

src/new/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,12 @@ cfg_if! {
203203
}
204204
}
205205

206-
#[cfg(target_family = "unix")]
207-
pub use unistd::*;
206+
// Per-family headers we export
207+
cfg_if! {
208+
if #[cfg(target_family = "unix")] {
209+
// FIXME(pthread): eventually all platforms should use this module
210+
#[cfg(target_os = "linux")]
211+
pub use pthread::*;
212+
pub use unistd::*;
213+
}
214+
}

src/new/musl/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub(crate) mod bits {
2020
}
2121
}
2222

23+
pub(crate) mod pthread;
24+
2325
/// Directory: `sys/`
2426
///
2527
/// <https://github.com/kraj/musl/tree/kraj/master/include/sys>

src/new/musl/pthread.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! Header: `pthread.h`
2+
//!
3+
//! <https://github.com/kraj/musl/blob/kraj/master/include/pthread.h>
4+
5+
pub use crate::new::common::posix::pthread::{
6+
pthread_atfork,
7+
pthread_attr_getguardsize,
8+
pthread_attr_getinheritsched,
9+
pthread_attr_getschedparam,
10+
pthread_attr_getschedpolicy,
11+
pthread_attr_setguardsize,
12+
pthread_attr_setinheritsched,
13+
pthread_attr_setschedparam,
14+
pthread_attr_setschedpolicy,
15+
pthread_barrier_destroy,
16+
pthread_barrier_init,
17+
pthread_barrier_wait,
18+
pthread_barrierattr_destroy,
19+
pthread_barrierattr_getpshared,
20+
pthread_barrierattr_init,
21+
pthread_barrierattr_setpshared,
22+
pthread_condattr_getpshared,
23+
pthread_create,
24+
pthread_getaffinity_np,
25+
pthread_getcpuclockid,
26+
pthread_getname_np,
27+
pthread_getschedparam,
28+
pthread_kill,
29+
pthread_mutex_timedlock,
30+
pthread_mutexattr_getprotocol,
31+
pthread_mutexattr_getpshared,
32+
pthread_mutexattr_setprotocol,
33+
pthread_once,
34+
pthread_setaffinity_np,
35+
pthread_setname_np,
36+
pthread_setschedparam,
37+
pthread_setschedprio,
38+
pthread_sigmask,
39+
pthread_spin_destroy,
40+
pthread_spin_init,
41+
pthread_spin_lock,
42+
pthread_spin_trylock,
43+
pthread_spin_unlock,
44+
};
45+
#[cfg(not(target_env = "ohos"))]
46+
pub use crate::new::common::posix::pthread::{
47+
pthread_cancel,
48+
pthread_mutex_consistent,
49+
pthread_mutexattr_getrobust,
50+
pthread_mutexattr_setrobust,
51+
};

src/new/uclibc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
//! * About: <https://uclibc.org/>
44
//! * Headers: <https://github.com/kraj/uClibc> (mirror)
55
6+
pub(crate) mod pthread;
67
pub(crate) mod unistd;

0 commit comments

Comments
 (0)