relibc/header/pthread/
attr.rs1use core::{ptr, sync::atomic::Ordering};
2
3use super::*;
4
5use crate::{
6 header::bits_pthread::{pthread_attr_t, pthread_t},
7 pthread::{Pthread, PthreadFlags},
8};
9
10impl Default for RlctAttr {
11 fn default() -> Self {
12 Self {
13 detachstate: PTHREAD_CREATE_JOINABLE as _,
15 inheritsched: PTHREAD_INHERIT_SCHED as _,
17 schedpolicy: SCHED_RR as _,
20 scope: PTHREAD_SCOPE_SYSTEM as _,
22 guardsize: Sys::getpagesize(),
23 stack: 0,
25 stacksize: 1024 * 1024,
27 param: sched_param {
28 sched_priority: 0,
30 },
31 #[cfg(target_pointer_width = "32")]
32 _pad: [0; 12],
33 }
34 }
35}
36
37#[unsafe(no_mangle)]
39pub unsafe extern "C" fn pthread_attr_destroy(attr: *mut pthread_attr_t) -> c_int {
40 unsafe { ptr::drop_in_place(attr) };
41 0
42}
43
44#[unsafe(no_mangle)]
46pub unsafe extern "C" fn pthread_attr_getdetachstate(
47 attr: *const pthread_attr_t,
48 detachstate: *mut c_int,
49) -> c_int {
50 unsafe { ptr::write(detachstate, (*attr.cast::<RlctAttr>()).detachstate.into()) };
51 0
52}
53
54#[unsafe(no_mangle)]
56pub unsafe extern "C" fn pthread_attr_getguardsize(
57 attr: *const pthread_attr_t,
58 size: *mut size_t,
59) -> c_int {
60 unsafe { ptr::write(size, (*attr.cast::<RlctAttr>()).guardsize) };
61 0
62}
63
64#[unsafe(no_mangle)]
66pub unsafe extern "C" fn pthread_attr_getinheritsched(
67 attr: *const pthread_attr_t,
68 inheritsched: *mut c_int,
69) -> c_int {
70 unsafe { ptr::write(inheritsched, (*attr.cast::<RlctAttr>()).inheritsched.into()) };
71 0
72}
73
74#[unsafe(no_mangle)]
76pub unsafe extern "C" fn pthread_attr_getschedparam(
77 attr: *const pthread_attr_t,
78 param: *mut sched_param,
79) -> c_int {
80 unsafe { param.write((*attr.cast::<RlctAttr>()).param) };
81 0
82}
83
84#[unsafe(no_mangle)]
86pub unsafe extern "C" fn pthread_attr_getschedpolicy(
87 attr: *const pthread_attr_t,
88 policy: *mut c_int,
89) -> c_int {
90 unsafe { ptr::write(policy, (*attr.cast::<RlctAttr>()).schedpolicy.into()) };
91 0
92}
93
94#[unsafe(no_mangle)]
96pub unsafe extern "C" fn pthread_attr_getscope(
97 attr: *const pthread_attr_t,
98 scope: *mut c_int,
99) -> c_int {
100 unsafe { ptr::write(scope, (*attr.cast::<RlctAttr>()).scope.into()) };
101 0
102}
103
104#[unsafe(no_mangle)]
106pub unsafe extern "C" fn pthread_attr_getstack(
107 attr: *const pthread_attr_t,
108 stackaddr: *mut *mut c_void,
109 stacksize: *mut size_t,
110) -> c_int {
111 unsafe { ptr::write(stackaddr, (*attr.cast::<RlctAttr>()).stack as _) };
112 unsafe { ptr::write(stacksize, (*attr.cast::<RlctAttr>()).stacksize as _) };
113 0
114}
115
116#[unsafe(no_mangle)]
118pub unsafe extern "C" fn pthread_attr_getstacksize(
119 attr: *const pthread_attr_t,
120 stacksize: *mut size_t,
121) -> c_int {
122 unsafe { ptr::write(stacksize, (*attr.cast::<RlctAttr>()).stacksize as _) };
123 0
124}
125
126#[unsafe(no_mangle)]
128pub unsafe extern "C" fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int {
129 unsafe { ptr::write(attr.cast::<RlctAttr>(), RlctAttr::default()) };
130 0
131}
132
133#[unsafe(no_mangle)]
135pub unsafe extern "C" fn pthread_attr_setdetachstate(
136 attr: *mut pthread_attr_t,
137 detachstate: c_int,
138) -> c_int {
139 unsafe {
140 (*attr.cast::<RlctAttr>()).detachstate = detachstate as _;
141 }
142 0
143}
144
145#[unsafe(no_mangle)]
147pub unsafe extern "C" fn pthread_attr_setguardsize(
148 attr: *mut pthread_attr_t,
149 guardsize: c_int,
150) -> c_int {
151 unsafe {
152 (*attr.cast::<RlctAttr>()).guardsize = guardsize as _;
153 }
154 0
155}
156
157#[unsafe(no_mangle)]
159pub unsafe extern "C" fn pthread_attr_setinheritsched(
160 attr: *mut pthread_attr_t,
161 inheritsched: c_int,
162) -> c_int {
163 unsafe {
164 (*attr.cast::<RlctAttr>()).inheritsched = inheritsched as _;
165 }
166 0
167}
168
169#[unsafe(no_mangle)]
171pub unsafe extern "C" fn pthread_attr_setschedparam(
172 attr: *mut pthread_attr_t,
173 param: *const sched_param,
174) -> c_int {
175 unsafe {
176 (*attr.cast::<RlctAttr>()).param = param.read();
177 }
178 0
179}
180
181#[unsafe(no_mangle)]
183pub unsafe extern "C" fn pthread_attr_setschedpolicy(
184 attr: *mut pthread_attr_t,
185 policy: c_int,
186) -> c_int {
187 unsafe {
188 (*attr.cast::<RlctAttr>()).schedpolicy = policy as u8;
189 }
190 0
191}
192
193#[unsafe(no_mangle)]
195pub unsafe extern "C" fn pthread_attr_setscope(attr: *mut pthread_attr_t, scope: c_int) -> c_int {
196 unsafe {
197 (*attr.cast::<RlctAttr>()).scope = scope as u8;
198 }
199 0
200}
201
202#[unsafe(no_mangle)]
204pub unsafe extern "C" fn pthread_attr_setstack(
205 attr: *mut pthread_attr_t,
206 stackaddr: *mut c_void,
207 stacksize: size_t,
208) -> c_int {
209 unsafe {
210 (*attr.cast::<RlctAttr>()).stack = stackaddr as usize;
211 }
212 unsafe {
213 (*attr.cast::<RlctAttr>()).stacksize = stacksize;
214 }
215 0
216}
217
218#[unsafe(no_mangle)]
220pub unsafe extern "C" fn pthread_attr_setstacksize(
221 attr: *mut pthread_attr_t,
222 stacksize: size_t,
223) -> c_int {
224 unsafe {
225 (*attr.cast::<RlctAttr>()).stacksize = stacksize;
226 }
227 0
228}
229
230#[unsafe(no_mangle)]
231pub unsafe extern "C" fn pthread_getattr_np(
232 thread_ptr: pthread_t,
233 attr_ptr: *mut pthread_attr_t,
234) -> c_int {
235 let thread = unsafe { &*thread_ptr.cast::<Pthread>() };
236 let attr_ptr = attr_ptr.cast::<RlctAttr>();
237 unsafe { ptr::write(attr_ptr, RlctAttr::default()) };
238 let attr = unsafe { &mut *attr_ptr };
239 if thread.flags.load(Ordering::Acquire) & PthreadFlags::DETACHED.bits() != 0 {
240 attr.detachstate = PTHREAD_CREATE_DETACHED as _;
241 }
242 attr.stack = thread.stack_base as usize;
243 attr.stacksize = thread.stack_size;
244 0
246}