KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/thread.h
4 Copyright (C) 2000, 2001, 2002, 2003 Megan Potter
5 Copyright (C) 2009, 2010, 2016 Lawrence Sebald
6 Copyright (C) 2023 Colton Pawielski
7 Copyright (C) 2023, 2024 Falco Girgis
8
9*/
10
11/** \file kos/thread.h
12 \brief Threading support.
13 \ingroup kthreads
14
15 This file contains the interface to the threading system of KOS. Timer
16 interrupts are used to reschedule threads within the system.
17
18 \see arch/timer.h
19 \see kos/genwait.h
20 \see kos/mutex.h
21 \see kos/once.h
22 \see kos/recursive_lock.h
23 \see kos/rwsem.h
24 \see kos/sem.h
25 \see kos/tls.h
26
27 \todo
28 - Remove deprecated thread mode API
29 - Remove global extern pointer to current thread
30
31 \author Megan Potter
32 \author Lawrence Sebald
33 \author Falco Girgis
34*/
35
36#ifndef __KOS_THREAD_H
37#define __KOS_THREAD_H
38
39#include <sys/cdefs.h>
40__BEGIN_DECLS
41
42#include <kos/cdefs.h>
43#include <kos/tls.h>
44#include <arch/irq.h>
45#include <sys/queue.h>
46#include <sys/reent.h>
47
48#include <stdint.h>
49#include <stdbool.h>
50
51/** \defgroup kthreads Kernel
52 \brief KOS Native Kernel Threading API
53 \ingroup threading
54
55 The thread scheduler itself is a relatively simplistic priority scheduler.
56 There is no provision for priorities to erode over time, so keep that in
57 mind. That practically means that if you have 2 high priority threads that
58 are always runnable and one low priority thread that is always runnable, the
59 low priority thread will never actually run (since it will never get to the
60 front of the run queue because of the high priority threads).
61
62 The scheduler supports two distinct types of threads: joinable and detached
63 threads. A joinable thread is one that can return a value to the creating
64 thread (or for that matter, any other thread that wishes to join it). A
65 detached thread is one that is completely detached from the rest of the
66 system and cannot return values by "normal" means. Detached threads
67 automatically clean up all of the internal resources associated with the
68 thread when it exits. Joinable threads, on the other hand, must keep some
69 state available for the ability to return values. To make sure that all
70 memory allocated by the thread's internal structures gets freed, you must
71 either join with the thread (with thd_join()) or detach it (with
72 thd_detach()). The old KOS threading system only had what would be
73 considered detached threads.
74
75 \sa semaphore_t, mutex_t, kthread_once_t, kthread_key_t, rw_semaphore_t
76
77 @{
78*/
79
80/** \brief Process ID
81
82 This macro defines the single process ID that encompasses all of KOS and the
83 running application along with all of its threads.
84*/
85#define KOS_PID 1
86
87/** \brief Maximal thread priority
88
89 This macro defines the maximum value for a thread's priority. Note that the
90 larger this number, the lower the priority of the thread.
91*/
92#define PRIO_MAX 4096
93
94/** \brief Default thread priority
95
96 Threads are created by default with the priority specified.
97*/
98#define PRIO_DEFAULT 10
99
100/** \brief Size of a kthread's label
101
102 Maximum number of characters in a thread's label or name
103 (including NULL terminator).
104*/
105#define KTHREAD_LABEL_SIZE 256
106
107/** \brief Size of a kthread's current directory
108
109 Maximum number of characters in a thread's current working
110 directory (including NULL terminator).
111*/
112#define KTHREAD_PWD_SIZE 256
113
114/* Pre-define list/queue types */
115struct kthread;
116
117/* \cond */
118TAILQ_HEAD(ktqueue, kthread);
119LIST_HEAD(ktlist, kthread);
120/* \endcond */
121
122/** \name Thread flag values
123 \brief Flags for kthread_flags_t
124
125 These are possible values for the flags field on the kthread_t structure.
126 These can be ORed together.
127
128 @{
129*/
130#define THD_DEFAULTS 0 /**< \brief Defaults: no flags */
131#define THD_USER 1 /**< \brief Thread runs in user mode */
132#define THD_QUEUED 2 /**< \brief Thread is in the run queue */
133#define THD_DETACHED 4 /**< \brief Thread is detached */
134#define THD_OWNS_STACK 8 /**< \brief Thread manages stack lifetime */
135/** @} */
136
137/** \brief Kernel thread flags type */
138typedef uint8_t kthread_flags_t;
139
140/** \brief Kernel thread state
141
142 Each thread in the system is in exactly one of this set of states.
143*/
144typedef enum kthread_state {
145 STATE_ZOMBIE = 0x0000, /**< \brief Waiting to die */
146 STATE_RUNNING = 0x0001, /**< \brief Process is "current" */
147 STATE_READY = 0x0002, /**< \brief Ready to be scheduled */
148 STATE_WAIT = 0x0003, /**< \brief Blocked on a genwait */
149 STATE_FINISHED = 0x0004 /**< \brief Finished execution */
151
152/** \brief Thread Control Block Header
153
154 Header preceding the static TLS data segments as defined by
155 the SH-ELF TLS ABI (version 1). This is what the thread pointer
156 (GBR) points to for compiler access to thread-local data.
157*/
158typedef struct tcbhead {
159 void *dtv; /**< \brief Dynamic TLS vector (unused) */
160 uintptr_t pointer_guard; /**< \brief Pointer guard (unused) */
161} tcbhead_t;
162
163/** \brief Structure describing one running thread.
164
165 Each thread has one of these structures assigned to it, which holds all the
166 data associated with the thread. There are various functions to manipulate
167 the data in here, so you shouldn't generally do so manually.
168*/
169typedef __attribute__((aligned(32))) struct kthread {
170 /** \brief Register store -- used to save thread context. */
172
173 /** \brief Thread list handle. Not a function. */
174 LIST_ENTRY(kthread) t_list;
175
176 /** \brief Run/Wait queue handle. Once again, not a function. */
177 TAILQ_ENTRY(kthread) thdq;
178
179 /** \brief Timer queue handle (if applicable). Also not a function. */
180 TAILQ_ENTRY(kthread) timerq;
181
182 /** \brief Kernel thread id. */
184
185 /** \brief Dynamic priority */
187
188 /** \brief Static priority: 0..PRIO_MAX (higher means lower priority). */
190
191 /** \brief Thread flags. */
193
194 /** \brief Process state */
196
197 /** \brief Generic wait target, if waiting.
198
199 \see kos/genwait.h
200 */
201 void *wait_obj;
202
203 /** \brief Generic wait message, if waiting.
204
205 \see kos/genwait.h
206 */
207 const char *wait_msg;
208
209 /** \brief Wait timeout callback.
210
211 If the genwait times out while waiting, this function will be called.
212 This allows hooks for things like fixing up semaphore count values, etc.
213
214 \param obj The object that we were waiting on.
215 */
216 void (*wait_callback)(void *obj);
217
218 /** \brief Next scheduled time.
219
220 This value is used for sleep and timed block operations. This value is
221 in milliseconds since the start of timer_ms_gettime(). This should be
222 enough for something like 2 million years of wait time. ;)
223 */
224 uint64_t wait_timeout;
225
226 /** \brief Per-Thread CPU Time. */
227 struct {
228 uint64_t scheduled; /**< \brief time when the thread became active */
229 uint64_t total; /**< \brief total running CPU time for thread */
230 } cpu_time;
231
232 /** \brief Thread label.
233
234 This value is used when printing out a user-readable process listing.
235 */
237
238 /** \brief Current file system path. */
240
241 /** \brief Thread private stack.
242
243 This should be a pointer to the base of a stack page.
244 */
245 void *stack;
246
247 /** \brief Size of the thread's stack, in bytes. */
249
250 /** \brief Thread errno variable. */
252
253 /** \brief Our reent struct for newlib. */
254 struct _reent thd_reent;
255
256 /** \brief OS-level thread-local storage.
257
258 \see kos/tls.h
259 */
260 struct kthread_tls_kv_list tls_list;
261
262 /** \brief Compiler-level thread-local storage. */
264
265 /** \brief Return value of the thread function.
266
267 This is only used in joinable threads.
268 */
269 void *rv;
270} kthread_t;
271
272/** \brief Thread creation attributes.
273
274 This structure allows you to specify the various attributes for a thread to
275 have when it is created. These can only be modified (in general) at thread
276 creation time (with the exception of detaching a thread, which can be done
277 later with thd_detach()).
278
279 Leaving any of the attributes in this structure 0 will set them to their
280 default value.
281
282 \headerfile kos/thread.h
283*/
284typedef struct kthread_attr {
285 /** \brief 1 for a detached thread. */
287
288 /** \brief Set the size of the stack to be created. */
290
291 /** \brief Pre-allocate a stack for the thread.
292 \note If you use this attribute, you must also set stack_size. */
294
295 /** \brief Set the thread's priority. */
297
298 /** \brief Thread label. */
299 const char *label;
301
302/** \brief kthread mode values
303
304 \deprecated
305 Only preemptive scheduling is still supported!
306
307 The threading system will always be in one of the following modes. This
308 represents either pre-emptive scheduling or an un-initialized state.
309*/
310typedef enum kthread_mode {
311 THD_MODE_NONE = -1, /**< \brief Threads not running */
312 THD_MODE_COOP = 0, /**< \brief Cooperative mode \deprecated */
313 THD_MODE_PREEMPT = 1 /**< \brief Preemptive threading mode */
315
316/** \cond The currently executing thread -- Do not manipulate directly! */
317extern kthread_t *thd_current;
318/** \endcond */
319
320/** \brief Block the current thread.
321
322 Blocks the calling thread and performs a reschedule as if a context switch
323 timer had been executed. This is useful for, e.g., blocking on sync
324 primitives. The param 'mycxt' should point to the calling thread's context
325 block. This is implemented in arch-specific code.
326
327 The meaningfulness of the return value depends on whether the unblocker set
328 a return value or not.
329
330 \param mycxt The IRQ context of the calling thread.
331
332 \return Whatever the unblocker deems necessary to return.
333*/
335
336/** \brief Find a new thread to swap in.
337
338 This function looks at the state of the system and returns a new thread
339 context to swap in. This is called from thd_block_now() and from the
340 preemptive context switcher. Note that thd_current might be NULL on entering
341 this function, if the caller blocked itself.
342
343 It is assumed that by the time this returns, the irq_srt_addr and
344 thd_current will be updated.
345
346 \return The IRQ context of the thread selected.
347*/
349
350/** \brief Given a thread ID, locates the thread structure.
351 \relatesalso kthread_t
352
353 \param tid The thread ID to retrieve.
354
355 \return The thread on success, NULL on failure.
356*/
358
359/** \brief Enqueue a process in the runnable queue.
360 \relatesalso kthread_t
361
362 This function adds a thread to the runnable queue after the process group of
363 the same priority if front_of_line is zero, otherwise queues it at the front
364 of its priority group. Generally, you will not have to do this manually.
365
366 \param t The thread to queue.
367 \param front_of_line Set to true to put this thread in front of other
368 threads of the same priority, false to put it
369 behind the other threads (normal behavior).
370
371 \sa thd_remove_from_runnable
372*/
373void thd_add_to_runnable(kthread_t *t, bool front_of_line);
374
375/** \brief Removes a thread from the runnable queue, if it's there.
376 \relatesalso kthread_t
377
378 This function removes a thread from the runnable queue, if it is currently
379 in that queue. Generally, you shouldn't have to do this manually, as waiting
380 on synchronization primitives and the like will do this for you if needed.
381
382 \param thd The thread to remove from the runnable queue.
383
384 \retval 0 On success, or if the thread isn't runnable.
385
386 \sa thd_add_to_runnable
387*/
389
390/** \brief Create a new thread.
391 \relatesalso kthread_t
392
393 This function creates a new kernel thread with default parameters to run the
394 given routine. The thread will terminate and clean up resources when the
395 routine completes if the thread is created detached, otherwise you must
396 join the thread with thd_join() to clean up after it.
397
398 \param detach Set to true to create a detached thread. Set to
399 false to create a joinable thread.
400 \param routine The function to call in the new thread.
401 \param param A parameter to pass to the function called.
402
403 \return The new thread on success, NULL on failure.
404
405 \sa thd_create_ex, thd_destroy
406*/
407kthread_t *thd_create(bool detach, void *(*routine)(void *param), void *param);
408
409/** \brief Create a new thread with the specified set of attributes.
410 \relatesalso kthread_t
411
412 This function creates a new kernel thread with the specified set of
413 parameters to run the given routine.
414
415 \param attr A set of thread attributes for the created thread.
416 Passing NULL will initialize all attributes to their
417 default values.
418 \param routine The function to call in the new thread.
419 \param param A parameter to pass to the function called.
420
421 \return The new thread on success, NULL on failure.
422
423 \sa thd_create, thd_destroy
424*/
426 void *(*routine)(void *param), void *param);
427
428/** \brief Brutally kill the given thread.
429 \relatesalso kthread_t
430
431 This function kills the given thread, removing it from the execution chain,
432 cleaning up thread-local data and other internal structures. In general, you
433 shouldn't call this function at all.
434
435 \warning
436 You should never call this function on the current thread.
437
438 \param thd The thread to destroy.
439 \retval 0 On success.
440
441 \sa thd_create
442*/
444
445/** \brief Exit the current thread.
446
447 This function ends the execution of the current thread, removing it from all
448 execution queues. This function will never return to the thread. Returning
449 from the thread's function is equivalent to calling this function.
450
451 \param rv The return value of the thread.
452*/
453void thd_exit(void *rv) __noreturn;
454
455/** \brief Force a thread reschedule.
456
457 This function is the thread scheduler, and is generally called from a timer
458 interrupt. You will most likely never have a reason to call this function
459 directly.
460
461 For most cases, you'll want to set front_of_line to zero, but read the
462 comments in kernel/thread/thread.c for more info, especially if you need to
463 guarantee low latencies. This function just updates irq_srt_addr and
464 thd_current. Set 'now' to non-zero if you want to use a particular system
465 time for checking timeouts.
466
467 \param front_of_line Set to false, unless you have a good reason not to.
468 \param now Set to 0, unless you have a good reason not to.
469
470 \sa thd_schedule_next
471*/
472void thd_schedule(bool front_of_line, uint64_t now);
473
474/** \brief Force a given thread to the front of the queue.
475 \relatesalso kthread_t
476
477 This function promotes the given thread to be the next one that will be
478 swapped in by the scheduler. This function is only callable inside an
479 interrupt context (it simply returns otherwise).
480
481 \param thd The thread to schedule next.
482*/
484
485/** \brief Throw away the current thread's timeslice.
486
487 This function manually yields the current thread's timeslice to the system,
488 forcing a reschedule to occur.
489*/
490void thd_pass(void);
491
492/** \brief Sleep for a given number of milliseconds.
493
494 This function puts the current thread to sleep for the specified amount of
495 time. The thread will be removed from the runnable queue until the given
496 number of milliseconds passes. That is to say that the thread will sleep for
497 at least the given number of milliseconds. If another thread is running, it
498 will likely sleep longer.
499
500 \note
501 When \p ms is given a value of `0`, this is equivalent to thd_pass().
502
503 \param ms The number of milliseconds to sleep.
504*/
505void thd_sleep(unsigned ms);
506
507/** \brief Set a thread's priority value.
508 \relatesalso kthread_t
509
510 This function is used to change the priority value of a thread. If the
511 thread is scheduled already, it will be rescheduled with the new priority
512 value.
513
514 \param thd The thread to change the priority of.
515 \param prio The priority value to assign to the thread.
516
517 \retval 0 On success.
518 \retval -1 thd is NULL.
519 \retval -2 prio requested was out of range.
520*/
522
523/** \brief Retrieve the current thread's kthread struct.
524 \relatesalso kthread_t
525
526 \return The current thread's structure.
527*/
529
530/** \brief Retrieve the thread's label.
531 \relatesalso kthread_t
532
533 \param thd The thread to retrieve from.
534
535 \return The human-readable label of the thread.
536
537 \sa thd_set_label
538*/
539const char *thd_get_label(kthread_t *thd);
540
541/** \brief Set the thread's label.
542 \relatesalso kthread_t
543
544 This function sets the label of a thread, which is simply a human-readable
545 string that is used to identify the thread. These labels aren't used for
546 anything internally, and you can give them any label you want. These are
547 mainly seen in the printouts from thd_pslist() or thd_pslist_queue().
548
549 \param thd The thread to set the label of.
550 \param label The string to set as the label.
551
552 \sa thd_get_label
553*/
554void thd_set_label(kthread_t *thd, const char *__RESTRICT label);
555
556/** \brief Retrieve the thread's current working directory.
557 \relatesalso kthread_t
558
559 This function retrieves the working directory of a thread. Generally, you
560 will want to use either fs_getwd() or one of the standard C functions for
561 doing this, but this is here in case you need it when the thread isn't
562 active for some reason.
563
564 \param thd The thread to retrieve from.
565
566 \return The thread's working directory.
567
568 \sa thd_set_pd
569*/
570const char *thd_get_pwd(kthread_t *thd);
571
572/** \brief Set the thread's current working directory.
573 \relatesalso kthread_t
574
575 This function will set the working directory of a thread. Generally, you
576 will want to use either fs_chdir() or the standard C chdir() function to
577 do this, but this is here in case you need to do it while the thread isn't
578 active for some reason.
579
580 \param thd The thread to set the working directory of.
581 \param pwd The directory to set as active.
582
583 \sa thd_get_pwd
584*/
585void thd_set_pwd(kthread_t *thd, const char *__RESTRICT pwd);
586
587/** \brief Retrieve a pointer to the thread errno.
588 \relatesalso kthread_t
589
590 This function retrieves a pointer to the errno value for the thread. You
591 should generally just use the errno variable to access this.
592
593 \param thd The thread to retrieve from.
594
595 \return A pointer to the thread's errno.
596*/
598
599/** \brief Retrieve a pointer to the thread reent struct.
600 \relatesalso kthread_t
601
602 This function is used to retrieve some internal state that is used by
603 newlib to provide a reentrant libc.
604
605 \param thd The thread to retrieve from.
606
607 \return The thread's reent struct.
608*/
609struct _reent *thd_get_reent(kthread_t *thd);
610
611
612/** \brief Retrieves the thread's elapsed CPU time
613 \relatesalso kthread_t
614
615 Returns the amount of active CPU time the thread has consumed in
616 nanoseconds.
617
618 \warning
619 The implementation uses perf_cntr_timer_ns() internally when maintaining
620 this CPU time, so disabling or clearing the nanosecond timer will
621 interfere with this time keeping.
622
623 \param thd The thead to retrieve the CPU time for
624
625 \retval Total utilized CPU time in nanoseconds OR
626 0 if the nanosecond timer of the performance
627 counters has been disturbed.
628*/
630
631/** \brief Change threading modes.
632
633 This function changes the current threading mode of the system.
634 With preemptive threading being the only mode.
635
636 \deprecated
637 This is now deprecated
638
639 \param mode One of the THD_MODE values.
640
641 \return The old mode of the threading system.
642
643 \sa thd_get_mode
644*/
646
647/** \brief Fetch the current threading mode.
648
649 With preemptive threading being the only mode.
650
651 \deprecated
652 This is now deprecated.
653
654 \return The current mode of the threading system.
655
656 \sa thd_set_mode
657*/
659
660/** \brief Set the scheduler's frequency.
661
662 Sets the frequency of the scheduler interrupts in hertz.
663
664 \param hertz The new frequency in hertz (1-1000)
665
666 \retval 0 The frequency was updated successfully.
667 \retval -1 \p hertz is invalid.
668
669 \sa thd_get_hz(), HZ
670*/
671int thd_set_hz(unsigned int hertz);
672
673/** \brief Fetch the scheduler's current frequency.
674
675 Queries the scheduler for its interrupt frequency in hertz.
676
677 \return Scheduler frequency in hertz.
678
679 \sa thd_set_hz(), HZ
680*/
681unsigned thd_get_hz(void);
682
683/** \brief Wait for a thread to exit.
684 \relatesalso kthread_t
685
686 This function "joins" a joinable thread. This means effectively that the
687 calling thread blocks until the speified thread completes execution. It is
688 invalid to join a detached thread, only joinable threads may be joined.
689
690 \param thd The joinable thread to join.
691 \param value_ptr A pointer to storage for the thread's return value,
692 or NULL if you don't care about it.
693
694 \return 0 on success, or less than 0 if the thread is
695 non-existent or not joinable.
696
697 \sa thd_detach
698*/
699int thd_join(kthread_t *thd, void **value_ptr);
700
701/** \brief Detach a joinable thread.
702 \relatesalso kthread_t
703
704 This function switches the specified thread's mode from THD_MODE_JOINABLE
705 to THD_MODE_DETACHED. This will ensure that the thread cleans up all of its
706 internal resources when it exits.
707
708 \param thd The joinable thread to detach.
709
710 \return 0 on success or less than 0 if the thread is
711 non-existent or already detached.
712 \sa thd_join()
713*/
715
716/** \brief Iterate all threads and call the passed callback for each
717 \relatesalso kthread_t
718
719 \param cb The callback to call for each thread.
720 If a nonzero value is returned, iteration
721 ceases immediately.
722 \param data User data to be passed to the callback
723
724 \retval 0 or the first nonzero value returned by \p cb.
725
726 \sa thd_pslist
727*/
728int thd_each(int (*cb)(kthread_t *thd, void *user_data), void *data);
729
730/** \brief Print a list of all threads using the given print function.
731
732 \param pf The printf-like function to print with.
733
734 \retval 0 On success.
735
736 \sa thd_pslist_queue
737*/
738int thd_pslist(int (*pf)(const char *fmt, ...));
739
740/** \brief Print a list of all queued threads using the given print function.
741
742 \param pf The printf-like function to print with.
743
744 \retval 0 On success.
745
746 \sa thd_pslist
747*/
748int thd_pslist_queue(int (*pf)(const char *fmt, ...));
749
750/** \cond INTERNAL */
751
752/** \brief Initialize the threading system.
753
754 This is normally done for you by default when KOS starts. This will also
755 initialize all the various synchronization primitives.
756 \retval -1 If threads are already initialized.
757 \retval 0 On success.
758 \sa thd_shutdown
759*/
760int thd_init(void);
761
762
763/** \brief Shutdown the threading system.
764
765 This is done for you by the normal shutdown procedure of KOS. This will
766 also shutdown all the various synchronization primitives.
767
768 \sa thd_init
769*/
770void thd_shutdown(void);
771
772/** \endcond */
773
774/** @} */
775
776__END_DECLS
777
778#endif /* __KOS_THREAD_H */
Definitions for builtin attributes and compiler directives.
irq_context_t * thd_choose_new(void)
Find a new thread to swap in.
kthread_mode_t thd_get_mode(void) __deprecated
Fetch the current threading mode.
int thd_set_hz(unsigned int hertz)
Set the scheduler's frequency.
int thd_each(int(*cb)(kthread_t *thd, void *user_data), void *data)
Iterate all threads and call the passed callback for each.
void thd_exit(void *rv) __noreturn
Exit the current thread.
int thd_remove_from_runnable(kthread_t *thd)
Removes a thread from the runnable queue, if it's there.
void thd_schedule(bool front_of_line, uint64_t now)
Force a thread reschedule.
struct _reent * thd_get_reent(kthread_t *thd)
Retrieve a pointer to the thread reent struct.
int thd_destroy(kthread_t *thd)
Brutally kill the given thread.
int thd_block_now(irq_context_t *mycxt)
Block the current thread.
unsigned thd_get_hz(void)
Fetch the scheduler's current frequency.
uint64_t thd_get_cpu_time(kthread_t *thd)
Retrieves the thread's elapsed CPU time.
const char * thd_get_pwd(kthread_t *thd)
Retrieve the thread's current working directory.
void thd_add_to_runnable(kthread_t *t, bool front_of_line)
Enqueue a process in the runnable queue.
int thd_detach(kthread_t *thd)
Detach a joinable thread.
int thd_set_prio(kthread_t *thd, prio_t prio)
Set a thread's priority value.
void thd_schedule_next(kthread_t *thd)
Force a given thread to the front of the queue.
kthread_t * thd_create(bool detach, void *(*routine)(void *param), void *param)
Create a new thread.
#define KTHREAD_LABEL_SIZE
Size of a kthread's label.
Definition thread.h:105
kthread_state_t
Kernel thread state.
Definition thread.h:144
int thd_join(kthread_t *thd, void **value_ptr)
Wait for a thread to exit.
kthread_mode_t
kthread mode values
Definition thread.h:310
int * thd_get_errno(kthread_t *thd)
Retrieve a pointer to the thread errno.
kthread_t * thd_by_tid(tid_t tid)
Given a thread ID, locates the thread structure.
void thd_sleep(unsigned ms)
Sleep for a given number of milliseconds.
kthread_t * thd_get_current(void)
Retrieve the current thread's kthread struct.
uint8_t kthread_flags_t
Kernel thread flags type.
Definition thread.h:138
void thd_set_pwd(kthread_t *thd, const char *__RESTRICT pwd)
Set the thread's current working directory.
const char * thd_get_label(kthread_t *thd)
Retrieve the thread's label.
kthread_t * thd_create_ex(const kthread_attr_t *__RESTRICT attr, void *(*routine)(void *param), void *param)
Create a new thread with the specified set of attributes.
int thd_pslist_queue(int(*pf)(const char *fmt,...))
Print a list of all queued threads using the given print function.
int thd_pslist(int(*pf)(const char *fmt,...))
Print a list of all threads using the given print function.
int thd_set_mode(kthread_mode_t mode) __deprecated
Change threading modes.
#define KTHREAD_PWD_SIZE
Size of a kthread's current directory.
Definition thread.h:112
void thd_pass(void)
Throw away the current thread's timeslice.
void thd_set_label(kthread_t *thd, const char *__RESTRICT label)
Set the thread's label.
@ STATE_READY
Ready to be scheduled.
Definition thread.h:147
@ STATE_FINISHED
Finished execution.
Definition thread.h:149
@ STATE_WAIT
Blocked on a genwait.
Definition thread.h:148
@ STATE_RUNNING
Process is "current".
Definition thread.h:146
@ STATE_ZOMBIE
Waiting to die.
Definition thread.h:145
@ THD_MODE_NONE
Threads not running.
Definition thread.h:311
@ THD_MODE_PREEMPT
Preemptive threading mode.
Definition thread.h:313
@ THD_MODE_COOP
Cooperative mode.
Definition thread.h:312
#define __RESTRICT
Definition cdefs.h:176
#define __deprecated
Mark something as deprecated.
Definition cdefs.h:109
#define __noreturn
Identify a function that will never return.
Definition cdefs.h:45
typedef LIST_HEAD(nmmgr_list, nmmgr_handler) nmmgr_list_t
Name handler list type.
handle_t prio_t
Priority value type.
Definition types.h:86
handle_t tid_t
Thread ID type.
Definition types.h:85
Interrupt and exception handling.
Architecture-specific structure for holding the processor state.
Definition irq.h:86
Thread creation attributes.
Definition thread.h:284
prio_t prio
Set the thread's priority.
Definition thread.h:296
void * stack_ptr
Pre-allocate a stack for the thread.
Definition thread.h:293
const char * label
Thread label.
Definition thread.h:299
bool create_detached
1 for a detached thread.
Definition thread.h:286
size_t stack_size
Set the size of the stack to be created.
Definition thread.h:289
Structure describing one running thread.
Definition thread.h:169
irq_context_t context
Register store – used to save thread context.
Definition thread.h:171
tid_t tid
Kernel thread id.
Definition thread.h:183
kthread_flags_t flags
Thread flags.
Definition thread.h:192
int thd_errno
Thread errno variable.
Definition thread.h:251
uint64_t wait_timeout
Next scheduled time.
Definition thread.h:224
uint64_t scheduled
time when the thread became active
Definition thread.h:228
uint64_t total
total running CPU time for thread
Definition thread.h:229
kthread_state_t state
Process state.
Definition thread.h:195
void * stack
Thread private stack.
Definition thread.h:245
void * rv
Return value of the thread function.
Definition thread.h:269
void * wait_obj
Generic wait target, if waiting.
Definition thread.h:201
TAILQ_ENTRY(kthread) timerq
Timer queue handle (if applicable).
tcbhead_t * tcbhead
Compiler-level thread-local storage.
Definition thread.h:263
prio_t prio
Dynamic priority.
Definition thread.h:186
LIST_ENTRY(kthread) t_list
Thread list handle.
TAILQ_ENTRY(kthread) thdq
Run/Wait queue handle.
const char * wait_msg
Generic wait message, if waiting.
Definition thread.h:207
prio_t real_prio
Static priority: 0..PRIO_MAX (higher means lower priority).
Definition thread.h:189
size_t stack_size
Size of the thread's stack, in bytes.
Definition thread.h:248
Thread Control Block Header.
Definition thread.h:158
void * dtv
Dynamic TLS vector (unused)
Definition thread.h:159
uintptr_t pointer_guard
Pointer guard (unused)
Definition thread.h:160
Thread-local storage support.