PatchworkOS  da8a090
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.
 
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.
 
void thread_free (thread_t *thread)
 Frees a thread structure.
 
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 void *buffer, uint64_t count)
 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 *dest, const void *userSrc, 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_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 103 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.

Warning
This state is more fragile than the others. Since a thread could technically be killed at any time and we have several systems relying on the thread state as a form of synchronization, this state should only be set when the thread is running in an interrupt context.

Definition at line 29 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 70 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 97 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 124 of file thread.c.

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

◆ 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 140 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 147 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 156 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 void *  buffer,
uint64_t  count 
)

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.
bufferThe buffer to write.
countThe number of bytes to write.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 161 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 188 of file thread.c.

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

◆ thread_copy_to_user()

uint64_t thread_copy_to_user ( thread_t thread,
void *  dest,
const void *  userSrc,
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.
destThe destination buffer in user space.
userSrcThe 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 206 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 224 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 265 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 294 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 333 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: