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