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