KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
thread.h File Reference

Threading support. More...

#include <sys/cdefs.h>
#include <kos/cdefs.h>
#include <kos/tls.h>
#include <arch/irq.h>
#include <sys/queue.h>
#include <sys/reent.h>
#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

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

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.
 

Detailed Description

Threading support.

This file contains the interface to the threading system of KOS. Timer interrupts are used to reschedule threads within the system.

See also
arch/timer.h
kos/genwait.h
kos/mutex.h
kos/once.h
kos/recursive_lock.h
kos/rwsem.h
kos/sem.h
kos/tls.h
Todo
  • Remove deprecated thread mode API
  • Remove global extern pointer to current thread
Author
Megan Potter
Lawrence Sebald
Falco Girgis