1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) 2020 Chris Johns (http://contemporary.software) |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #if !defined(_GNU_SOURCE) |
---|
29 | #define _GNU_SOURCE |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <system_error> |
---|
33 | |
---|
34 | #include <rtems/error.hpp> |
---|
35 | #include <rtems/thread.hpp> |
---|
36 | |
---|
37 | #include <pthread.h> |
---|
38 | |
---|
39 | #include <rtems.h> |
---|
40 | |
---|
41 | #if HAVE_GET_SCHEDULER_NAME |
---|
42 | extern "C" bool get_scheduler_name (rtems_id sid, char* name); |
---|
43 | #endif |
---|
44 | |
---|
45 | #if HAVE_GET_SCHEDULER_NAME |
---|
46 | bool get_scheduler_name (rtems_id sid, char* name) |
---|
47 | { |
---|
48 | name[0] = 'N'; |
---|
49 | name[1] = 'O'; |
---|
50 | name[2] = 'P'; |
---|
51 | name[3] = '\0'; |
---|
52 | return true; |
---|
53 | } |
---|
54 | #endif |
---|
55 | |
---|
56 | namespace rtems |
---|
57 | { |
---|
58 | namespace thread |
---|
59 | { |
---|
60 | void |
---|
61 | system_error_check (int ec, const char* what) |
---|
62 | { |
---|
63 | if (ec != 0) { |
---|
64 | throw std::system_error (ec, std::system_category(), what); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | attributes::attributes () |
---|
69 | : priority (-1), |
---|
70 | stack_size (RTEMS_MINIMUM_STACK_SIZE), |
---|
71 | attr (sched_inherit), |
---|
72 | policy (sched_fifo) |
---|
73 | { |
---|
74 | update (); |
---|
75 | } |
---|
76 | |
---|
77 | attributes::attributes (const attributes& attr) |
---|
78 | : name (attr.name), |
---|
79 | priority (attr.priority), |
---|
80 | stack_size (attr.stack_size), |
---|
81 | scheduler (attr.scheduler), |
---|
82 | attr (attr.attr), |
---|
83 | policy (attr.policy) |
---|
84 | { |
---|
85 | } |
---|
86 | |
---|
87 | void |
---|
88 | attributes::set_name (const std::string& name_) |
---|
89 | { |
---|
90 | name = name_; |
---|
91 | } |
---|
92 | |
---|
93 | void |
---|
94 | attributes::set_name (const char* name_) |
---|
95 | { |
---|
96 | name = name_; |
---|
97 | } |
---|
98 | |
---|
99 | const std::string& |
---|
100 | attributes::get_name () const |
---|
101 | { |
---|
102 | return name; |
---|
103 | } |
---|
104 | |
---|
105 | void |
---|
106 | attributes::set_priority (int priority_) |
---|
107 | { |
---|
108 | priority = priority_; |
---|
109 | } |
---|
110 | |
---|
111 | int |
---|
112 | attributes::get_priority () const |
---|
113 | { |
---|
114 | return priority; |
---|
115 | } |
---|
116 | |
---|
117 | void |
---|
118 | attributes::set_stack_size(size_t size) |
---|
119 | { |
---|
120 | stack_size = size; |
---|
121 | } |
---|
122 | |
---|
123 | size_t |
---|
124 | attributes::get_stack_size () const |
---|
125 | { |
---|
126 | return stack_size; |
---|
127 | } |
---|
128 | |
---|
129 | void |
---|
130 | attributes::set_scheduler (const std::string& scheduler_) |
---|
131 | { |
---|
132 | scheduler = scheduler_; |
---|
133 | } |
---|
134 | |
---|
135 | void |
---|
136 | attributes::set_scheduler (const char* scheduler_) |
---|
137 | { |
---|
138 | scheduler = scheduler_; |
---|
139 | } |
---|
140 | |
---|
141 | const std::string& |
---|
142 | attributes::get_scheduler () |
---|
143 | { |
---|
144 | return scheduler; |
---|
145 | } |
---|
146 | |
---|
147 | attributes::sched_attr |
---|
148 | attributes::get_scheduler_attr () const |
---|
149 | { |
---|
150 | return attr; |
---|
151 | } |
---|
152 | |
---|
153 | void |
---|
154 | attributes::set_scheduler_policy (sched_policy policy_) |
---|
155 | { |
---|
156 | attr = sched_explicit; |
---|
157 | policy = policy_; |
---|
158 | } |
---|
159 | |
---|
160 | attributes::sched_policy |
---|
161 | attributes::get_scheduler_policy () const |
---|
162 | { |
---|
163 | return policy; |
---|
164 | } |
---|
165 | |
---|
166 | void |
---|
167 | attributes::commit () |
---|
168 | { |
---|
169 | pthread_t pid = ::pthread_self (); |
---|
170 | |
---|
171 | system_error_check (::pthread_setname_np (pid, name.c_str ()), |
---|
172 | "getting name"); |
---|
173 | |
---|
174 | int spolicy; |
---|
175 | struct sched_param sched_param; |
---|
176 | |
---|
177 | system_error_check (::pthread_getschedparam (::pthread_self (), |
---|
178 | &spolicy, |
---|
179 | &sched_param), |
---|
180 | "getting scheduler parameters"); |
---|
181 | |
---|
182 | switch (policy) { |
---|
183 | case sched_other: |
---|
184 | spolicy = SCHED_OTHER; |
---|
185 | break; |
---|
186 | case sched_fifo: |
---|
187 | spolicy = SCHED_FIFO; |
---|
188 | break; |
---|
189 | case sched_roundrobin: |
---|
190 | spolicy = SCHED_RR; |
---|
191 | break; |
---|
192 | case sched_sporadic: |
---|
193 | spolicy = SCHED_SPORADIC; |
---|
194 | break; |
---|
195 | default: |
---|
196 | system_error_check (EINVAL, "get scheduler policy"); |
---|
197 | break; |
---|
198 | } |
---|
199 | |
---|
200 | sched_param.sched_priority = priority; |
---|
201 | |
---|
202 | system_error_check (::pthread_setschedparam (::pthread_self (), |
---|
203 | spolicy, |
---|
204 | &sched_param), |
---|
205 | "getting scheduler parameters"); |
---|
206 | |
---|
207 | if (!scheduler.empty ()) { |
---|
208 | char sname[4] = { ' ', ' ', ' ', ' ' }; |
---|
209 | for (size_t c = 0; c < sizeof (sname); ++c) { |
---|
210 | if (c >= scheduler.length ()) { |
---|
211 | break; |
---|
212 | } |
---|
213 | sname[c] = scheduler[c]; |
---|
214 | } |
---|
215 | rtems_name scheduler_name = rtems_build_name (sname[0], |
---|
216 | sname[1], |
---|
217 | sname[2], |
---|
218 | sname[3]); |
---|
219 | rtems_id scheduler_id; |
---|
220 | runtime_error_check (::rtems_scheduler_ident (scheduler_name, |
---|
221 | &scheduler_id), |
---|
222 | "get scheduler id"); |
---|
223 | // runtime_error_check (::rtems_task_set_scheduler (RTEMS_SELF, |
---|
224 | // scheduler_id, |
---|
225 | // 1), |
---|
226 | // "set scheduler id"); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | void |
---|
231 | attributes::update () |
---|
232 | { |
---|
233 | char buf[32]; |
---|
234 | system_error_check (::pthread_getname_np (::pthread_self (), |
---|
235 | buf, |
---|
236 | sizeof (buf)), |
---|
237 | "getting name"); |
---|
238 | name = buf; |
---|
239 | |
---|
240 | int spolicy; |
---|
241 | struct sched_param sched_param; |
---|
242 | system_error_check (::pthread_getschedparam (::pthread_self (), |
---|
243 | &spolicy, |
---|
244 | &sched_param), |
---|
245 | "getting scheduler parameters"); |
---|
246 | |
---|
247 | switch (spolicy) { |
---|
248 | case SCHED_OTHER: |
---|
249 | policy = sched_other; |
---|
250 | break; |
---|
251 | case SCHED_FIFO: |
---|
252 | policy = sched_fifo; |
---|
253 | break; |
---|
254 | case SCHED_RR: |
---|
255 | policy = sched_roundrobin; |
---|
256 | break; |
---|
257 | case SCHED_SPORADIC: |
---|
258 | policy = sched_sporadic; |
---|
259 | break; |
---|
260 | default: |
---|
261 | system_error_check (EINVAL, "get scheduler policy"); |
---|
262 | break; |
---|
263 | } |
---|
264 | priority = sched_param.sched_priority; |
---|
265 | |
---|
266 | pthread_attr_t pattr; |
---|
267 | system_error_check (::pthread_getattr_np (::pthread_self (), &pattr), |
---|
268 | "getting thread attributes"); |
---|
269 | system_error_check (::pthread_attr_getstacksize (&pattr, &stack_size), |
---|
270 | "getting stack size"); |
---|
271 | int inheritsched = 0; |
---|
272 | system_error_check (::pthread_attr_getinheritsched (&pattr, &inheritsched), |
---|
273 | "getting inherited sheduler attribute"); |
---|
274 | switch (inheritsched) { |
---|
275 | case PTHREAD_INHERIT_SCHED: |
---|
276 | attr = sched_inherit; |
---|
277 | break; |
---|
278 | case PTHREAD_EXPLICIT_SCHED: |
---|
279 | attr = sched_explicit; |
---|
280 | break; |
---|
281 | default: |
---|
282 | system_error_check (EINVAL, "get scheduler attribute"); |
---|
283 | break; |
---|
284 | } |
---|
285 | |
---|
286 | rtems_id scheduler_id; |
---|
287 | runtime_error_check (::rtems_task_get_scheduler (RTEMS_SELF, &scheduler_id)); |
---|
288 | #if HAVE_GET_SCHEDULER_NAME |
---|
289 | char name[5]; |
---|
290 | if (!get_scheduler_name (scheduler_id, &name[0])) { |
---|
291 | system_error_check (ENOENT, "get scheduler name"); |
---|
292 | } |
---|
293 | scheduler = name; |
---|
294 | #endif |
---|
295 | } |
---|
296 | |
---|
297 | attributes& |
---|
298 | attributes::operator= (const attributes& other) |
---|
299 | { |
---|
300 | name = other.name; |
---|
301 | priority = other.priority; |
---|
302 | stack_size = other.stack_size; |
---|
303 | attr = other.attr; |
---|
304 | policy = other.policy; |
---|
305 | return *this; |
---|
306 | } |
---|
307 | |
---|
308 | bool |
---|
309 | attributes::operator== (const attributes& other) const |
---|
310 | { |
---|
311 | return |
---|
312 | name == other.name && |
---|
313 | priority == other.priority && |
---|
314 | stack_size == other.stack_size && |
---|
315 | attr == other.attr && |
---|
316 | policy == other.policy; |
---|
317 | } |
---|
318 | |
---|
319 | void* |
---|
320 | thread_generic_entry (void* arg) |
---|
321 | { |
---|
322 | thread::state_ptr s{ static_cast<thread::state_base*> (arg) }; |
---|
323 | try { |
---|
324 | s->run (); |
---|
325 | } catch (...) { |
---|
326 | std::terminate (); |
---|
327 | } |
---|
328 | return nullptr; |
---|
329 | } |
---|
330 | |
---|
331 | thread& |
---|
332 | thread::operator= (thread&& thread_) |
---|
333 | { |
---|
334 | if (joinable ()) { |
---|
335 | std::terminate (); |
---|
336 | } |
---|
337 | swap(thread_); |
---|
338 | return *this; |
---|
339 | } |
---|
340 | |
---|
341 | void |
---|
342 | thread::swap(thread& thread_) noexcept |
---|
343 | { |
---|
344 | std::swap(id_, thread_.id_); |
---|
345 | } |
---|
346 | |
---|
347 | bool |
---|
348 | thread::joinable() const noexcept |
---|
349 | { |
---|
350 | return !(id_ == id()); |
---|
351 | } |
---|
352 | |
---|
353 | void |
---|
354 | thread::join() |
---|
355 | { |
---|
356 | if (!joinable()) { |
---|
357 | system_error_check (EINVAL, "join"); |
---|
358 | } |
---|
359 | system_error_check (::pthread_join (id_.id_, nullptr), "join"); |
---|
360 | id_ = id(); |
---|
361 | } |
---|
362 | |
---|
363 | void |
---|
364 | thread::detach() |
---|
365 | { |
---|
366 | if (!joinable()) { |
---|
367 | system_error_check (EINVAL, "detach"); |
---|
368 | } |
---|
369 | system_error_check (::pthread_detach (id_.id_), "detach"); |
---|
370 | id_ = id(); |
---|
371 | } |
---|
372 | |
---|
373 | thread::state_base::~state_base () = default; |
---|
374 | |
---|
375 | void |
---|
376 | thread::start_thread (thread::state_ptr s) |
---|
377 | { |
---|
378 | const attributes attr = s->get_attributes (); |
---|
379 | |
---|
380 | pthread_attr_t pattr; |
---|
381 | |
---|
382 | system_error_check (::pthread_attr_init (&pattr), |
---|
383 | "attribute init"); |
---|
384 | |
---|
385 | struct sched_param param; |
---|
386 | param.sched_priority = attr.get_priority (); |
---|
387 | system_error_check (::pthread_attr_setschedparam (&pattr, ¶m), |
---|
388 | "set sched param"); |
---|
389 | |
---|
390 | int spolicy; |
---|
391 | switch (attr.get_scheduler_policy ()) { |
---|
392 | case attributes::sched_other: |
---|
393 | spolicy = SCHED_OTHER; |
---|
394 | break; |
---|
395 | case attributes::sched_roundrobin: |
---|
396 | spolicy = SCHED_RR; |
---|
397 | break; |
---|
398 | case attributes::sched_sporadic: |
---|
399 | spolicy = SCHED_SPORADIC; |
---|
400 | break; |
---|
401 | default: |
---|
402 | spolicy = SCHED_FIFO; |
---|
403 | break; |
---|
404 | } |
---|
405 | system_error_check (::pthread_attr_setschedpolicy (&pattr, spolicy), |
---|
406 | "set scheduler policy"); |
---|
407 | |
---|
408 | if (attr.get_scheduler_attr () == attributes::sched_inherit) { |
---|
409 | ::pthread_attr_setinheritsched (&pattr, PTHREAD_INHERIT_SCHED); |
---|
410 | } |
---|
411 | else { |
---|
412 | ::pthread_attr_setinheritsched (&pattr, PTHREAD_EXPLICIT_SCHED); |
---|
413 | } |
---|
414 | |
---|
415 | system_error_check (::pthread_attr_setstacksize(&pattr, |
---|
416 | attr.get_stack_size ()), |
---|
417 | "set stack size"); |
---|
418 | |
---|
419 | /* |
---|
420 | * Hold the new thread in the state's run handler until the rest |
---|
421 | * of the thread is set up after the create call. |
---|
422 | */ |
---|
423 | system_error_check (::pthread_create (&id_.id_, |
---|
424 | &pattr, |
---|
425 | thread_generic_entry, |
---|
426 | s.get ()), |
---|
427 | "create thread"); |
---|
428 | |
---|
429 | system_error_check (::pthread_setname_np (id_.id_, |
---|
430 | attr.get_name ().c_str ()), |
---|
431 | "setting thread name"); |
---|
432 | |
---|
433 | ::pthread_attr_destroy (&pattr); |
---|
434 | |
---|
435 | s.release (); |
---|
436 | }; |
---|
437 | }; |
---|
438 | }; |
---|