KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
Kernel

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_tthd_choose_new (void)
 Find a new thread to swap in.
 
kthread_tthd_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_tthd_create (bool detach, void *(*routine)(void *param), void *param)
 Create a new thread.
 
kthread_tthd_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_tthd_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_tthd_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_tthd_create (bool detach, void *(*routine)(void *param), void *param)
 Create a new thread.
 
kthread_tthd_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_tthd_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.
 

Detailed Description

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.

See also
semaphore_t, mutex_t, kthread_once_t, kthread_key_t, rw_semaphore_t

Macro Definition Documentation

◆ KOS_PID

#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.

◆ KTHREAD_LABEL_SIZE

#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).

◆ KTHREAD_PWD_SIZE

#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).

◆ PRIO_DEFAULT

#define PRIO_DEFAULT   10

Default thread priority.

Threads are created by default with the priority specified.

◆ PRIO_MAX

#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.

◆ THD_DEFAULTS

#define THD_DEFAULTS   0

Defaults: no flags.

◆ THD_DETACHED

#define THD_DETACHED   4

Thread is detached.

◆ THD_OWNS_STACK

#define THD_OWNS_STACK   8

Thread manages stack lifetime.

◆ THD_QUEUED

#define THD_QUEUED   2

Thread is in the run queue.

◆ THD_USER

#define THD_USER   1

Thread runs in user mode.

Typedef Documentation

◆ kthread_flags_t

typedef uint8_t kthread_flags_t

Kernel thread flags type.

Enumeration Type Documentation

◆ kthread_mode_t

kthread mode values

Deprecated
Only preemptive scheduling is still supported!

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.

Deprecated
THD_MODE_PREEMPT 

Preemptive threading mode.

◆ kthread_state_t

Kernel thread state.

Each thread in the system is in exactly one of this set of states.

Enumerator
STATE_ZOMBIE 

Waiting to die.

STATE_RUNNING 

Process is "current".

STATE_READY 

Ready to be scheduled.

STATE_WAIT 

Blocked on a genwait.

STATE_FINISHED 

Finished execution.

Function Documentation

◆ thd_add_to_runnable() [1/2]

void thd_add_to_runnable ( kthread_t * t,
bool front_of_line )
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.

Parameters
tThe thread to queue.
front_of_lineSet 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).
See also
thd_remove_from_runnable

◆ thd_add_to_runnable() [2/2]

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.

Parameters
tThe thread to queue.
front_of_lineSet 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).
See also
thd_remove_from_runnable

◆ thd_block_now()

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.

Parameters
mycxtThe IRQ context of the calling thread.
Returns
Whatever the unblocker deems necessary to return.

◆ thd_by_tid() [1/2]

kthread_t * thd_by_tid ( tid_t tid)
related

Given a thread ID, locates the thread structure.

Parameters
tidThe thread ID to retrieve.
Returns
The thread on success, NULL on failure.

◆ thd_by_tid() [2/2]

kthread_t * thd_by_tid ( tid_t tid)

Given a thread ID, locates the thread structure.

Parameters
tidThe thread ID to retrieve.
Returns
The thread on success, NULL on failure.

◆ thd_choose_new()

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.

Returns
The IRQ context of the thread selected.

◆ thd_create() [1/2]

kthread_t * thd_create ( bool detach,
void *(* routine )(void *param),
void * param )
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.

Parameters
detachSet to true to create a detached thread. Set to false to create a joinable thread.
routineThe function to call in the new thread.
paramA parameter to pass to the function called.
Returns
The new thread on success, NULL on failure.
See also
thd_create_ex, thd_destroy

◆ thd_create() [2/2]

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.

Parameters
detachSet to true to create a detached thread. Set to false to create a joinable thread.
routineThe function to call in the new thread.
paramA parameter to pass to the function called.
Returns
The new thread on success, NULL on failure.
See also
thd_create_ex, thd_destroy

◆ thd_create_ex() [1/2]

kthread_t * thd_create_ex ( const kthread_attr_t *__RESTRICT attr,
void *(* routine )(void *param),
void * param )
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.

Parameters
attrA set of thread attributes for the created thread. Passing NULL will initialize all attributes to their default values.
routineThe function to call in the new thread.
paramA parameter to pass to the function called.
Returns
The new thread on success, NULL on failure.
See also
thd_create, thd_destroy

◆ thd_create_ex() [2/2]

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.

Parameters
attrA set of thread attributes for the created thread. Passing NULL will initialize all attributes to their default values.
routineThe function to call in the new thread.
paramA parameter to pass to the function called.
Returns
The new thread on success, NULL on failure.
See also
thd_create, thd_destroy

◆ thd_destroy() [1/2]

int thd_destroy ( kthread_t * thd)
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.

Warning
You should never call this function on the current thread.
Parameters
thdThe thread to destroy.
Return values
0On success.
See also
thd_create

◆ thd_destroy() [2/2]

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.

Warning
You should never call this function on the current thread.
Parameters
thdThe thread to destroy.
Return values
0On success.
See also
thd_create

◆ thd_detach() [1/2]

int thd_detach ( kthread_t * thd)
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.

Parameters
thdThe joinable thread to detach.
Returns
0 on success or less than 0 if the thread is non-existent or already detached.
See also
thd_join()

◆ thd_detach() [2/2]

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.

Parameters
thdThe joinable thread to detach.
Returns
0 on success or less than 0 if the thread is non-existent or already detached.
See also
thd_join()

◆ thd_each() [1/2]

int thd_each ( int(* cb )(kthread_t *thd, void *user_data),
void * data )
related

Iterate all threads and call the passed callback for each.

Parameters
cbThe callback to call for each thread. If a nonzero value is returned, iteration ceases immediately.
dataUser data to be passed to the callback
Return values
0or the first nonzero value returned by cb.
See also
thd_pslist

◆ thd_each() [2/2]

int thd_each ( int(* cb )(kthread_t *thd, void *user_data),
void * data )

Iterate all threads and call the passed callback for each.

Parameters
cbThe callback to call for each thread. If a nonzero value is returned, iteration ceases immediately.
dataUser data to be passed to the callback
Return values
0or the first nonzero value returned by cb.
See also
thd_pslist

◆ thd_exit()

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.

Parameters
rvThe return value of the thread.

◆ thd_get_cpu_time() [1/2]

uint64_t thd_get_cpu_time ( kthread_t * thd)
related

Retrieves the thread's elapsed CPU time.

Returns the amount of active CPU time the thread has consumed in nanoseconds.

Warning
The implementation uses perf_cntr_timer_ns() internally when maintaining this CPU time, so disabling or clearing the nanosecond timer will interfere with this time keeping.
Parameters
thdThe thead to retrieve the CPU time for
Return values
Totalutilized CPU time in nanoseconds OR 0 if the nanosecond timer of the performance counters has been disturbed.

◆ thd_get_cpu_time() [2/2]

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.

Warning
The implementation uses perf_cntr_timer_ns() internally when maintaining this CPU time, so disabling or clearing the nanosecond timer will interfere with this time keeping.
Parameters
thdThe thead to retrieve the CPU time for
Return values
Totalutilized CPU time in nanoseconds OR 0 if the nanosecond timer of the performance counters has been disturbed.

◆ thd_get_current() [1/2]

kthread_t * thd_get_current ( void )
related

Retrieve the current thread's kthread struct.

Returns
The current thread's structure.

◆ thd_get_current() [2/2]

kthread_t * thd_get_current ( void )

Retrieve the current thread's kthread struct.

Returns
The current thread's structure.

◆ thd_get_errno() [1/2]

int * thd_get_errno ( kthread_t * thd)
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.

Parameters
thdThe thread to retrieve from.
Returns
A pointer to the thread's errno.

◆ thd_get_errno() [2/2]

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.

Parameters
thdThe thread to retrieve from.
Returns
A pointer to the thread's errno.

◆ thd_get_hz()

unsigned thd_get_hz ( void )

Fetch the scheduler's current frequency.

Queries the scheduler for its interrupt frequency in hertz.

Returns
Scheduler frequency in hertz.
See also
thd_set_hz(), HZ

◆ thd_get_label() [1/2]

const char * thd_get_label ( kthread_t * thd)
related

Retrieve the thread's label.

Parameters
thdThe thread to retrieve from.
Returns
The human-readable label of the thread.
See also
thd_set_label

◆ thd_get_label() [2/2]

const char * thd_get_label ( kthread_t * thd)

Retrieve the thread's label.

Parameters
thdThe thread to retrieve from.
Returns
The human-readable label of the thread.
See also
thd_set_label

◆ thd_get_mode()

kthread_mode_t thd_get_mode ( void )

Fetch the current threading mode.

With preemptive threading being the only mode.

Deprecated
This is now deprecated.
Returns
The current mode of the threading system.
See also
thd_set_mode

◆ thd_get_pwd() [1/2]

const char * thd_get_pwd ( kthread_t * thd)
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.

Parameters
thdThe thread to retrieve from.
Returns
The thread's working directory.
See also
thd_set_pd

◆ thd_get_pwd() [2/2]

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.

Parameters
thdThe thread to retrieve from.
Returns
The thread's working directory.
See also
thd_set_pd

◆ thd_get_reent() [1/2]

struct _reent * thd_get_reent ( kthread_t * thd)
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.

Parameters
thdThe thread to retrieve from.
Returns
The thread's reent struct.

◆ thd_get_reent() [2/2]

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.

Parameters
thdThe thread to retrieve from.
Returns
The thread's reent struct.

◆ thd_join() [1/2]

int thd_join ( kthread_t * thd,
void ** value_ptr )
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.

Parameters
thdThe joinable thread to join.
value_ptrA pointer to storage for the thread's return value, or NULL if you don't care about it.
Returns
0 on success, or less than 0 if the thread is non-existent or not joinable.
See also
thd_detach

◆ thd_join() [2/2]

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.

Parameters
thdThe joinable thread to join.
value_ptrA pointer to storage for the thread's return value, or NULL if you don't care about it.
Returns
0 on success, or less than 0 if the thread is non-existent or not joinable.
See also
thd_detach

◆ thd_pass()

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.

◆ thd_pslist()

int thd_pslist ( int(* pf )(const char *fmt,...))

Print a list of all threads using the given print function.

Parameters
pfThe printf-like function to print with.
Return values
0On success.
See also
thd_pslist_queue

◆ thd_pslist_queue()

int thd_pslist_queue ( int(* pf )(const char *fmt,...))

Print a list of all queued threads using the given print function.

Parameters
pfThe printf-like function to print with.
Return values
0On success.
See also
thd_pslist

◆ thd_remove_from_runnable() [1/2]

int thd_remove_from_runnable ( kthread_t * thd)
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.

Parameters
thdThe thread to remove from the runnable queue.
Return values
0On success, or if the thread isn't runnable.
See also
thd_add_to_runnable

◆ thd_remove_from_runnable() [2/2]

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.

Parameters
thdThe thread to remove from the runnable queue.
Return values
0On success, or if the thread isn't runnable.
See also
thd_add_to_runnable

◆ thd_schedule()

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.

Parameters
front_of_lineSet to false, unless you have a good reason not to.
nowSet to 0, unless you have a good reason not to.
See also
thd_schedule_next

◆ thd_schedule_next() [1/2]

void thd_schedule_next ( kthread_t * thd)
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).

Parameters
thdThe thread to schedule next.

◆ thd_schedule_next() [2/2]

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).

Parameters
thdThe thread to schedule next.

◆ thd_set_hz()

int thd_set_hz ( unsigned int hertz)

Set the scheduler's frequency.

Sets the frequency of the scheduler interrupts in hertz.

Parameters
hertzThe new frequency in hertz (1-1000)
Return values
0The frequency was updated successfully.
-1hertz is invalid.
See also
thd_get_hz(), HZ

◆ thd_set_label() [1/2]

void thd_set_label ( kthread_t * thd,
const char *__RESTRICT label )
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().

Parameters
thdThe thread to set the label of.
labelThe string to set as the label.
See also
thd_get_label

◆ thd_set_label() [2/2]

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().

Parameters
thdThe thread to set the label of.
labelThe string to set as the label.
See also
thd_get_label

◆ thd_set_mode()

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.

Deprecated
This is now deprecated
Parameters
modeOne of the THD_MODE values.
Returns
The old mode of the threading system.
See also
thd_get_mode

◆ thd_set_prio() [1/2]

int thd_set_prio ( kthread_t * thd,
prio_t prio )
related

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.

Parameters
thdThe thread to change the priority of.
prioThe priority value to assign to the thread.
Return values
0On success.
-1thd is NULL.
-2prio requested was out of range.

◆ thd_set_prio() [2/2]

int thd_set_prio ( kthread_t * thd,
prio_t prio )

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.

Parameters
thdThe thread to change the priority of.
prioThe priority value to assign to the thread.
Return values
0On success.
-1thd is NULL.
-2prio requested was out of range.

◆ thd_set_pwd() [1/2]

void thd_set_pwd ( kthread_t * thd,
const char *__RESTRICT pwd )
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.

Parameters
thdThe thread to set the working directory of.
pwdThe directory to set as active.
See also
thd_get_pwd

◆ thd_set_pwd() [2/2]

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.

Parameters
thdThe thread to set the working directory of.
pwdThe directory to set as active.
See also
thd_get_pwd

◆ thd_sleep()

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.

Note
When ms is given a value of 0, this is equivalent to thd_pass().
Parameters
msThe number of milliseconds to sleep.