removed typedef for process control block an renamed struct to match snake case

master
Anton Lydike 3 years ago
parent a3712570b0
commit f479e151cb

@ -7,7 +7,7 @@
void read_binary_table(); void read_binary_table();
extern ProcessControlBlock processes[PROCESS_COUNT]; extern struct process_control_block processes[PROCESS_COUNT];
// this array is populated when the memory image is built, therefore it should // this array is populated when the memory image is built, therefore it should
// resign in a section which is not overwritten with zeros on startup // resign in a section which is not overwritten with zeros on startup

@ -5,7 +5,7 @@
#include "io.h" #include "io.h"
// this type is only used here, therefore we don't need it in the ktypes header // this type is only used here, therefore we don't need it in the ktypes header
typedef optional_int (*ecall_handler)(int*, ProcessControlBlock*); typedef optional_int (*ecall_handler)(int*, struct process_control_block*);
ecall_handler ecall_table[ECALL_TABLE_LEN] = { 0 }; ecall_handler ecall_table[ECALL_TABLE_LEN] = { 0 };
@ -14,7 +14,7 @@ ecall_handler ecall_table[ECALL_TABLE_LEN] = { 0 };
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wunused-parameter"
optional_int ecall_handle_spawn_thread(int* args_ptr, ProcessControlBlock* pcb) optional_int ecall_handle_spawn_thread(int* args_ptr, struct process_control_block* pcb)
{ {
void* entry = (void*) args_ptr[0]; // a0 void* entry = (void*) args_ptr[0]; // a0
void* args = (void*) args_ptr[1]; // a1 void* args = (void*) args_ptr[1]; // a1
@ -28,7 +28,7 @@ optional_int ecall_handle_spawn_thread(int* args_ptr, ProcessControlBlock* pcb)
return (optional_int) { .value = pcb_or_err.value->pid }; return (optional_int) { .value = pcb_or_err.value->pid };
} }
optional_int ecall_handle_sleep(int* args, ProcessControlBlock* pcb) optional_int ecall_handle_sleep(int* args, struct process_control_block* pcb)
{ {
int len = args[0]; int len = args[0];
@ -44,11 +44,11 @@ optional_int ecall_handle_sleep(int* args, ProcessControlBlock* pcb)
return (optional_int) { .value = 0 }; return (optional_int) { .value = 0 };
} }
optional_int ecall_handle_join(int* args, ProcessControlBlock* pcb) optional_int ecall_handle_join(int* args, struct process_control_block* pcb)
{ {
int pid = args[0]; // a0 int pid = args[0]; // a0
ProcessControlBlock* target = process_from_pid(pid); struct process_control_block* target = process_from_pid(pid);
if (target == NULL) if (target == NULL)
return (optional_int) { .error = ESRCH }; return (optional_int) { .error = ESRCH };
@ -71,10 +71,10 @@ optional_int ecall_handle_join(int* args, ProcessControlBlock* pcb)
return (optional_int) { .value = 0 }; return (optional_int) { .value = 0 };
} }
optional_int ecall_handle_kill(int* args, ProcessControlBlock* pcb) optional_int ecall_handle_kill(int* args, struct process_control_block* pcb)
{ {
int pid = args[0]; int pid = args[0];
ProcessControlBlock* target = process_from_pid(pid); struct process_control_block* target = process_from_pid(pid);
// return error if no process has that id // return error if no process has that id
if (target == NULL) if (target == NULL)
@ -95,7 +95,7 @@ optional_int ecall_handle_kill(int* args, ProcessControlBlock* pcb)
return (optional_int) { .value = 1 }; return (optional_int) { .value = 1 };
} }
optional_int ecall_handle_exit(int* args, ProcessControlBlock* pcb) optional_int ecall_handle_exit(int* args, struct process_control_block* pcb)
{ {
pcb->status = PROC_DEAD; pcb->status = PROC_DEAD;
pcb->exit_code = *args; pcb->exit_code = *args;
@ -120,7 +120,7 @@ optional_int ecall_handle_exit(int* args, ProcessControlBlock* pcb)
void trap_handle_ecall() void trap_handle_ecall()
{ {
mark_ecall_entry(); mark_ecall_entry();
ProcessControlBlock* pcb = get_current_process(); struct process_control_block* pcb = get_current_process();
int *regs = pcb->regs; int *regs = pcb->regs;
int code = regs[REG_A0 + 7]; // code is stored inside a7 int code = regs[REG_A0 + 7]; // code is stored inside a7
@ -199,7 +199,7 @@ void init_ecall_table()
void handle_exception(int ecode, int mtval) void handle_exception(int ecode, int mtval)
{ {
// kill off offending process // kill off offending process
ProcessControlBlock* pcb = get_current_process(); struct process_control_block* pcb = get_current_process();
pcb->status = PROC_DEAD; pcb->status = PROC_DEAD;
pcb->exit_code = -99; pcb->exit_code = -99;

@ -28,21 +28,21 @@ enum process_status {
}; };
// forward define structs for recursive references // forward define structs for recursive references
typedef struct ProcessControlBlock ProcessControlBlock; struct process_control_block;
struct loaded_binary; struct loaded_binary;
struct ProcessControlBlock { struct process_control_block {
int pid; int pid;
int pc; int pc;
int regs[31]; int regs[31];
int exit_code; int exit_code;
// scheduling information // scheduling information
enum process_status status; enum process_status status;
ProcessControlBlock* waiting_for_process; struct process_control_block* waiting_for_process;
struct loaded_binary* binary; struct loaded_binary* binary;
unsigned long long int asleep_until; unsigned long long int asleep_until;
// parent // parent
ProcessControlBlock* parent; struct process_control_block* parent;
void* stack_top; void* stack_top;
}; };
@ -98,7 +98,7 @@ typedef struct loaded_binary {
// define some type aliases that only contain ascii character // define some type aliases that only contain ascii character
// they are used in the name of the struct optional_<name> // they are used in the name of the struct optional_<name>
typedef void* voidptr; typedef void* voidptr;
typedef ProcessControlBlock* pcbptr; typedef struct process_control_block* pcbptr;
// size_t is another standard type // size_t is another standard type
typedef unsigned int size_t; typedef unsigned int size_t;

@ -11,9 +11,9 @@ extern void memset(int, void*, void*);
extern int thread_finalizer; extern int thread_finalizer;
// process list, holds all active and some dead processes // process list, holds all active and some dead processes
ProcessControlBlock processes[PROCESS_COUNT]; struct process_control_block processes[PROCESS_COUNT];
// pointer to the currently scheduled process // pointer to the currently scheduled process
ProcessControlBlock* current_process = NULL; struct process_control_block* current_process = NULL;
// timer variables to add kernel time back to the processes time slice // timer variables to add kernel time back to the processes time slice
unsigned long long int scheduling_interrupted_start; unsigned long long int scheduling_interrupted_start;
unsigned long long int next_interrupt_scheduled_for; unsigned long long int next_interrupt_scheduled_for;
@ -35,7 +35,7 @@ void scheudler_init()
} }
// try to return to a process // try to return to a process
void scheduler_try_return_to(ProcessControlBlock* pcb) void scheduler_try_return_to(struct process_control_block* pcb)
{ {
// if the process isn't ready, schedule a new one // if the process isn't ready, schedule a new one
if (pcb->status != PROC_RDY) { if (pcb->status != PROC_RDY) {
@ -58,15 +58,15 @@ void scheduler_try_return_to(ProcessControlBlock* pcb)
} }
// select a new process to run next // select a new process to run next
ProcessControlBlock* scheduler_select_free() struct process_control_block* scheduler_select_free()
{ {
unsigned long long int mtime; unsigned long long int mtime;
int timeout_available = false; // note if a timeout is available int timeout_available = 0; // note if a timeout is available
while (true) { while (1) {
mtime = read_time(); mtime = read_time();
// start at the last scheduled process // start at the last scheduled process
ProcessControlBlock* pcb = current_process; struct process_control_block* pcb = current_process;
// iterate once over the whole list // iterate once over the whole list
do { do {
@ -122,7 +122,7 @@ ProcessControlBlock* scheduler_select_free()
} }
} }
void scheduler_switch_to(ProcessControlBlock* pcb) void scheduler_switch_to(struct process_control_block* pcb)
{ {
CSR_WRITE(CSR_MEPC, pcb->pc); CSR_WRITE(CSR_MEPC, pcb->pc);
@ -167,7 +167,7 @@ void scheduler_switch_to(ProcessControlBlock* pcb)
__builtin_unreachable(); __builtin_unreachable();
} }
ProcessControlBlock* process_from_pid(int pid) struct process_control_block* process_from_pid(int pid)
{ {
for (int i = 0; i < PROCESS_COUNT; i++) { for (int i = 0; i < PROCESS_COUNT; i++) {
if (processes[i].pid == pid) if (processes[i].pid == pid)
@ -181,7 +181,7 @@ int* get_current_process_registers()
return current_process->regs; return current_process->regs;
} }
ProcessControlBlock* get_current_process() struct process_control_block* get_current_process()
{ {
return current_process; return current_process;
} }
@ -201,7 +201,7 @@ optional_pcbptr find_available_pcb_slot()
{ {
static int index = 0; static int index = 0;
int start_index = index; int start_index = index;
ProcessControlBlock* pcb = processes + index; struct process_control_block* pcb = processes + index;
while (pcb->status != PROC_DEAD) { while (pcb->status != PROC_DEAD) {
index = (index + 1) % PROCESS_COUNT; index = (index + 1) % PROCESS_COUNT;
@ -234,7 +234,7 @@ optional_pcbptr create_new_process(loaded_binary* bin, int stack_size)
return (optional_pcbptr) { .error = stack_top_or_err.error }; return (optional_pcbptr) { .error = stack_top_or_err.error };
} }
ProcessControlBlock* pcb = slot_or_err.value; struct process_control_block* pcb = slot_or_err.value;
// determine next pid // determine next pid
int pid = next_process_id++; int pid = next_process_id++;
@ -259,7 +259,7 @@ optional_pcbptr create_new_process(loaded_binary* bin, int stack_size)
return (optional_pcbptr) { .value = pcb }; return (optional_pcbptr) { .value = pcb };
} }
optional_pcbptr create_new_thread(ProcessControlBlock* parent, void* entrypoint, void* args, int stack_size) optional_pcbptr create_new_thread(struct process_control_block* parent, void* entrypoint, void* args, int stack_size)
{ {
// try to get a position in the processes list // try to get a position in the processes list
optional_pcbptr slot_or_err = find_available_pcb_slot(); optional_pcbptr slot_or_err = find_available_pcb_slot();
@ -279,7 +279,7 @@ optional_pcbptr create_new_thread(ProcessControlBlock* parent, void* entrypoint,
return (optional_pcbptr) { .error = stack_top_or_err.error }; return (optional_pcbptr) { .error = stack_top_or_err.error };
} }
ProcessControlBlock* pcb = slot_or_err.value; struct process_control_block* pcb = slot_or_err.value;
// determine next pid // determine next pid
int pid = next_process_id++; int pid = next_process_id++;
@ -308,10 +308,10 @@ optional_pcbptr create_new_thread(ProcessControlBlock* parent, void* entrypoint,
return (optional_pcbptr) { .value = pcb }; return (optional_pcbptr) { .value = pcb };
} }
void kill_child_processes(ProcessControlBlock* pcb) void kill_child_processes(struct process_control_block* pcb)
{ {
for (int i = 0; i < PROCESS_COUNT; i++) { for (int i = 0; i < PROCESS_COUNT; i++) {
ProcessControlBlock* proc = processes + i; struct process_control_block* proc = processes + i;
// if this is not a child process or already exited // if this is not a child process or already exited
if (proc->parent != pcb || proc->status == PROC_DEAD) if (proc->parent != pcb || proc->status == PROC_DEAD)
continue; continue;
@ -322,7 +322,7 @@ void kill_child_processes(ProcessControlBlock* pcb)
} }
} }
void destroy_process(ProcessControlBlock* pcb) void destroy_process(struct process_control_block* pcb)
{ {
// kill child processes // kill child processes
kill_child_processes(pcb); kill_child_processes(pcb);

@ -5,23 +5,23 @@
#include "ktypes.h" #include "ktypes.h"
// scheduling data: // scheduling data:
extern ProcessControlBlock processes[PROCESS_COUNT]; extern struct process_control_block processes[PROCESS_COUNT];
// scheduler methods // scheduler methods
void scheudler_init(); void scheudler_init();
ProcessControlBlock* scheduler_select_free(); struct process_control_block* scheduler_select_free();
void set_next_interrupt(); void set_next_interrupt();
void __attribute__((noreturn)) scheduler_run_next(); void __attribute__((noreturn)) scheduler_run_next();
void __attribute__((noreturn)) scheduler_try_return_to(ProcessControlBlock*); void __attribute__((noreturn)) scheduler_try_return_to(struct process_control_block*);
void __attribute__((noreturn)) scheduler_switch_to(ProcessControlBlock*); void __attribute__((noreturn)) scheduler_switch_to(struct process_control_block*);
ProcessControlBlock* process_from_pid(int pid); struct process_control_block* process_from_pid(int pid);
int* get_current_process_registers(); int* get_current_process_registers();
ProcessControlBlock* get_current_process(); struct process_control_block* get_current_process();
void mark_ecall_entry(); void mark_ecall_entry();
// process creation / destruction // process creation / destruction
optional_pcbptr create_new_process(loaded_binary*, int); optional_pcbptr create_new_process(loaded_binary*, int);
optional_pcbptr create_new_thread(ProcessControlBlock*, void*, void*, int); optional_pcbptr create_new_thread(struct process_control_block*, void*, void*, int);
void destroy_process(ProcessControlBlock* pcb); void destroy_process(struct process_control_block* pcb);
void kill_child_processes(ProcessControlBlock* pcb); void kill_child_processes(struct process_control_block* pcb);
#endif #endif

Loading…
Cancel
Save