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#define PRIO_MAX 4096
94
95/** \brief Default thread priority
96
97 Threads are created by default with the priority specified.
98*/
99#define PRIO_DEFAULT 10
100
101/** \brief Size of a kthread's label
102
103 Maximum number of characters in a thread's label or name
104 (including NULL terminator).
105*/
106#define KTHREAD_LABEL_SIZE 256
107
108/** \brief Size of a kthread's current directory
109
110 Maximum number of characters in a thread's current working
111 directory (including NULL terminator).
112*/
113#define KTHREAD_PWD_SIZE 256
114
115/* Pre-define list/queue types */
116struct kthread;
117
118/* \cond */
119TAILQ_HEAD(ktqueue, kthread);
120LIST_HEAD(ktlist, kthread);
121/* \endcond */
122
123/** \name Thread flag values
124 \brief Flags for kthread_flags_t
125
126 These are possible values for the flags field on the kthread_t structure.
127 These can be ORed together.
128
129 @{
130*/
131#define THD_DEFAULTS 0x0 /**< \brief Defaults: no flags */
132#define THD_USER 0x1 /**< \brief Thread runs in user mode */
133#define THD_QUEUED 0x2 /**< \brief Thread is in the run queue */
134#define THD_DETACHED 0x4 /**< \brief Thread is detached */
135#define THD_OWNS_STACK 0x8 /**< \brief Thread manages stack lifetime */
136#define THD_DISABLE_TLS 0x10 /**< \brief Thread does not use TLS variables */
137/** @} */
138
139/** \brief Kernel thread flags type */
140typedef uint8_t kthread_flags_t;
141
142/** \brief Kernel thread state
143
144 Each thread in the system is in exactly one of this set of states.
145*/
146typedef enum kthread_state {
147 STATE_ZOMBIE = 0x0000, /**< \brief Waiting to die */
148 STATE_RUNNING = 0x0001, /**< \brief Process is "current" */
149 STATE_READY = 0x0002, /**< \brief Ready to be scheduled */
150 STATE_WAIT = 0x0003, /**< \brief Blocked on a genwait */
151 STATE_POLLING = 0x0004, /**< \brief Blocked on a poll */
152 STATE_FINISHED = 0x0005 /**< \brief Finished execution */
154
155/* Thread and priority types */
156typedef int tid_t; /**< \brief Thread ID type */
157typedef int prio_t; /**< \brief Priority value type */
158
159/** \brief Structure describing one running thread.
160
161 Each thread has one of these structures assigned to it, which holds all the
162 data associated with the thread. There are various functions to manipulate
163 the data in here, so you shouldn't generally do so manually.
164*/
165typedef struct __attribute__((aligned(32))) kthread {
166 /** \brief Register store -- used to save thread context. */
167 irq_context_t context;
168
169 /** \brief Thread list handle. Not a function. */
170 LIST_ENTRY(kthread) t_list;
171
172 /** \brief Run/Wait queue handle. Once again, not a function. */
173 TAILQ_ENTRY(kthread) thdq;
174
175 /** \brief Timer queue handle (if applicable). Also not a function. */
176 TAILQ_ENTRY(kthread) timerq;
177
178 /** \brief Kernel thread id. */
180
181 /** \brief Dynamic priority */
183
184 /** \brief Static priority: 0..PRIO_MAX (higher means lower priority). */
186
187 /** \brief Thread flags. */
189
190 /** \brief Process state */
192
193 /** \brief Generic wait target, if waiting.
194
195 \see kos/genwait.h
196 */
197 void *wait_obj;
198
199 /** \brief Generic wait message, if waiting.
200
201 \see kos/genwait.h
202 */
203 const char *wait_msg;
204
205 /** \brief Poll callback.
206
207 \param data A pointer passed to the polling function.
208 */
209 int (*poll_cb)(void *data);
210
211 /** \brief Next scheduled time.
212
213 This value is used for sleep and timed block operations. This value is
214 in milliseconds since the start of timer_ms_gettime(). This should be
215 enough for something like 2 million years of wait time. ;)
216 */
217 uint64_t wait_timeout;
218
219 /** \brief Per-Thread CPU Time. */
220 struct {
221 uint64_t scheduled; /**< \brief time when the thread became active */
222 uint64_t total; /**< \brief total running CPU time for thread */
223 } cpu_time;
224
225 /** \brief Thread label.
226
227 This value is used when printing out a user-readable process listing.
228 */
230
231 /** \brief Current file system path. */
233
234 /** \brief Thread private stack.
235
236 This should be a pointer to the base of a stack page.
237 */
238 void *stack;
239
240 /** \brief Size of the thread's stack, in bytes. */
242
243 /** \brief Thread errno variable. */
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);
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);
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*/
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*/
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.
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.
598 \param label The string to set as the label.
599
600 \sa thd_get_label
601*/
603
604/** \brief Retrieve the thread's current working directory.
605 \relatesalso kthread_t
606
607 This function retrieves the working directory of a thread. Generally, you
608 will want to use either fs_getwd() or one of the standard C functions for
609 doing this, but this is here in case you need it when the thread isn't
610 active for some reason.
611
612 \param thd The thread to retrieve from.
613
614 \return The thread's working directory.
615
616 \sa thd_set_pd
617*/
618const char *thd_get_pwd(const kthread_t *thd);
619
620/** \brief Set the thread's current working directory.
621 \relatesalso kthread_t
622
623 This function will set the working directory of a thread. Generally, you
624 will want to use either fs_chdir() or the standard C chdir() function to
625 do this, but this is here in case you need to do it while the thread isn't
626 active for some reason.
627
628 \param thd The thread to set the working directory of.
629 \param pwd The directory to set as active.
630
631 \sa thd_get_pwd
632*/
634
635/** \brief Retrieve a pointer to the thread errno.
636 \relatesalso kthread_t
637
638 This function retrieves a pointer to the errno value for the thread. You
639 should generally just use the errno variable to access this.
640
641 \param thd The thread to retrieve from.
642
643 \return A pointer to the thread's errno.
644*/
646
647/** \brief Retrieve a pointer to the thread reent struct.
648 \relatesalso kthread_t
649
650 This function is used to retrieve some internal state that is used by
651 newlib to provide a reentrant libc.
652
653 \param thd The thread to retrieve from.
654
655 \return The thread's reent struct.
656*/
657struct _reent *thd_get_reent(kthread_t *thd);
658
659/** \brief Retrieves the thread's elapsed CPU time
660 \relatesalso kthread_t
661
662 Returns the amount of active CPU time the thread has consumed in
663 nanoseconds.
664
665 \param thd The thead to retrieve the CPU time for.
666
667 \retval Total utilized CPU time in nanoseconds.
668*/
670
671/** \brief Retrieves all thread's elapsed CPU time
672 \relatesalso kthread_t
673
674 Returns the amount of active CPU time all threads have consumed in
675 nanoseconds.
676
677 \retval Total utilized CPU time in nanoseconds.
678*/
680
681/** \brief Change threading modes.
682
683 This function changes the current threading mode of the system.
684 With preemptive threading being the only mode.
685
686 \deprecated
687 This is now deprecated
688
689 \param mode One of the THD_MODE values.
690
691 \return The old mode of the threading system.
692
693 \sa thd_get_mode
694*/
696
697/** \brief Fetch the current threading mode.
698
699 With preemptive threading being the only mode.
700
701 \deprecated
702 This is now deprecated.
703
704 \return The current mode of the threading system.
705
706 \sa thd_set_mode
707*/
708kthread_mode_t thd_get_mode(void) __deprecated;
709
710/** \brief Set the scheduler's frequency.
711
712 Sets the frequency of the scheduler interrupts in hertz.
713
714 \param hertz The new frequency in hertz (1-1000)
715
716 \retval 0 The frequency was updated successfully.
717 \retval -1 \p hertz is invalid.
718
719 \sa thd_get_hz(), HZ
720*/
721int thd_set_hz(unsigned int hertz);
722
723/** \brief Fetch the scheduler's current frequency.
724
725 Queries the scheduler for its interrupt frequency in hertz.
726
727 \return Scheduler frequency in hertz.
728
729 \sa thd_set_hz(), HZ
730*/
731unsigned thd_get_hz(void);
732
733/** \brief Wait for a thread to exit.
734 \relatesalso kthread_t
735
736 This function "joins" a joinable thread. This means effectively that the
737 calling thread blocks until the specified thread completes execution. It is
738 invalid to join a detached thread, only joinable threads may be joined.
739
740 \param thd The joinable thread to join.
741 \param value_ptr A pointer to storage for the thread's return value,
742 or NULL if you don't care about it.
743
744 \return 0 on success, or less than 0 if the thread is
745 non-existent or not joinable.
746
747 \sa thd_detach
748*/
749int thd_join(kthread_t *thd, void **value_ptr);
750
751/** \brief Detach a joinable thread.
752 \relatesalso kthread_t
753
754 This function switches the specified thread's mode from THD_MODE_JOINABLE
755 to THD_MODE_DETACHED. This will ensure that the thread cleans up all of its
756 internal resources when it exits.
757
758 \param thd The joinable thread to detach.
759
760 \return 0 on success or less than 0 if the thread is
761 non-existent or already detached.
762 \sa thd_join()
763*/
765
766/** \brief Iterate all threads and call the passed callback for each
767 \relatesalso kthread_t
768
769 \param cb The callback to call for each thread.
770 If a nonzero value is returned, iteration
771 ceases immediately.
772 \param data User data to be passed to the callback
773
774 \retval 0 or the first nonzero value returned by \p cb.
775
776 \sa thd_pslist
777*/
778int thd_each(int (*cb)(kthread_t *thd, void *user_data), void *data);
779
780/** \brief Print a list of all threads using the given print function.
781
782 Each thread is printed with its address, tid, priority level, flags,
783 it's wait timeout (if sleeping) the amount of cpu time usage in ns
784 (this includes time in IRQs), state, and name.
785
786 In addition a '[system]' item is provided that represents time since
787 initialization not spent in a thread (context switching, updating
788 wait timeouts, etc).
789
790 \param pf The printf-like function to print with.
791
792 \retval 0 On success.
793
794 \sa thd_pslist_queue
795*/
796int thd_pslist(int (*pf)(const char *fmt, ...));
797
798/** \brief Print a list of all queued threads using the given print function.
799
800 \param pf The printf-like function to print with.
801
802 \retval 0 On success.
803
804 \sa thd_pslist
805*/
806int thd_pslist_queue(int (*pf)(const char *fmt, ...));
807
808/** \cond INTERNAL */
809
810/** \brief Initialize the threading system.
811
812 This is normally done for you by default when KOS starts. This will also
813 initialize all the various synchronization primitives.
814 \retval -1 If threads are already initialized.
815 \retval 0 On success.
816 \sa thd_shutdown
817*/
818int thd_init(void);
819
820
821/** \brief Shutdown the threading system.
822
823 This is done for you by the normal shutdown procedure of KOS. This will
824 also shutdown all the various synchronization primitives.
825
826 \sa thd_init
827*/
828void thd_shutdown(void);
829
830/** \endcond */
831
832/** @} */
833
834__END_DECLS
835
836#endif /* __KOS_THREAD_H */
int mode
Definition 2ndmix.c:539
static struct @68 data[BARRIER_COUNT]
Various common macros used throughout the codebase.
void * thd(void *v)
Definition compiler_tls.c:63
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_each(int(*cb)(kthread_t *thd, void *user_data), void *data)
Iterate all threads and call the passed callback for each.
tid_t thd_get_id(const kthread_t *thd)
Retrieve a thread's numeric identifier.
void thd_exit(void *rv) __noreturn
Exit the current thread.
int thd_remove_from_runnable(kthread_t *thd)
Removes a thread from the runnable queue, if it's there.
int prio_t
Priority value type.
Definition thread.h:157
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.
int thd_destroy(kthread_t *thd)
Brutally kill the given thread.
int thd_block_now(irq_context_t *mycxt)
Block the current thread.
unsigned thd_get_hz(void)
Fetch the scheduler's current frequency.
uint64_t thd_get_cpu_time(kthread_t *thd)
Retrieves the thread's elapsed CPU time.
const char * thd_get_pwd(const kthread_t *thd)
Retrieve the thread's current working directory.
void thd_set_pwd(kthread_t *__RESTRICT thd, const char *__RESTRICT pwd)
Set the thread's current working directory.
void thd_add_to_runnable(kthread_t *t, bool front_of_line)
Enqueue a process in the runnable queue.
int(* thd_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.
void thd_schedule_next(kthread_t *thd)
Force a given thread to the front of the queue.
kthread_t * thd_create(bool detach, void *(*routine)(void *param), void *param)
Create a new thread.
int tid_t
Thread ID type.
Definition thread.h:156
void thd_set_label(kthread_t *__RESTRICT thd, const char *__RESTRICT label)
Set the thread's label.
#define KTHREAD_LABEL_SIZE
Size of a kthread's label.
Definition thread.h:106
kthread_state_t
Kernel thread state.
Definition thread.h:146
int thd_join(kthread_t *thd, void **value_ptr)
Wait for a thread to exit.
kthread_mode_t
kthread mode values
Definition thread.h: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.
kthread_t * thd_get_current(void)
Retrieve the current thread's kthread struct.
uint8_t kthread_flags_t
Kernel thread flags type.
Definition thread.h:140
const char * thd_get_label(const kthread_t *thd)
Retrieve the thread's label.
kthread_t * thd_create_ex(const kthread_attr_t *__RESTRICT attr, void *(*routine)(void *param), void *param)
Create a new thread with the specified set of attributes.
int thd_pslist_queue(int(*pf)(const char *fmt,...))
Print a list of all queued threads using the given print function.
prio_t thd_get_prio(const kthread_t *thd)
Retrieve a thread's priority value.
int thd_pslist(int(*pf)(const char *fmt,...))
Print a list of all threads using the given print function.
#define KTHREAD_PWD_SIZE
Size of a kthread's current directory.
Definition thread.h:113
void thd_schedule(bool front_of_line)
Force a thread reschedule.
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:151
@ STATE_READY
Ready to be scheduled.
Definition thread.h:149
@ STATE_FINISHED
Finished execution.
Definition thread.h:152
@ STATE_WAIT
Blocked on a genwait.
Definition thread.h:150
@ STATE_RUNNING
Process is "current".
Definition thread.h:148
@ STATE_ZOMBIE
Waiting to die.
Definition thread.h:147
@ THD_MODE_NONE
Threads not running.
Definition thread.h: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 LIST_HEAD(nmmgr_list, nmmgr_handler) nmmgr_list_t
Name handler list type.
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:165
irq_context_t context
Register store – used to save thread context.
Definition thread.h:167
tid_t tid
Kernel thread id.
Definition thread.h:179
kthread_flags_t flags
Thread flags.
Definition thread.h:188
int thd_errno
Thread errno variable.
Definition thread.h:244
uint64_t wait_timeout
Next scheduled time.
Definition thread.h:217
uint64_t scheduled
time when the thread became active
Definition thread.h:221
uint64_t total
total running CPU time for thread
Definition thread.h:222
kthread_state_t state
Process state.
Definition thread.h:191
void * stack
Thread private stack.
Definition thread.h:238
void * rv
Return value of the thread function.
Definition thread.h:262
void * wait_obj
Generic wait target, if waiting.
Definition thread.h:197
TAILQ_ENTRY(kthread) timerq
Timer queue handle (if applicable).
prio_t prio
Dynamic priority.
Definition thread.h:182
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:203
prio_t real_prio
Static priority: 0..PRIO_MAX (higher means lower priority).
Definition thread.h:185
size_t stack_size
Size of the thread's stack, in bytes.
Definition thread.h:241
static uint32_t ms
Definition timer.c:10
Thread-local storage support.
Common integer types.