KallistiOS git master
Independent SDK for the Sega Dreamcast
|
KOS Native Kernel Threading API More...
Files | |
file | cond.h |
Condition variables. | |
file | genwait.h |
Generic wait system. | |
file | mutex.h |
Mutual exclusion locks. | |
file | once.h |
Dynamic package initialization. | |
file | oneshot_timer.h |
One-shot timer support. | |
file | recursive_lock.h |
Definitions for a recursive mutex. | |
file | rwsem.h |
Definition for a reader/writer semaphore. | |
file | sem.h |
Semaphores. | |
file | thread.h |
Threading support. | |
file | tls.h |
Thread-local storage support. | |
file | worker_thread.h |
Threaded worker support. | |
file | spinlock.h |
Simple locking. | |
Data Structures | |
struct | tcbhead_t |
Thread Control Block Header. More... | |
struct | kthread_t |
Structure describing one running thread. More... | |
struct | kthread_attr_t |
Thread creation attributes. More... | |
Macros | |
#define | KOS_PID 1 |
Process ID. | |
#define | PRIO_MAX 4096 |
Maximal thread priority. | |
#define | PRIO_DEFAULT 10 |
Default thread priority. | |
#define | KTHREAD_LABEL_SIZE 256 |
Size of a kthread's label. | |
#define | KTHREAD_PWD_SIZE 256 |
Size of a kthread's current directory. | |
Typedefs | |
typedef uint8_t | kthread_flags_t |
Kernel thread flags type. | |
Enumerations | |
enum | kthread_state_t { STATE_ZOMBIE = 0x0000 , STATE_RUNNING = 0x0001 , STATE_READY = 0x0002 , STATE_WAIT = 0x0003 , STATE_FINISHED = 0x0004 } |
Kernel thread state. More... | |
enum | kthread_mode_t { THD_MODE_NONE = -1 , THD_MODE_COOP = 0 , THD_MODE_PREEMPT = 1 } |
kthread mode values More... | |
Functions | |
int | thd_block_now (irq_context_t *mycxt) |
Block the current thread. | |
irq_context_t * | thd_choose_new (void) |
Find a new thread to swap in. | |
kthread_t * | thd_by_tid (tid_t tid) |
Given a thread ID, locates the thread structure. | |
void | thd_add_to_runnable (kthread_t *t, bool front_of_line) |
Enqueue a process in the runnable queue. | |
int | thd_remove_from_runnable (kthread_t *thd) |
Removes a thread from the runnable queue, if it's there. | |
kthread_t * | thd_create (bool detach, void *(*routine)(void *param), void *param) |
Create a new thread. | |
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_destroy (kthread_t *thd) |
Brutally kill the given thread. | |
void | thd_exit (void *rv) __noreturn |
Exit the current thread. | |
void | thd_schedule (bool front_of_line, uint64_t now) |
Force a thread reschedule. | |
void | thd_schedule_next (kthread_t *thd) |
Force a given thread to the front of the queue. | |
void | thd_pass (void) |
Throw away the current thread's timeslice. | |
void | thd_sleep (unsigned ms) |
Sleep for a given number of milliseconds. | |
int | thd_set_prio (kthread_t *thd, prio_t prio) |
Set a thread's priority value. | |
kthread_t * | thd_get_current (void) |
Retrieve the current thread's kthread struct. | |
const char * | thd_get_label (kthread_t *thd) |
Retrieve the thread's label. | |
void | thd_set_label (kthread_t *thd, const char *__RESTRICT label) |
Set the thread's label. | |
const char * | thd_get_pwd (kthread_t *thd) |
Retrieve the thread's current working directory. | |
void | thd_set_pwd (kthread_t *thd, const char *__RESTRICT pwd) |
Set the thread's current working directory. | |
int * | thd_get_errno (kthread_t *thd) |
Retrieve a pointer to the thread errno. | |
struct _reent * | thd_get_reent (kthread_t *thd) |
Retrieve a pointer to the thread reent struct. | |
uint64_t | thd_get_cpu_time (kthread_t *thd) |
Retrieves the thread's elapsed CPU time. | |
int | thd_set_mode (kthread_mode_t mode) __deprecated |
Change threading modes. | |
kthread_mode_t | thd_get_mode (void) __deprecated |
Fetch the current threading mode. | |
int | thd_set_hz (unsigned int hertz) |
Set the scheduler's frequency. | |
unsigned | thd_get_hz (void) |
Fetch the scheduler's current frequency. | |
int | thd_join (kthread_t *thd, void **value_ptr) |
Wait for a thread to exit. | |
int | thd_detach (kthread_t *thd) |
Detach a joinable thread. | |
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_pslist (int(*pf)(const char *fmt,...)) |
Print a list of all threads using the given print function. | |
int | thd_pslist_queue (int(*pf)(const char *fmt,...)) |
Print a list of all queued threads using the given print function. | |
kthread_t * | thd_by_tid (tid_t tid) |
Given a thread ID, locates the thread structure. | |
void | thd_add_to_runnable (kthread_t *t, bool front_of_line) |
Enqueue a process in the runnable queue. | |
int | thd_remove_from_runnable (kthread_t *thd) |
Removes a thread from the runnable queue, if it's there. | |
kthread_t * | thd_create (bool detach, void *(*routine)(void *param), void *param) |
Create a new thread. | |
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_destroy (kthread_t *thd) |
Brutally kill the given thread. | |
void | thd_schedule_next (kthread_t *thd) |
Force a given thread to the front of the queue. | |
int | thd_set_prio (kthread_t *thd, prio_t prio) |
Set a thread's priority value. | |
kthread_t * | thd_get_current (void) |
Retrieve the current thread's kthread struct. | |
const char * | thd_get_label (kthread_t *thd) |
Retrieve the thread's label. | |
void | thd_set_label (kthread_t *thd, const char *__RESTRICT label) |
Set the thread's label. | |
const char * | thd_get_pwd (kthread_t *thd) |
Retrieve the thread's current working directory. | |
void | thd_set_pwd (kthread_t *thd, const char *__RESTRICT pwd) |
Set the thread's current working directory. | |
int * | thd_get_errno (kthread_t *thd) |
Retrieve a pointer to the thread errno. | |
struct _reent * | thd_get_reent (kthread_t *thd) |
Retrieve a pointer to the thread reent struct. | |
uint64_t | thd_get_cpu_time (kthread_t *thd) |
Retrieves the thread's elapsed CPU time. | |
int | thd_join (kthread_t *thd, void **value_ptr) |
Wait for a thread to exit. | |
int | thd_detach (kthread_t *thd) |
Detach a joinable thread. | |
int | thd_each (int(*cb)(kthread_t *thd, void *user_data), void *data) |
Iterate all threads and call the passed callback for each. | |
Thread flag values | |
Flags for kthread_flags_t These are possible values for the flags field on the kthread_t structure. These can be ORed together. | |
#define | THD_DEFAULTS 0 |
Defaults: no flags. | |
#define | THD_USER 1 |
Thread runs in user mode. | |
#define | THD_QUEUED 2 |
Thread is in the run queue. | |
#define | THD_DETACHED 4 |
Thread is detached. | |
#define | THD_OWNS_STACK 8 |
Thread manages stack lifetime. | |
KOS Native Kernel Threading API
The thread scheduler itself is a relatively simplistic priority scheduler. There is no provision for priorities to erode over time, so keep that in mind. That practically means that if you have 2 high priority threads that are always runnable and one low priority thread that is always runnable, the low priority thread will never actually run (since it will never get to the front of the run queue because of the high priority threads).
The scheduler supports two distinct types of threads: joinable and detached threads. A joinable thread is one that can return a value to the creating thread (or for that matter, any other thread that wishes to join it). A detached thread is one that is completely detached from the rest of the system and cannot return values by "normal" means. Detached threads automatically clean up all of the internal resources associated with the thread when it exits. Joinable threads, on the other hand, must keep some state available for the ability to return values. To make sure that all memory allocated by the thread's internal structures gets freed, you must either join with the thread (with thd_join()) or detach it (with thd_detach()). The old KOS threading system only had what would be considered detached threads.
#define KOS_PID 1 |
Process ID.
This macro defines the single process ID that encompasses all of KOS and the running application along with all of its threads.
#define KTHREAD_LABEL_SIZE 256 |
Size of a kthread's label.
Maximum number of characters in a thread's label or name (including NULL terminator).
#define KTHREAD_PWD_SIZE 256 |
Size of a kthread's current directory.
Maximum number of characters in a thread's current working directory (including NULL terminator).
#define PRIO_DEFAULT 10 |
Default thread priority.
Threads are created by default with the priority specified.
#define PRIO_MAX 4096 |
Maximal thread priority.
This macro defines the maximum value for a thread's priority. Note that the larger this number, the lower the priority of the thread.
#define THD_DEFAULTS 0 |
Defaults: no flags.
#define THD_DETACHED 4 |
Thread is detached.
#define THD_OWNS_STACK 8 |
Thread manages stack lifetime.
#define THD_QUEUED 2 |
Thread is in the run queue.
#define THD_USER 1 |
Thread runs in user mode.
typedef uint8_t kthread_flags_t |
Kernel thread flags type.
enum kthread_mode_t |
kthread mode values
The threading system will always be in one of the following modes. This represents either pre-emptive scheduling or an un-initialized state.
Enumerator | |
---|---|
THD_MODE_NONE | Threads not running. |
THD_MODE_COOP | Cooperative mode. |
THD_MODE_PREEMPT | Preemptive threading mode. |
enum kthread_state_t |
|
related |
Enqueue a process in the runnable queue.
This function adds a thread to the runnable queue after the process group of the same priority if front_of_line is zero, otherwise queues it at the front of its priority group. Generally, you will not have to do this manually.
t | The thread to queue. |
front_of_line | Set to true to put this thread in front of other threads of the same priority, false to put it behind the other threads (normal behavior). |
void thd_add_to_runnable | ( | kthread_t * | t, |
bool | front_of_line ) |
Enqueue a process in the runnable queue.
This function adds a thread to the runnable queue after the process group of the same priority if front_of_line is zero, otherwise queues it at the front of its priority group. Generally, you will not have to do this manually.
t | The thread to queue. |
front_of_line | Set to true to put this thread in front of other threads of the same priority, false to put it behind the other threads (normal behavior). |
int thd_block_now | ( | irq_context_t * | mycxt | ) |
Block the current thread.
Blocks the calling thread and performs a reschedule as if a context switch timer had been executed. This is useful for, e.g., blocking on sync primitives. The param 'mycxt' should point to the calling thread's context block. This is implemented in arch-specific code.
The meaningfulness of the return value depends on whether the unblocker set a return value or not.
mycxt | The IRQ context of the calling thread. |
Given a thread ID, locates the thread structure.
tid | The thread ID to retrieve. |
Given a thread ID, locates the thread structure.
tid | The thread ID to retrieve. |
irq_context_t * thd_choose_new | ( | void | ) |
Find a new thread to swap in.
This function looks at the state of the system and returns a new thread context to swap in. This is called from thd_block_now() and from the preemptive context switcher. Note that thd_current might be NULL on entering this function, if the caller blocked itself.
It is assumed that by the time this returns, the irq_srt_addr and thd_current will be updated.
|
related |
Create a new thread.
This function creates a new kernel thread with default parameters to run the given routine. The thread will terminate and clean up resources when the routine completes if the thread is created detached, otherwise you must join the thread with thd_join() to clean up after it.
detach | Set to true to create a detached thread. Set to false to create a joinable thread. |
routine | The function to call in the new thread. |
param | A parameter to pass to the function called. |
kthread_t * thd_create | ( | bool | detach, |
void *(* | routine )(void *param), | ||
void * | param ) |
Create a new thread.
This function creates a new kernel thread with default parameters to run the given routine. The thread will terminate and clean up resources when the routine completes if the thread is created detached, otherwise you must join the thread with thd_join() to clean up after it.
detach | Set to true to create a detached thread. Set to false to create a joinable thread. |
routine | The function to call in the new thread. |
param | A parameter to pass to the function called. |
|
related |
Create a new thread with the specified set of attributes.
This function creates a new kernel thread with the specified set of parameters to run the given routine.
attr | A set of thread attributes for the created thread. Passing NULL will initialize all attributes to their default values. |
routine | The function to call in the new thread. |
param | A parameter to pass to the function called. |
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.
This function creates a new kernel thread with the specified set of parameters to run the given routine.
attr | A set of thread attributes for the created thread. Passing NULL will initialize all attributes to their default values. |
routine | The function to call in the new thread. |
param | A parameter to pass to the function called. |
|
related |
Brutally kill the given thread.
This function kills the given thread, removing it from the execution chain, cleaning up thread-local data and other internal structures. In general, you shouldn't call this function at all.
thd | The thread to destroy. |
0 | On success. |
int thd_destroy | ( | kthread_t * | thd | ) |
Brutally kill the given thread.
This function kills the given thread, removing it from the execution chain, cleaning up thread-local data and other internal structures. In general, you shouldn't call this function at all.
thd | The thread to destroy. |
0 | On success. |
|
related |
Detach a joinable thread.
This function switches the specified thread's mode from THD_MODE_JOINABLE to THD_MODE_DETACHED. This will ensure that the thread cleans up all of its internal resources when it exits.
thd | The joinable thread to detach. |
int thd_detach | ( | kthread_t * | thd | ) |
Detach a joinable thread.
This function switches the specified thread's mode from THD_MODE_JOINABLE to THD_MODE_DETACHED. This will ensure that the thread cleans up all of its internal resources when it exits.
thd | The joinable thread to detach. |
|
related |
Iterate all threads and call the passed callback for each.
cb | The callback to call for each thread. If a nonzero value is returned, iteration ceases immediately. |
data | User data to be passed to the callback |
0 | or the first nonzero value returned by cb . |
int thd_each | ( | int(* | cb )(kthread_t *thd, void *user_data), |
void * | data ) |
Iterate all threads and call the passed callback for each.
cb | The callback to call for each thread. If a nonzero value is returned, iteration ceases immediately. |
data | User data to be passed to the callback |
0 | or the first nonzero value returned by cb . |
void thd_exit | ( | void * | rv | ) |
Exit the current thread.
This function ends the execution of the current thread, removing it from all execution queues. This function will never return to the thread. Returning from the thread's function is equivalent to calling this function.
rv | The return value of the thread. |
|
related |
Retrieves the thread's elapsed CPU time.
Returns the amount of active CPU time the thread has consumed in nanoseconds.
thd | The thead to retrieve the CPU time for |
Total | utilized CPU time in nanoseconds OR 0 if the nanosecond timer of the performance counters has been disturbed. |
uint64_t thd_get_cpu_time | ( | kthread_t * | thd | ) |
Retrieves the thread's elapsed CPU time.
Returns the amount of active CPU time the thread has consumed in nanoseconds.
thd | The thead to retrieve the CPU time for |
Total | utilized CPU time in nanoseconds OR 0 if the nanosecond timer of the performance counters has been disturbed. |
|
related |
Retrieve the current thread's kthread struct.
kthread_t * thd_get_current | ( | void | ) |
Retrieve the current thread's kthread struct.
|
related |
Retrieve a pointer to the thread errno.
This function retrieves a pointer to the errno value for the thread. You should generally just use the errno variable to access this.
thd | The thread to retrieve from. |
int * thd_get_errno | ( | kthread_t * | thd | ) |
Retrieve a pointer to the thread errno.
This function retrieves a pointer to the errno value for the thread. You should generally just use the errno variable to access this.
thd | The thread to retrieve from. |
unsigned thd_get_hz | ( | void | ) |
Fetch the scheduler's current frequency.
Queries the scheduler for its interrupt frequency in hertz.
|
related |
Retrieve the thread's label.
thd | The thread to retrieve from. |
const char * thd_get_label | ( | kthread_t * | thd | ) |
Retrieve the thread's label.
thd | The thread to retrieve from. |
kthread_mode_t thd_get_mode | ( | void | ) |
Fetch the current threading mode.
With preemptive threading being the only mode.
|
related |
Retrieve the thread's current working directory.
This function retrieves the working directory of a thread. Generally, you will want to use either fs_getwd() or one of the standard C functions for doing this, but this is here in case you need it when the thread isn't active for some reason.
thd | The thread to retrieve from. |
const char * thd_get_pwd | ( | kthread_t * | thd | ) |
Retrieve the thread's current working directory.
This function retrieves the working directory of a thread. Generally, you will want to use either fs_getwd() or one of the standard C functions for doing this, but this is here in case you need it when the thread isn't active for some reason.
thd | The thread to retrieve from. |
|
related |
Retrieve a pointer to the thread reent struct.
This function is used to retrieve some internal state that is used by newlib to provide a reentrant libc.
thd | The thread to retrieve from. |
struct _reent * thd_get_reent | ( | kthread_t * | thd | ) |
Retrieve a pointer to the thread reent struct.
This function is used to retrieve some internal state that is used by newlib to provide a reentrant libc.
thd | The thread to retrieve from. |
|
related |
Wait for a thread to exit.
This function "joins" a joinable thread. This means effectively that the calling thread blocks until the speified thread completes execution. It is invalid to join a detached thread, only joinable threads may be joined.
thd | The joinable thread to join. |
value_ptr | A pointer to storage for the thread's return value, or NULL if you don't care about it. |
int thd_join | ( | kthread_t * | thd, |
void ** | value_ptr ) |
Wait for a thread to exit.
This function "joins" a joinable thread. This means effectively that the calling thread blocks until the speified thread completes execution. It is invalid to join a detached thread, only joinable threads may be joined.
thd | The joinable thread to join. |
value_ptr | A pointer to storage for the thread's return value, or NULL if you don't care about it. |
void thd_pass | ( | void | ) |
Throw away the current thread's timeslice.
This function manually yields the current thread's timeslice to the system, forcing a reschedule to occur.
int thd_pslist | ( | int(* | pf )(const char *fmt,...) | ) |
Print a list of all threads using the given print function.
pf | The printf-like function to print with. |
0 | On success. |
int thd_pslist_queue | ( | int(* | pf )(const char *fmt,...) | ) |
Print a list of all queued threads using the given print function.
pf | The printf-like function to print with. |
0 | On success. |
|
related |
Removes a thread from the runnable queue, if it's there.
This function removes a thread from the runnable queue, if it is currently in that queue. Generally, you shouldn't have to do this manually, as waiting on synchronization primitives and the like will do this for you if needed.
thd | The thread to remove from the runnable queue. |
0 | On success, or if the thread isn't runnable. |
int thd_remove_from_runnable | ( | kthread_t * | thd | ) |
Removes a thread from the runnable queue, if it's there.
This function removes a thread from the runnable queue, if it is currently in that queue. Generally, you shouldn't have to do this manually, as waiting on synchronization primitives and the like will do this for you if needed.
thd | The thread to remove from the runnable queue. |
0 | On success, or if the thread isn't runnable. |
void thd_schedule | ( | bool | front_of_line, |
uint64_t | now ) |
Force a thread reschedule.
This function is the thread scheduler, and is generally called from a timer interrupt. You will most likely never have a reason to call this function directly.
For most cases, you'll want to set front_of_line to zero, but read the comments in kernel/thread/thread.c for more info, especially if you need to guarantee low latencies. This function just updates irq_srt_addr and thd_current. Set 'now' to non-zero if you want to use a particular system time for checking timeouts.
front_of_line | Set to false, unless you have a good reason not to. |
now | Set to 0, unless you have a good reason not to. |
|
related |
Force a given thread to the front of the queue.
This function promotes the given thread to be the next one that will be swapped in by the scheduler. This function is only callable inside an interrupt context (it simply returns otherwise).
thd | The thread to schedule next. |
void thd_schedule_next | ( | kthread_t * | thd | ) |
Force a given thread to the front of the queue.
This function promotes the given thread to be the next one that will be swapped in by the scheduler. This function is only callable inside an interrupt context (it simply returns otherwise).
thd | The thread to schedule next. |
int thd_set_hz | ( | unsigned int | hertz | ) |
Set the scheduler's frequency.
Sets the frequency of the scheduler interrupts in hertz.
hertz | The new frequency in hertz (1-1000) |
0 | The frequency was updated successfully. |
-1 | hertz is invalid. |
|
related |
Set the thread's label.
This function sets the label of a thread, which is simply a human-readable string that is used to identify the thread. These labels aren't used for anything internally, and you can give them any label you want. These are mainly seen in the printouts from thd_pslist() or thd_pslist_queue().
thd | The thread to set the label of. |
label | The string to set as the label. |
void thd_set_label | ( | kthread_t * | thd, |
const char *__RESTRICT | label ) |
Set the thread's label.
This function sets the label of a thread, which is simply a human-readable string that is used to identify the thread. These labels aren't used for anything internally, and you can give them any label you want. These are mainly seen in the printouts from thd_pslist() or thd_pslist_queue().
thd | The thread to set the label of. |
label | The string to set as the label. |
int thd_set_mode | ( | kthread_mode_t | mode | ) |
Change threading modes.
This function changes the current threading mode of the system. With preemptive threading being the only mode.
mode | One of the THD_MODE values. |
Set a thread's priority value.
This function is used to change the priority value of a thread. If the thread is scheduled already, it will be rescheduled with the new priority value.
thd | The thread to change the priority of. |
prio | The priority value to assign to the thread. |
0 | On success. |
-1 | thd is NULL. |
-2 | prio requested was out of range. |
Set a thread's priority value.
This function is used to change the priority value of a thread. If the thread is scheduled already, it will be rescheduled with the new priority value.
thd | The thread to change the priority of. |
prio | The priority value to assign to the thread. |
0 | On success. |
-1 | thd is NULL. |
-2 | prio requested was out of range. |
|
related |
Set the thread's current working directory.
This function will set the working directory of a thread. Generally, you will want to use either fs_chdir() or the standard C chdir() function to do this, but this is here in case you need to do it while the thread isn't active for some reason.
thd | The thread to set the working directory of. |
pwd | The directory to set as active. |
void thd_set_pwd | ( | kthread_t * | thd, |
const char *__RESTRICT | pwd ) |
Set the thread's current working directory.
This function will set the working directory of a thread. Generally, you will want to use either fs_chdir() or the standard C chdir() function to do this, but this is here in case you need to do it while the thread isn't active for some reason.
thd | The thread to set the working directory of. |
pwd | The directory to set as active. |
void thd_sleep | ( | unsigned | ms | ) |
Sleep for a given number of milliseconds.
This function puts the current thread to sleep for the specified amount of time. The thread will be removed from the runnable queue until the given number of milliseconds passes. That is to say that the thread will sleep for at least the given number of milliseconds. If another thread is running, it will likely sleep longer.
ms
is given a value of 0
, this is equivalent to thd_pass().ms | The number of milliseconds to sleep. |