Skip to content

Commit b96e1cb

Browse files
committed
linux: Move Linux-specific pthread bits 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 05d2e60 commit b96e1cb

File tree

13 files changed

+404
-118
lines changed

13 files changed

+404
-118
lines changed

src/new/common/linux_like/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
//! API that primarily comes from Linux but is also used other platforms (e.g. Android).
2+
3+
#[cfg(target_os = "linux")]
4+
pub(crate) mod pthread;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::prelude::*;
2+
3+
extern "C" {
4+
#[cfg(target_os = "linux")]
5+
pub fn pthread_getaffinity_np(
6+
thread: crate::pthread_t,
7+
cpusetsize: size_t,
8+
cpuset: *mut crate::cpu_set_t,
9+
) -> c_int;
10+
11+
#[cfg(target_os = "linux")]
12+
pub fn pthread_getname_np(thread: crate::pthread_t, name: *mut c_char, len: size_t) -> c_int;
13+
14+
#[cfg(target_os = "linux")]
15+
pub fn pthread_setaffinity_np(
16+
thread: crate::pthread_t,
17+
cpusetsize: size_t,
18+
cpuset: *const crate::cpu_set_t,
19+
) -> c_int;
20+
21+
#[cfg(target_os = "linux")]
22+
pub fn pthread_setname_np(thread: crate::pthread_t, name: *const c_char) -> c_int;
23+
}

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: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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_getcpuclockid(thread: crate::pthread_t, clk_id: *mut crate::clockid_t) -> c_int;
107+
108+
#[cfg(target_os = "linux")]
109+
pub fn pthread_getschedparam(
110+
native: crate::pthread_t,
111+
policy: *mut c_int,
112+
param: *mut crate::sched_param,
113+
) -> c_int;
114+
115+
// FIXME(reorg): In recent POSIX versions, this is a signal.h function and not required
116+
// in pthread.
117+
#[cfg(target_os = "linux")]
118+
pub fn pthread_kill(thread: crate::pthread_t, sig: c_int) -> c_int;
119+
120+
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
121+
pub fn pthread_mutex_consistent(mutex: *mut crate::pthread_mutex_t) -> c_int;
122+
123+
#[cfg(target_os = "linux")]
124+
#[cfg_attr(gnu_time_bits64, link_name = "__pthread_mutex_timedlock64")]
125+
pub fn pthread_mutex_timedlock(
126+
lock: *mut crate::pthread_mutex_t,
127+
abstime: *const crate::timespec,
128+
) -> c_int;
129+
130+
#[cfg(target_os = "linux")]
131+
pub fn pthread_mutexattr_getprotocol(
132+
attr: *const crate::pthread_mutexattr_t,
133+
protocol: *mut c_int,
134+
) -> c_int;
135+
136+
#[cfg(target_os = "linux")]
137+
pub fn pthread_mutexattr_getpshared(
138+
attr: *const crate::pthread_mutexattr_t,
139+
pshared: *mut c_int,
140+
) -> c_int;
141+
142+
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
143+
pub fn pthread_mutexattr_getrobust(
144+
attr: *const crate::pthread_mutexattr_t,
145+
robustness: *mut c_int,
146+
) -> c_int;
147+
148+
#[cfg(target_os = "linux")]
149+
pub fn pthread_mutexattr_setprotocol(
150+
attr: *mut crate::pthread_mutexattr_t,
151+
protocol: c_int,
152+
) -> c_int;
153+
154+
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
155+
pub fn pthread_mutexattr_setrobust(
156+
attr: *mut crate::pthread_mutexattr_t,
157+
robustness: c_int,
158+
) -> c_int;
159+
160+
#[cfg(target_os = "linux")]
161+
pub fn pthread_once(control: *mut crate::pthread_once_t, routine: extern "C" fn()) -> c_int;
162+
163+
#[cfg(target_os = "linux")]
164+
pub fn pthread_setschedparam(
165+
native: crate::pthread_t,
166+
policy: c_int,
167+
param: *const crate::sched_param,
168+
) -> c_int;
169+
170+
#[cfg(target_os = "linux")]
171+
pub fn pthread_setschedprio(native: crate::pthread_t, priority: c_int) -> c_int;
172+
173+
// FIXME(reorg): In recent POSIX versions, this is a signal.h function and not required
174+
// in pthread.
175+
#[cfg(target_os = "linux")]
176+
pub fn pthread_sigmask(
177+
how: c_int,
178+
set: *const crate::sigset_t,
179+
oldset: *mut crate::sigset_t,
180+
) -> c_int;
181+
182+
#[cfg(target_os = "linux")]
183+
pub fn pthread_spin_destroy(lock: *mut crate::pthread_spinlock_t) -> c_int;
184+
185+
#[cfg(target_os = "linux")]
186+
pub fn pthread_spin_init(lock: *mut crate::pthread_spinlock_t, pshared: c_int) -> c_int;
187+
188+
#[cfg(target_os = "linux")]
189+
pub fn pthread_spin_lock(lock: *mut crate::pthread_spinlock_t) -> c_int;
190+
191+
#[cfg(target_os = "linux")]
192+
pub fn pthread_spin_trylock(lock: *mut crate::pthread_spinlock_t) -> c_int;
193+
194+
#[cfg(target_os = "linux")]
195+
pub fn pthread_spin_unlock(lock: *mut crate::pthread_spinlock_t) -> c_int;
196+
}

src/new/glibc/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ mod posix {
1717
///
1818
/// <https://github.com/bminor/glibc/tree/master/sysdeps>
1919
mod sysdeps {
20+
// FIXME(pthread): eventually all platforms should use this module
21+
#[cfg(target_os = "linux")]
22+
pub(crate) mod nptl;
2023
pub(crate) mod unix;
2124
}
2225

2326
pub(crate) use posix::*;
27+
// FIXME(pthread): eventually all platforms should use this module
28+
#[cfg(target_os = "linux")]
29+
pub(crate) use sysdeps::nptl::*;
2430
#[cfg(target_os = "linux")]
2531
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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::linux_like::pthread::{
6+
pthread_getaffinity_np,
7+
pthread_getname_np,
8+
pthread_setaffinity_np,
9+
pthread_setname_np,
10+
};
11+
pub use crate::new::common::posix::pthread::{
12+
pthread_atfork,
13+
pthread_attr_getguardsize,
14+
pthread_attr_getinheritsched,
15+
pthread_attr_getschedparam,
16+
pthread_attr_getschedpolicy,
17+
pthread_attr_setguardsize,
18+
pthread_attr_setinheritsched,
19+
pthread_attr_setschedparam,
20+
pthread_attr_setschedpolicy,
21+
pthread_barrier_destroy,
22+
pthread_barrier_init,
23+
pthread_barrier_wait,
24+
pthread_barrierattr_destroy,
25+
pthread_barrierattr_getpshared,
26+
pthread_barrierattr_init,
27+
pthread_barrierattr_setpshared,
28+
pthread_cancel,
29+
pthread_condattr_getpshared,
30+
pthread_create,
31+
pthread_getcpuclockid,
32+
pthread_getschedparam,
33+
pthread_kill,
34+
pthread_mutex_consistent,
35+
pthread_mutex_timedlock,
36+
pthread_mutexattr_getprotocol,
37+
pthread_mutexattr_getpshared,
38+
pthread_mutexattr_getrobust,
39+
pthread_mutexattr_setprotocol,
40+
pthread_mutexattr_setrobust,
41+
pthread_once,
42+
pthread_setschedparam,
43+
pthread_setschedprio,
44+
pthread_sigmask,
45+
pthread_spin_destroy,
46+
pthread_spin_init,
47+
pthread_spin_lock,
48+
pthread_spin_trylock,
49+
pthread_spin_unlock,
50+
};

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

0 commit comments

Comments
 (0)