PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches

Thread of execution. More...

Collaboration diagram for Threads:

Detailed Description

Thread of execution.

Data Structures

struct  thread_t
 Thread of execution structure. More...
 

Typedefs

typedef void(* thread_kernel_entry_t) (void *arg)
 Kernel thread entry point function type.
 

Enumerations

enum  thread_state_t {
  THREAD_PARKED = 0 , THREAD_ACTIVE , THREAD_PRE_BLOCK , THREAD_BLOCKED ,
  THREAD_UNBLOCKING , THREAD_DYING
}
 Thread state enum. More...
 

Functions

thread_tthread_new (process_t *process)
 Creates a new thread structure.
 
void thread_free (thread_t *thread)
 Frees a thread structure.
 
tid_t thread_kernel_create (thread_kernel_entry_t entry, void *arg)
 Creates a new thread that runs in kernel mode and submits it to the scheduler.
 
static thread_tthread_current (void)
 Retrieves the currently running thread.
 
static thread_tthread_current_unsafe (void)
 Retrieves the currently running thread without disabling interrupts.
 
static thread_tthread_idle (void)
 Retrieves the idle thread for the current CPU.
 
static thread_tthread_idle_unsafe (void)
 Retrieves the idle thread for the current CPU without disabling interrupts.
 
void thread_save (thread_t *thread, const interrupt_frame_t *frame)
 Save state to a thread.
 
void thread_load (thread_t *thread, interrupt_frame_t *frame)
 Load state from a thread.
 
bool thread_is_note_pending (thread_t *thread)
 Check if a thread has a note pending.
 
uint64_t thread_send_note (thread_t *thread, const char *string)
 Send a note to a thread.
 
uint64_t thread_copy_from_user (thread_t *thread, void *dest, const void *userSrc, uint64_t length)
 Safely copy data from user space.
 
uint64_t thread_copy_to_user (thread_t *thread, void *userDest, const void *src, uint64_t length)
 Safely copy data to user space.
 
uint64_t thread_copy_from_user_terminated (thread_t *thread, const void *userArray, const void *terminator, uint8_t objectSize, uint64_t maxCount, void **outArray, uint64_t *outCount)
 Safely copy a null-terminated array of objects from user space.
 
uint64_t thread_copy_from_user_string (thread_t *thread, char *dest, const char *userSrc, uint64_t size)
 Safely copy a string from user space.
 
uint64_t thread_copy_from_user_pathname (thread_t *thread, pathname_t *pathname, const char *userPath)
 Safely copy a string from user space and use it to initialize a pathname.
 
uint64_t thread_copy_from_user_string_array (thread_t *thread, const char **user, char ***out, uint64_t *outAmount)
 Safely copy a null-terminated array of strings and their contents from user space into a string vector.
 
uint64_t thread_load_atomic_from_user (thread_t *thread, atomic_uint64_t *userObj, uint64_t *outValue)
 Atomically load a 64-bit value from a user-space atomic variable.
 
_NORETURN void thread_jump (thread_t *thread)
 Jump to a thread by calling thread_load() and then loading its interrupt frame.
 

Typedef Documentation

◆ thread_kernel_entry_t

typedef void(* thread_kernel_entry_t) (void *arg)

Kernel thread entry point function type.

Definition at line 110 of file thread.h.

Enumeration Type Documentation

◆ thread_state_t

Thread state enum.

Enumerator
THREAD_PARKED 

Is doing nothing, not in a queue, not blocking, think of it as "other".

THREAD_ACTIVE 

Is either running or ready to run.

THREAD_PRE_BLOCK 

Has started the process of blocking but has not yet been given to a owner cpu.

THREAD_BLOCKED 

Is blocking and waiting in one or multiple wait queues.

THREAD_UNBLOCKING 

Has started unblocking, used to prevent the same thread being unblocked multiple times.

THREAD_DYING 

The thread is currently dying, it will be freed by the scheduler once its invoked.

Definition at line 34 of file thread.h.

Function Documentation

◆ thread_new()

thread_t * thread_new ( process_t process)

Creates a new thread structure.

Does not push the created thread to the scheduler or similar, merely handling allocation and initialization.

Parameters
processThe parent process that the thread will execute within.
Returns
On success, returns the newly created thread. On failure, returns NULL and errno is set.

Definition at line 50 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_free()

void thread_free ( thread_t thread)

Frees a thread structure.

Parameters
threadThe thread to be freed.

Definition at line 111 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_kernel_create()

tid_t thread_kernel_create ( thread_kernel_entry_t  entry,
void *  arg 
)

Creates a new thread that runs in kernel mode and submits it to the scheduler.

Parameters
entryThe entry point function for the thread.
argAn argument to pass to the entry point function.
Returns
On success, returns the newly created thread ID. On failure, returns ERR and errno is set.

Definition at line 126 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_current()

static thread_t * thread_current ( void  )
inlinestatic

Retrieves the currently running thread.

Returns
The currently running thread.

Definition at line 126 of file thread.h.

Here is the caller graph for this function:

◆ thread_current_unsafe()

static thread_t * thread_current_unsafe ( void  )
inlinestatic

Retrieves the currently running thread without disabling interrupts.

Returns
The currently running thread.

Definition at line 137 of file thread.h.

Here is the caller graph for this function:

◆ thread_idle()

static thread_t * thread_idle ( void  )
inlinestatic

Retrieves the idle thread for the current CPU.

Returns
The idle thread for the current CPU.

Definition at line 147 of file thread.h.

Here is the caller graph for this function:

◆ thread_idle_unsafe()

static thread_t * thread_idle_unsafe ( void  )
inlinestatic

Retrieves the idle thread for the current CPU without disabling interrupts.

Returns
The idle thread for the current CPU.

Definition at line 158 of file thread.h.

◆ thread_save()

void thread_save ( thread_t thread,
const interrupt_frame_t frame 
)

Save state to a thread.

Parameters
threadThe destination thread where the state will be saved.
frameThe source frame..

Definition at line 153 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_load()

void thread_load ( thread_t thread,
interrupt_frame_t frame 
)

Load state from a thread.

Will retrieve the interrupt frame and setup the CPU with the threads contexts/data.

Parameters
threadThe source thread to load state from.
frameThe destination interrupt frame.

Definition at line 160 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_is_note_pending()

bool thread_is_note_pending ( thread_t thread)

Check if a thread has a note pending.

Parameters
threadThe thread to query.
Returns
True if there is a note pending, false otherwise.

Definition at line 171 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_send_note()

uint64_t thread_send_note ( thread_t thread,
const char *  string 
)

Send a note to a thread.

This function should always be used over the note_queue_push() function, as it performs additional checks, like unblocking the thread to notify it of the received note.

Parameters
threadThe destination thread.
stringThe note string to send, should be a null-terminated string.
Returns
On success, 0. On failure, ERR and errno is set to:

Definition at line 176 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_copy_from_user()

uint64_t thread_copy_from_user ( thread_t thread,
void *  dest,
const void *  userSrc,
uint64_t  length 
)

Safely copy data from user space.

Will pin the user pages in memory while performing the copy and expand the user stack if necessary.

Parameters
threadThe thread performing the operation.
destThe destination buffer in kernel space.
userSrcThe source buffer in user space.
lengthThe number of bytes to copy.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 202 of file thread.c.

Here is the call graph for this function:

◆ thread_copy_to_user()

uint64_t thread_copy_to_user ( thread_t thread,
void *  userDest,
const void *  src,
uint64_t  length 
)

Safely copy data to user space.

Will pin the user pages in memory while performing the copy and expand the user stack if necessary.

Parameters
threadThe thread performing the operation.
userDestThe destination buffer in user space.
srcThe source buffer in kernel space.
lengthThe number of bytes to copy.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 220 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_copy_from_user_terminated()

uint64_t thread_copy_from_user_terminated ( thread_t thread,
const void *  userArray,
const void *  terminator,
uint8_t  objectSize,
uint64_t  maxCount,
void **  outArray,
uint64_t outCount 
)

Safely copy a null-terminated array of objects from user space.

Parameters
threadThe thread performing the operation.
userArrayThe source array in user space.
terminatorA pointer to the terminator object.
objectSizeThe size of each object in the array.
maxCountThe maximum number of objects to copy.
outArrayOutput pointer to store the allocated array in kernel space, must be freed by the caller.
outCountOutput pointer to store the number of objects copied, can be NULL.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 238 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_copy_from_user_string()

uint64_t thread_copy_from_user_string ( thread_t thread,
char *  dest,
const char *  userSrc,
uint64_t  size 
)

Safely copy a string from user space.

Parameters
threadThe thread performing the operation.
destThe destination buffer in kernel space.
userSrcThe source buffer in user space.
sizeThe size of the destination buffer.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 278 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_copy_from_user_pathname()

uint64_t thread_copy_from_user_pathname ( thread_t thread,
pathname_t pathname,
const char *  userPath 
)

Safely copy a string from user space and use it to initialize a pathname.

Parameters
threadThe thread performing the operation.
pathnameA pointer to the pathname to initialize.
userPathThe string in user space.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 300 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_copy_from_user_string_array()

uint64_t thread_copy_from_user_string_array ( thread_t thread,
const char **  user,
char ***  out,
uint64_t outAmount 
)

Safely copy a null-terminated array of strings and their contents from user space into a string vector.

Parameters
threadThe thread performing the operation.
userThe source array of strings in user space.
outOutput pointer to store the allocated array of strings in kernel space, must be freed by the caller.
outAmountOutput pointer to store the number of strings copied, can be NULL.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 328 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_load_atomic_from_user()

uint64_t thread_load_atomic_from_user ( thread_t thread,
atomic_uint64_t *  userObj,
uint64_t outValue 
)

Atomically load a 64-bit value from a user-space atomic variable.

Will pin the user pages in memory while performing the load and expand the user stack if necessary.

Parameters
threadThe thread performing the operation.
userObjThe user-space atomic variable to load from.
outValueOutput pointer to store the loaded value.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 373 of file thread.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_jump()

_NORETURN void thread_jump ( thread_t thread)
extern

Jump to a thread by calling thread_load() and then loading its interrupt frame.

Must be done in assembly as it requires directly modifying registers.

Will never return instead it ends up at thread->frame.rip.

Parameters
threadThe thread to jump to.
Here is the caller graph for this function: