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 <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 Our reent struct for newlib. */
247 struct _reent thd_reent;
248
249 /** \brief OS-level thread-local storage.
250
251 \see kos/tls.h
252 */
253 struct kthread_tls_kv_list tls_list;
254
255 /** \brief Compiler-level thread-local storage. */
256 void *tls_hnd;
257
258 /** \brief Return value of the thread function.
259
260 This is only used in joinable threads.
261 */
262 void *rv;
263} kthread_t;
264
265/** \brief Thread creation attributes.
266
267 This structure allows you to specify the various attributes for a thread to
268 have when it is created. These can only be modified (in general) at thread
269 creation time (with the exception of detaching a thread, which can be done
270 later with thd_detach()).
271
272 Leaving any of the attributes in this structure 0 will set them to their
273 default value.
274
275 \headerfile kos/thread.h
276*/
277typedef struct kthread_attr {
278 /** \brief 1 for a detached thread. */
280
281 /** \brief Set the size of the stack to be created. */
283
284 /** \brief Pre-allocate a stack for the thread.
285 \note If you use this attribute, you must also set stack_size. */
287
288 /** \brief Set the thread's priority. */
290
291 /** \brief Thread label. */
292 const char *label;
293
294 /** \brief 1 if the thread doesn't use thread_local variables. */
297
298/** \brief kthread mode values
299
300 \deprecated
301 Only preemptive scheduling is still supported!
302
303 The threading system will always be in one of the following modes. This
304 represents either pre-emptive scheduling or an un-initialized state.
305*/
306typedef enum kthread_mode {
307 THD_MODE_NONE = -1, /**< \brief Threads not running */
308 THD_MODE_COOP = 0, /**< \brief Cooperative mode \deprecated */
309 THD_MODE_PREEMPT = 1 /**< \brief Preemptive threading mode */
311
312/** \cond The currently executing thread -- Do not manipulate directly! */
313extern kthread_t *thd_current;
314/** \endcond */
315
316/** \brief Block the current thread.
317
318 Blocks the calling thread and performs a reschedule as if a context switch
319 timer had been executed. This is useful for, e.g., blocking on sync
320 primitives. The param 'mycxt' should point to the calling thread's context
321 block. This is implemented in arch-specific code.
322
323 The meaningfulness of the return value depends on whether the unblocker set
324 a return value or not.
325
326 \param mycxt The IRQ context of the calling thread.
327
328 \return Whatever the unblocker deems necessary to return.
329*/
330int thd_block_now(irq_context_t *mycxt) __nonnull_all;
331
332/** \brief Find a new thread to swap in.
333
334 This function looks at the state of the system and returns a new thread
335 context to swap in. This is called from thd_block_now() and from the
336 preemptive context switcher. Note that thd_current might be NULL on entering
337 this function, if the caller blocked itself.
338
339 It is assumed that by the time this returns, the irq_srt_addr and
340 thd_current will be updated.
341
342 \return The IRQ context of the thread selected.
343*/
344irq_context_t *thd_choose_new(void);
345
346/** \brief Given a thread ID, locates the thread structure.
347 \relatesalso kthread_t
348
349 \param tid The thread ID to retrieve.
350
351 \return The thread on success, NULL on failure.
352*/
354
355/** \brief Enqueue a process in the runnable queue.
356 \relatesalso kthread_t
357
358 This function adds a thread to the runnable queue after the process group of
359 the same priority if front_of_line is zero, otherwise queues it at the front
360 of its priority group. Generally, you will not have to do this manually.
361
362 \param t The thread to queue.
363 \param front_of_line Set to true to put this thread in front of other
364 threads of the same priority, false to put it
365 behind the other threads (normal behavior).
366
367 \sa thd_remove_from_runnable
368*/
369void thd_add_to_runnable(kthread_t *t, bool front_of_line) __nonnull_all;
370
371/** \brief Removes a thread from the runnable queue, if it's there.
372 \relatesalso kthread_t
373
374 This function removes a thread from the runnable queue, if it is currently
375 in that queue. Generally, you shouldn't have to do this manually, as waiting
376 on synchronization primitives and the like will do this for you if needed.
377
378 \param thd The thread to remove from the runnable queue.
379
380 \retval 0 On success, or if the thread isn't runnable.
381
382 \sa thd_add_to_runnable
383*/
385
386/** \brief Create a new thread.
387 \relatesalso kthread_t
388
389 This function creates a new kernel thread with default parameters to run the
390 given routine. The thread will terminate and clean up resources when the
391 routine completes if the thread is created detached, otherwise you must
392 join the thread with thd_join() to clean up after it.
393
394 \param detach Set to true to create a detached thread. Set to
395 false to create a joinable thread.
396 \param routine The function to call in the new thread.
397 \param param A parameter to pass to the function called.
398
399 \return The new thread on success, NULL on failure.
400
401 \sa thd_create_ex, thd_destroy
402*/
403kthread_t *thd_create(bool detach, void *(*routine)(void *param), void *param);
404
405/** \brief Create a new thread with the specified set of attributes.
406 \relatesalso kthread_t
407
408 This function creates a new kernel thread with the specified set of
409 parameters to run the given routine.
410
411 \param attr A set of thread attributes for the created thread.
412 Passing NULL will initialize all attributes to their
413 default values.
414 \param routine The function to call in the new thread.
415 \param param A parameter to pass to the function called.
416
417 \return The new thread on success, NULL on failure.
418
419 \sa thd_create, thd_destroy
420*/
422 void *(*routine)(void *param), void *param);
423
424/** \brief Brutally kill the given thread.
425 \relatesalso kthread_t
426
427 This function kills the given thread, removing it from the execution chain,
428 cleaning up thread-local data and other internal structures. In general, you
429 shouldn't call this function at all.
430
431 \warning
432 You should never call this function on the current thread.
433
434 \param thd The thread to destroy.
435 \retval 0 On success.
436
437 \sa thd_create
438*/
439int thd_destroy(kthread_t *thd) __nonnull_all;
440
441/** \brief Exit the current thread.
442
443 This function ends the execution of the current thread, removing it from all
444 execution queues. This function will never return to the thread. Returning
445 from the thread's function is equivalent to calling this function.
446
447 \param rv The return value of the thread.
448*/
449void thd_exit(void *rv) __noreturn;
450
451/** \brief Force a thread reschedule.
452
453 This function is the thread scheduler, and MUST be called in an interrupt
454 context (typically from the primary timer interrupt).
455
456 For most cases, you'll want to set front_of_line to zero, but read the
457 comments in kernel/thread/thread.c for more info, especially if you need to
458 guarantee low latencies. This function just updates irq_srt_addr and
459 thd_current. Set 'now' to non-zero if you want to use a particular system
460 time for checking timeouts.
461
462 \param front_of_line Set to false, unless you have a good reason not to.
463
464 \sa thd_schedule_next
465 \warning Never call this function from outside of an
466 interrupt context! Doing so will almost certainly
467 end very poorly.
468*/
469void thd_schedule(bool front_of_line);
470
471/** \brief Force a given thread to the front of the queue.
472 \relatesalso kthread_t
473
474 This function promotes the given thread to be the next one that will be
475 swapped in by the scheduler. This function is only callable inside an
476 interrupt context (it simply returns otherwise).
477
478 \param thd The thread to schedule next.
479*/
480void thd_schedule_next(kthread_t *thd) __nonnull_all;
481
482/** \brief Throw away the current thread's timeslice.
483
484 This function manually yields the current thread's timeslice to the system,
485 forcing a reschedule to occur.
486*/
487void thd_pass(void);
488
489/** \brief Sleep for a given number of milliseconds.
490
491 This function puts the current thread to sleep for the specified amount of
492 time. The thread will be removed from the runnable queue until the given
493 number of milliseconds passes. That is to say that the thread will sleep for
494 at least the given number of milliseconds. If another thread is running, it
495 will likely sleep longer.
496
497 \note
498 When \p ms is given a value of `0`, this is equivalent to thd_pass().
499
500 \param ms The number of milliseconds to sleep.
501*/
502void thd_sleep(unsigned ms);
503
504/** \brief Callback type for thd_poll(). */
505typedef int (*thd_cb_t)(void *);
506
507/** \brief Poll until the callback function returns non-zero.
508
509 This function will put the current thread into a pseudo-sleep state. The
510 scheduler will periodically call the callback function, and if it returns
511 non-zero, the thread is awaken.
512 Since the callback function is called by the scheduler, the callback will
513 be running inside an interrupt context, with all that entails.
514
515 \param cb The polling function.
516 \param data A pointer provided to the polling function.
517 \param timeout_ms If non-zero, the number of milliseconds to sleep.
518
519 \return Zero if a timeout occurs; the return value of the
520 polling function otherwise.
521*/
522int thd_poll(thd_cb_t cb, void *data, unsigned long timeout_ms);
523
524/** \brief Set a thread's priority value.
525 \relatesalso kthread_t
526
527 This function is used to change the priority value of a thread. If the
528 thread is scheduled already, it will be rescheduled with the new priority
529 value.
530
531 \param thd The thread to change the priority of.
532 \param prio The priority value to assign to the thread.
533
534 \retval 0 On success.
535 \retval -1 thd is NULL.
536 \retval -2 prio requested was out of range.
537
538 \sa thd_get_prio
539*/
541
542/** \brief Retrieve a thread's priority value.
543 \relatesalso kthread_t
544
545 \param thd The thread to retrieve from. If NULL, the current
546 thread will be used.
547
548 \return The priority value of the thread
549
550 \sa thd_set_prio
551*/
553
554/** \brief Retrieve a thread's numeric identifier.
555 \relatesalso kthread_t
556
557 \param thd The thread to retrieve from. If NULL, the current
558 thread will be used.
559
560 \return The identifier of the thread
561*/
563
564/** \brief Retrieve the current thread's kthread struct.
565 \relatesalso kthread_t
566
567 \return The current thread's structure.
568*/
570
571/** \brief Retrieve the idle thread's kthread struct.
572 \relatesalso kthread_t
573
574 \return The idle thread's structure.
575*/
577
578/** \brief Retrieve the thread's label.
579 \relatesalso kthread_t
580
581 \param thd The thread to retrieve from. If NULL, the current
582 thread will be used.
583
584 \return The human-readable label of the thread.
585
586 \sa thd_set_label
587*/
588const char *thd_get_label(const kthread_t *thd);
589
590/** \brief Set the thread's label.
591 \relatesalso kthread_t
592
593 This function sets the label of a thread, which is simply a human-readable
594 string that is used to identify the thread. These labels aren't used for
595 anything internally, and you can give them any label you want. These are
596 mainly seen in the printouts from thd_pslist() or thd_pslist_queue().
597
598 \param thd The thread to set the label of. If NULL, the current
599 thread will be used.
600 \param label The string to set as the label.
601
602 \sa thd_get_label
603*/
605
606/** \brief Retrieve the thread's current working directory.
607 \relatesalso kthread_t
608
609 This function retrieves the working directory of a thread. Generally, you
610 will want to use either fs_getwd() or one of the standard C functions for
611 doing this, but this is here in case you need it when the thread isn't
612 active for some reason.
613
614 \param thd The thread to retrieve from. If NULL, the current
615 thread will be used.
616
617 \return The thread's working directory.
618
619 \sa thd_set_pd
620*/
621const char *thd_get_pwd(const kthread_t *thd);
622
623/** \brief Set the thread's current working directory.
624 \relatesalso kthread_t
625
626 This function will set the working directory of a thread. Generally, you
627 will want to use either fs_chdir() or the standard C chdir() function to
628 do this, but this is here in case you need to do it while the thread isn't
629 active for some reason.
630
631 \param thd The thread to set the working directory of.
632 If NULL, the current thread will be used.
633 \param pwd The directory to set as active.
634
635 \sa thd_get_pwd
636*/
638
639/** \brief Retrieve a pointer to the thread errno.
640 \relatesalso kthread_t
641
642 This function retrieves a pointer to the errno value for the thread. You
643 should generally just use the errno variable to access this.
644
645 \param thd The thread to retrieve from. If NULL, the current
646 thread will be used.
647
648 \return A pointer to the thread's errno.
649*/
651
652/** \brief Retrieve a pointer to the thread reent struct.
653 \relatesalso kthread_t
654
655 This function is used to retrieve some internal state that is used by
656 newlib to provide a reentrant libc.
657
658 \param thd The thread to retrieve from.
659
660 \return The thread's reent struct.
661*/
662struct _reent *thd_get_reent(kthread_t *thd);
663
664/** \brief Retrieves the thread's elapsed CPU time
665 \relatesalso kthread_t
666
667 Returns the amount of active CPU time the thread has consumed in
668 milliseconds.
669
670 \param thd The thead to retrieve the CPU time for.
671
672 \retval Total utilized CPU time in milliseconds.
673*/
675
676/** \brief Retrieves all thread's elapsed CPU time
677 \relatesalso kthread_t
678
679 Returns the amount of active CPU time all threads have consumed in
680 milliseconds.
681
682 \retval Total utilized CPU time in milliseconds.
683*/
685
686/** \brief Change threading modes.
687
688 This function changes the current threading mode of the system.
689 With preemptive threading being the only mode.
690
691 \deprecated
692 This is now deprecated
693
694 \param mode One of the THD_MODE values.
695
696 \return The old mode of the threading system.
697
698 \sa thd_get_mode
699*/
701
702/** \brief Fetch the current threading mode.
703
704 With preemptive threading being the only mode.
705
706 \deprecated
707 This is now deprecated.
708
709 \return The current mode of the threading system.
710
711 \sa thd_set_mode
712*/
713kthread_mode_t thd_get_mode(void) __deprecated;
714
715/** \brief Set the scheduler's frequency.
716
717 Sets the frequency of the scheduler interrupts in hertz.
718
719 \param hertz The new frequency in hertz (1-1000)
720
721 \retval 0 The frequency was updated successfully.
722 \retval -1 \p hertz is invalid.
723
724 \sa thd_get_hz(), HZ
725*/
726int thd_set_hz(unsigned int hertz);
727
728/** \brief Fetch the scheduler's current frequency.
729
730 Queries the scheduler for its interrupt frequency in hertz.
731
732 \return Scheduler frequency in hertz.
733
734 \sa thd_set_hz(), HZ
735*/
736unsigned thd_get_hz(void);
737
738/** \brief Wait for a thread to exit.
739 \relatesalso kthread_t
740
741 This function "joins" a joinable thread. This means effectively that the
742 calling thread blocks until the specified thread completes execution. It is
743 invalid to join a detached thread, only joinable threads may be joined.
744
745 \param thd The joinable thread to join.
746 \param value_ptr A pointer to storage for the thread's return value,
747 or NULL if you don't care about it.
748
749 \return 0 on success, or less than 0 if the thread is
750 non-existent or not joinable.
751
752 \sa thd_detach
753*/
754int thd_join(kthread_t *thd, void **value_ptr);
755
756/** \brief Detach a joinable thread.
757 \relatesalso kthread_t
758
759 This function switches the specified thread's mode from THD_MODE_JOINABLE
760 to THD_MODE_DETACHED. This will ensure that the thread cleans up all of its
761 internal resources when it exits.
762
763 \param thd The joinable thread to detach.
764
765 \return 0 on success or less than 0 if the thread is
766 non-existent or already detached.
767 \sa thd_join()
768*/
770
771/** \brief Iterate all threads and call the passed callback for each
772 \relatesalso kthread_t
773
774 \param cb The callback to call for each thread.
775 If a nonzero value is returned, iteration
776 ceases immediately.
777 \param data User data to be passed to the callback
778
779 \retval 0 or the first nonzero value returned by \p cb.
780
781 \sa thd_pslist
782*/
783int thd_each(int (*cb)(kthread_t *thd, void *user_data), void *data);
784
785/** \brief Print a list of all threads using the given print function.
786
787 Each thread is printed with its address, tid, priority level, flags,
788 it's wait timeout (if sleeping) the amount of cpu time usage in ns
789 (this includes time in IRQs), state, and name.
790
791 In addition a '[system]' item is provided that represents time since
792 initialization not spent in a thread (context switching, updating
793 wait timeouts, etc).
794
795 \param pf The printf-like function to print with.
796
797 \retval 0 On success.
798
799 \sa thd_pslist_queue
800*/
801int thd_pslist(int (*pf)(const char *fmt, ...)) __nonnull_all;
802
803/** \brief Print a list of all queued threads using the given print function.
804
805 \param pf The printf-like function to print with.
806
807 \retval 0 On success.
808
809 \sa thd_pslist
810*/
811int thd_pslist_queue(int (*pf)(const char *fmt, ...)) __nonnull_all;
812
813/** \cond INTERNAL */
814
815/** \brief Initialize the threading system.
816
817 This is normally done for you by default when KOS starts. This will also
818 initialize all the various synchronization primitives.
819 \retval -1 If threads are already initialized.
820 \retval 0 On success.
821 \sa thd_shutdown
822*/
823int thd_init(void);
824
825/** \brief Shutdown the threading system.
826
827 This is done for you by the normal shutdown procedure of KOS. This will
828 also shutdown all the various synchronization primitives.
829
830 \sa thd_init
831*/
832void thd_shutdown(void);
833
834/** \endcond */
835
836/** @} */
837
838__END_DECLS
839
840#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: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:505
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:306
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:307
@ THD_MODE_PREEMPT
Preemptive threading mode.
Definition thread.h:309
@ THD_MODE_COOP
Cooperative mode.
Definition thread.h:308
#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:277
prio_t prio
Set the thread's priority.
Definition thread.h:289
void * stack_ptr
Pre-allocate a stack for the thread.
Definition thread.h:286
const char * label
Thread label.
Definition thread.h:292
bool create_detached
1 for a detached thread.
Definition thread.h:279
size_t stack_size
Set the size of the stack to be created.
Definition thread.h:282
bool disable_tls
1 if the thread doesn't use thread_local variables.
Definition thread.h:295
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
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:262
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:256
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.