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