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