MyOS Kernel
tasking.c File Reference
#include "cpu/halt.h"
#include "cpu/paging.h"
#include "cpu/serial.h"
#include "cpu/tasking_helpers.h"
#include "kmalloc.h"
#include "tasking.h"
#include <sys/types.h>
#include <string.h>

Macros

#define MAX_PROCS   32768
 Maximum number of processes that can be running at a time.
 
#define HAS_UNBLOCKED_THREADS(proc)   (proc->num_threads!=proc->num_threads_blocked)
 Macro to check whethe a process has unblocked threads.
 
#define NUM_UNBLOCKED_THREADS(proc)   (proc->num_threads-proc->num_threads_blocked)
 Macro to get the number of unblocked threads for a process.
 
#define SAME_PROC(thread1, thread2)   (thread1->process->pid==thread2->process->pid)
 Macro to check whether two threads have the same PID.
 
#define SAME_THREAD(thread1, thread2)   (thread1->process->pid==thread2->process->pid&&thread1->tid==thread2->tid)
 Macro to check whether two threads have the same PID and TID.
 

Functions

static char is_proc_scheduled (pid_t index)
 
static void mark_proc_scheduled (pid_t index)
 
static void unmark_proc_scheduled (pid_t index)
 
void schedule_thread (Thread *thread)
 
void tasking_create_task (void *eip, void *address_space, char kmode, void *param1, void *param2, char isThread)
 
void tasking_init ()
 
char tasking_is_privleged ()
 
pid_t tasking_get_PID ()
 
pid_t tasking_get_TID ()
 
int * tasking_get_errno_address ()
 
pid_t tasking_new_thread (void *start, pid_t pid, void *param)
 
static Threadget_next_ready_thread (Thread *thread, Thread *thread_to_skip)
 
void switch_to_thread (Thread *thread)
 
void tasking_yield ()
 
void tasking_block (thread_state newstate)
 
static Threadget_thread (pid_t pid, pid_t tid)
 
void tasking_unblock (pid_t pid, pid_t tid)
 
void tasking_exit (int code)
 
void * tasking_get_address_space (pid_t pid)
 
void tasking_set_rpc_calling_thread (pid_t pid, pid_t tid)
 
pid_t tasking_get_rpc_calling_thread (pid_t *tid)
 
void tasking_set_rpc_ret_buf (void *buf)
 
void * tasking_get_rpc_ret_buf ()
 
void tasking_thread_exit ()
 
char tasking_check_proc_exists (pid_t pid)
 

Variables

pid_t next_pid =0
 PID to use for the next created process.
 
size_t num_procs =0
 Number of non-exited processes.
 
Process processes [MAX_PROCS]
 Array pf processes by PID.
 
char proc_schedule_bmap [MAX_PROCS/8]
 Bitmap of what processes are scheduled.
 
Threadcurrent_thread
 Currently running thread.
 
static Threadready_to_run_head =NULL
 Head of the linked list of ready to run threads.
 
static Threadready_to_run_tail =NULL
 Tail of the linked list of ready to run threads.
 

Function Documentation

◆ get_next_ready_thread()

static Thread* get_next_ready_thread ( Thread thread,
Thread thread_to_skip 
)
static

Get the next ready thread in a list of threads, starting at the specified thread's next thread

Parameters
threadThe start thread
thread_to_skipA thread to skip even if it's ready
Returns
the next ready thread

◆ get_thread()

static Thread* get_thread ( pid_t  pid,
pid_t  tid 
)
static

Get a thread

Parameters
pidThe PID of the thread
tidThe TID of the thread
Returns
the thread wih the specified PID and TID

◆ is_proc_scheduled()

static char is_proc_scheduled ( pid_t  index)
static

Check whether a process is scheduled

Parameters
indexThe PID to check
Returns
whether the process is scheduled

◆ mark_proc_scheduled()

static void mark_proc_scheduled ( pid_t  index)
static

Mark a process as scheduled

Parameters
indexThe PID to mark

◆ schedule_thread()

void schedule_thread ( Thread thread)

Schedules a thread if the thread's prcess does not have a scheduled thread

Parameters
threadThe thread to schedule

◆ switch_to_thread()

void switch_to_thread ( Thread thread)

Switch to a thread and schedule the next ready thread in the current process, if there is one.

Parameters
threadThe thread to switch to

◆ tasking_block()

void tasking_block ( thread_state  newstate)

Block the current thread & yield

Parameters
newstateThe state to block it in

◆ tasking_check_proc_exists()

char tasking_check_proc_exists ( pid_t  pid)

Check if a process exists

Parameters
pidThe param of the process to check
Returns
Whether the process exists

◆ tasking_create_task()

void tasking_create_task ( void *  eip,
void *  address_space,
char  kmode,
void *  param1,
void *  param2,
char  isThread 
)

Create a task

Parameters
eipThe start address of the task
address_spaceThe address space of the task
kmodeWhether the task is a kernel mode task
param1The thread's start function first parameter
param2The thread's start function second parameter
isThreadWhether we are creating a new process or a thread in a process. If we are creating a theead, param2_arg becomes the PID for the newly created thread.

◆ tasking_exit()

void tasking_exit ( int  code)

Terminate the current thread If the main thread terminates, the whole process terminates.

Note
Currently, calling tasking_exit from any thread terminates the whole process.
Parameters
codeThe exit code of the thread

◆ tasking_get_address_space()

void* tasking_get_address_space ( pid_t  pid)

Get the address_space of a process

Parameters
pidthe PID of the process
Returns
the address_space of the process

◆ tasking_get_errno_address()

int* tasking_get_errno_address ( )

Get the adddress of errno for the current thread

Returns
The address of errno

◆ tasking_get_PID()

pid_t tasking_get_PID ( )

Get the PID of the current thread.

Returns
The current thread's PID

◆ tasking_get_rpc_calling_thread()

pid_t tasking_get_rpc_calling_thread ( pid_t *  tid)

Get the RPC calling thread for the current thread

Parameters
tidA pointer to a pid_t to store the return TID
Returns
the return PID
Note
This is only applicable for an RPC handler thread

◆ tasking_get_rpc_ret_buf()

void* tasking_get_rpc_ret_buf ( )

Get the RPC return buffer for the current thread

Returns
the return buffer

◆ tasking_get_TID()

pid_t tasking_get_TID ( )

Get the TID of the current thread.

Returns
The current thread's TID

◆ tasking_init()

void tasking_init ( )

Initialize tasking

◆ tasking_is_privleged()

char tasking_is_privleged ( )

Check whether the current process is privleged

Returns
whether the current process is privleged

◆ tasking_new_thread()

pid_t tasking_new_thread ( void *  start,
pid_t  pid,
void *  param 
)

Create a new thread

Parameters
startThe start address of the task
pidThe PID that gets the new thread
paramThe thread's start function parameter
Returns
the TID of the thread

◆ tasking_set_rpc_calling_thread()

void tasking_set_rpc_calling_thread ( pid_t  pid,
pid_t  tid 
)

Set the RPC calling thread for an RPC handler thread to the current threasd

Parameters
pidThe PID of the handler thread
tidThe TID of the handler thread

◆ tasking_set_rpc_ret_buf()

void tasking_set_rpc_ret_buf ( void *  buf)

Set the RPC return buffer for the calling thread

Parameters
bufThe return buffer

◆ tasking_thread_exit()

void tasking_thread_exit ( )

Terminate the current thread

◆ tasking_unblock()

void tasking_unblock ( pid_t  pid,
pid_t  tid 
)

Unblock a thread

Parameters
pidThe PID that contains the thread to unblock
tidThe TID in the process to unblock.

◆ tasking_yield()

void tasking_yield ( )

Yield to the next ready thread in any process

◆ unmark_proc_scheduled()

static void unmark_proc_scheduled ( pid_t  index)
static

Unmark a process as scheduled

Parameters
indexThe PID to unmark