summaryrefslogtreecommitdiff
path: root/kernel/procs.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-03-27 11:39:12 -0400
committerFreya Murphy <freya@freyacat.org>2025-03-27 11:39:12 -0400
commit0ff301cda68669c59351e5854ce98f2cf460543f (patch)
treecfe8f976261962420ada64b821559b9da0a56841 /kernel/procs.c
parentadd compile_flags.txt for clangd lsp (diff)
downloadcomus-0ff301cda68669c59351e5854ce98f2cf460543f.tar.gz
comus-0ff301cda68669c59351e5854ce98f2cf460543f.tar.bz2
comus-0ff301cda68669c59351e5854ce98f2cf460543f.zip
pull upstream changes, add auto formatting
Diffstat (limited to 'kernel/procs.c')
-rw-r--r--kernel/procs.c514
1 files changed, 247 insertions, 267 deletions
diff --git a/kernel/procs.c b/kernel/procs.c
index 96bb3fd..64edb49 100644
--- a/kernel/procs.c
+++ b/kernel/procs.c
@@ -6,7 +6,7 @@
** @brief Process-related implementations
*/
-#define KERNEL_SRC
+#define KERNEL_SRC
#include <common.h>
@@ -18,7 +18,7 @@
*/
// determine if a queue is empty; assumes 'q' is a valid pointer
-#define PCB_QUEUE_EMPTY(q) ((q)->head == NULL)
+#define PCB_QUEUE_EMPTY(q) ((q)->head == NULL)
/*
** PRIVATE DATA TYPES
@@ -75,32 +75,28 @@ pcb_t *init_pcb;
// table of state name strings
const char *state_str[N_STATES] = {
- [ STATE_UNUSED ] = "Unu", // "Unused"
- [ STATE_NEW ] = "New",
- [ STATE_READY ] = "Rdy", // "Ready"
- [ STATE_RUNNING ] = "Run", // "Running"
- [ STATE_SLEEPING ] = "Slp", // "Sleeping"
- [ STATE_BLOCKED ] = "Blk", // "Blocked"
- [ STATE_WAITING ] = "Wat", // "Waiting"
- [ STATE_KILLED ] = "Kil", // "Killed"
- [ STATE_ZOMBIE ] = "Zom" // "Zombie"
+ [STATE_UNUSED] = "Unu", // "Unused"
+ [STATE_NEW] = "New",
+ [STATE_READY] = "Rdy", // "Ready"
+ [STATE_RUNNING] = "Run", // "Running"
+ [STATE_SLEEPING] = "Slp", // "Sleeping"
+ [STATE_BLOCKED] = "Blk", // "Blocked"
+ [STATE_WAITING] = "Wat", // "Waiting"
+ [STATE_KILLED] = "Kil", // "Killed"
+ [STATE_ZOMBIE] = "Zom" // "Zombie"
};
// table of priority name strings
-const char *prio_str[N_PRIOS] = {
- [ PRIO_HIGH ] = "High",
- [ PRIO_STD ] = "User",
- [ PRIO_LOW ] = "Low ",
- [ PRIO_DEFERRED ] = "Def "
-};
+const char *prio_str[N_PRIOS] = { [PRIO_HIGH] = "High",
+ [PRIO_STD] = "User",
+ [PRIO_LOW] = "Low ",
+ [PRIO_DEFERRED] = "Def " };
// table of queue ordering name strings
-const char *ord_str[N_PRIOS] = {
- [ O_FIFO ] = "FIFO",
- [ O_PRIO ] = "PRIO",
- [ O_PID ] = "PID ",
- [ O_WAKEUP ] = "WAKE"
-};
+const char *ord_str[N_PRIOS] = { [O_FIFO] = "FIFO",
+ [O_PRIO] = "PRIO",
+ [O_PID] = "PID ",
+ [O_WAKEUP] = "WAKE" };
/*
** PRIVATE FUNCTIONS
@@ -125,16 +121,16 @@ const char *ord_str[N_PRIOS] = {
** @return a pointer to the predecessor in the queue, or NULL if
** this PCB would be at the beginning of the queue.
*/
-static pcb_t *find_prev_wakeup( pcb_queue_t queue, pcb_t *pcb ) {
-
+static pcb_t *find_prev_wakeup(pcb_queue_t queue, pcb_t *pcb)
+{
// sanity checks!
- assert1( queue != NULL );
- assert1( pcb != NULL );
+ assert1(queue != NULL);
+ assert1(pcb != NULL);
pcb_t *prev = NULL;
pcb_t *curr = queue->head;
- while( curr != NULL && curr->wakeup <= pcb->wakeup ) {
+ while (curr != NULL && curr->wakeup <= pcb->wakeup) {
prev = curr;
curr = curr->next;
}
@@ -142,16 +138,16 @@ static pcb_t *find_prev_wakeup( pcb_queue_t queue, pcb_t *pcb ) {
return prev;
}
-static pcb_t *find_prev_priority( pcb_queue_t queue, pcb_t *pcb ) {
-
+static pcb_t *find_prev_priority(pcb_queue_t queue, pcb_t *pcb)
+{
// sanity checks!
- assert1( queue != NULL );
- assert1( pcb != NULL );
+ assert1(queue != NULL);
+ assert1(pcb != NULL);
pcb_t *prev = NULL;
pcb_t *curr = queue->head;
- while( curr != NULL && curr->priority <= pcb->priority ) {
+ while (curr != NULL && curr->priority <= pcb->priority) {
prev = curr;
curr = curr->next;
}
@@ -159,16 +155,16 @@ static pcb_t *find_prev_priority( pcb_queue_t queue, pcb_t *pcb ) {
return prev;
}
-static pcb_t *find_prev_pid( pcb_queue_t queue, pcb_t *pcb ) {
-
+static pcb_t *find_prev_pid(pcb_queue_t queue, pcb_t *pcb)
+{
// sanity checks!
- assert1( queue != NULL );
- assert1( pcb != NULL );
+ assert1(queue != NULL);
+ assert1(pcb != NULL);
pcb_t *prev = NULL;
pcb_t *curr = queue->head;
- while( curr != NULL && curr->pid <= pcb->pid ) {
+ while (curr != NULL && curr->pid <= pcb->pid) {
prev = curr;
curr = curr->next;
}
@@ -181,10 +177,10 @@ static pcb_t *find_prev_pid( pcb_queue_t queue, pcb_t *pcb ) {
*/
// a macro to simplify queue setup
-#define QINIT(q,s) \
- q = &q##_queue; \
- if( pcb_queue_reset(q,s) != SUCCESS ) { \
- PANIC( 0, "pcb_init can't reset " # q ); \
+#define QINIT(q, s) \
+ q = &q##_queue; \
+ if (pcb_queue_reset(q, s) != SUCCESS) { \
+ PANIC(0, "pcb_init can't reset " #q); \
}
/**
@@ -192,22 +188,22 @@ static pcb_t *find_prev_pid( pcb_queue_t queue, pcb_t *pcb ) {
**
** Initialization for the Process module.
*/
-void pcb_init( void ) {
-
+void pcb_init(void)
+{
#if TRACING_INIT
- cio_puts( " Procs" );
+ cio_puts(" Procs");
#endif
// there is no current process
current = NULL;
// set up the external links to the queues
- QINIT( pcb_freelist, O_FIFO );
- QINIT( ready, O_PRIO );
- QINIT( waiting, O_PID );
- QINIT( sleeping, O_WAKEUP );
- QINIT( zombie, O_PID );
- QINIT( sioread, O_FIFO );
+ QINIT(pcb_freelist, O_FIFO);
+ QINIT(ready, O_PRIO);
+ QINIT(waiting, O_PID);
+ QINIT(sleeping, O_WAKEUP);
+ QINIT(zombie, O_PID);
+ QINIT(sioread, O_FIFO);
/*
** We statically allocate our PCBs, so we need to add them
@@ -218,8 +214,8 @@ void pcb_init( void ) {
*/
pcb_t *ptr = ptable;
- for( int i = 0; i < N_PROCS; ++i ) {
- pcb_free( ptr );
+ for (int i = 0; i < N_PROCS; ++i) {
+ pcb_free(ptr);
++ptr;
}
}
@@ -233,14 +229,14 @@ void pcb_init( void ) {
**
** @return status of the allocation attempt
*/
-int pcb_alloc( pcb_t **pcb ) {
-
+int pcb_alloc(pcb_t **pcb)
+{
// sanity check!
- assert1( pcb != NULL );
+ assert1(pcb != NULL);
// remove the first PCB from the free list
pcb_t *tmp;
- if( pcb_queue_remove(pcb_freelist,&tmp) != SUCCESS ) {
+ if (pcb_queue_remove(pcb_freelist, &tmp) != SUCCESS) {
return E_NO_PCBS;
}
@@ -255,20 +251,19 @@ int pcb_alloc( pcb_t **pcb ) {
**
** @param pcb Pointer to the PCB to be deallocated.
*/
-void pcb_free( pcb_t *pcb ) {
-
- if( pcb != NULL ) {
+void pcb_free(pcb_t *pcb)
+{
+ if (pcb != NULL) {
// mark the PCB as available
pcb->state = STATE_UNUSED;
// add it to the free list
- int status = pcb_queue_insert( pcb_freelist, pcb );
+ int status = pcb_queue_insert(pcb_freelist, pcb);
// if that failed, we're in trouble
- if( status != SUCCESS ) {
- sprint( b256, "pcb_free(0x%08x) status %d", (uint32_t) pcb,
- status );
- PANIC( 0, b256 );
+ if (status != SUCCESS) {
+ sprint(b256, "pcb_free(0x%08x) status %d", (uint32_t)pcb, status);
+ PANIC(0, b256);
}
}
}
@@ -282,15 +277,15 @@ void pcb_free( pcb_t *pcb ) {
**
** @param pcb Pointer to the newly-undead PCB
*/
-void pcb_zombify( register pcb_t *victim ) {
-
+void pcb_zombify(register pcb_t *victim)
+{
// should this be an error?
- if( victim == NULL ) {
+ if (victim == NULL) {
return;
}
// every process must have a parent, even if it's 'init'
- assert( victim->parent != NULL );
+ assert(victim->parent != NULL);
/*
** We need to locate the parent of this process. We also need
@@ -306,26 +301,24 @@ void pcb_zombify( register pcb_t *victim ) {
// speed up access to the process table entries
register pcb_t *curr = ptable;
- for( int i = 0; i < N_PROCS; ++i, ++curr ) {
-
+ for (int i = 0; i < N_PROCS; ++i, ++curr) {
// make sure this is a valid entry
- if( curr->state == STATE_UNUSED ) {
+ if (curr->state == STATE_UNUSED) {
continue;
}
// if this is our parent, just keep going - we continue
// iterating to find all the children of this process.
- if( curr == parent ) {
+ if (curr == parent) {
continue;
}
- if( curr->parent == victim ) {
-
+ if (curr->parent == victim) {
// found a child - reparent it
curr->parent = init_pcb;
// see if this child is already undead
- if( curr->state == STATE_ZOMBIE ) {
+ if (curr->state == STATE_ZOMBIE) {
// if it's already a zombie, remember it, so we
// can pass it on to 'init'; also, if there are
// two or more zombie children, it doesn't matter
@@ -333,7 +326,6 @@ void pcb_zombify( register pcb_t *victim ) {
// collected when 'init' loops
zchild = curr;
}
-
}
}
@@ -353,20 +345,19 @@ void pcb_zombify( register pcb_t *victim ) {
** call waitpid() again, by which time this exiting process will
** be marked as a zombie.
*/
- if( zchild != NULL && init_pcb->state == STATE_WAITING ) {
-
+ if (zchild != NULL && init_pcb->state == STATE_WAITING) {
// dequeue the zombie
- assert( pcb_queue_remove_this(zombie,zchild) == SUCCESS );
+ assert(pcb_queue_remove_this(zombie, zchild) == SUCCESS);
- assert( pcb_queue_remove_this(waiting,init_pcb) == SUCCESS );
+ assert(pcb_queue_remove_this(waiting, init_pcb) == SUCCESS);
// intrinsic return value is the PID
RET(init_pcb) = zchild->pid;
// may also want to return the exit status
- int32_t *ptr = (int32_t *) ARG(init_pcb,2);
+ int32_t *ptr = (int32_t *)ARG(init_pcb, 2);
- if( ptr != NULL ) {
+ if (ptr != NULL) {
// ********************************************************
// ** Potential VM issue here! This code assigns the exit
// ** status into a variable in the parent's address space.
@@ -379,8 +370,8 @@ void pcb_zombify( register pcb_t *victim ) {
}
// all done - schedule 'init', and clean up the zombie
- schedule( init_pcb );
- pcb_cleanup( zchild );
+ schedule(init_pcb);
+ pcb_cleanup(zchild);
}
/*
@@ -394,14 +385,12 @@ void pcb_zombify( register pcb_t *victim ) {
** worry about it being scheduled twice.
*/
- if( parent->state == STATE_WAITING ) {
-
+ if (parent->state == STATE_WAITING) {
// verify that the parent is either waiting for this process
// or is waiting for any of its children
- uint32_t target = ARG(parent,1);
-
- if( target == 0 || target == vicpid ) {
+ uint32_t target = ARG(parent, 1);
+ if (target == 0 || target == vicpid) {
// the parent is waiting for this child or is waiting
// for any of its children, so we can wake it up.
@@ -409,9 +398,9 @@ void pcb_zombify( register pcb_t *victim ) {
RET(parent) = vicpid;
// may also want to return the exit status
- int32_t *ptr = (int32_t *) ARG(parent,2);
+ int32_t *ptr = (int32_t *)ARG(parent, 2);
- if( ptr != NULL ) {
+ if (ptr != NULL) {
// ********************************************************
// ** Potential VM issue here! This code assigns the exit
// ** status into a variable in the parent's address space.
@@ -424,8 +413,8 @@ void pcb_zombify( register pcb_t *victim ) {
}
// all done - schedule the parent, and clean up the zombie
- schedule( parent );
- pcb_cleanup( victim );
+ schedule(parent);
+ pcb_cleanup(victim);
return;
}
@@ -443,7 +432,7 @@ void pcb_zombify( register pcb_t *victim ) {
*/
victim->state = STATE_ZOMBIE;
- assert( pcb_queue_insert(zombie,victim) == SUCCESS );
+ assert(pcb_queue_insert(zombie, victim) == SUCCESS);
/*
** Note: we don't call _dispatch() here - we leave that for
@@ -459,23 +448,23 @@ void pcb_zombify( register pcb_t *victim ) {
**
** @param pcb The PCB to reclaim
*/
-void pcb_cleanup( pcb_t *pcb ) {
-
+void pcb_cleanup(pcb_t *pcb)
+{
#if TRACING_PCB
- cio_printf( "** pcb_cleanup(0x%08x)\n", (uint32_t) pcb );
+ cio_printf("** pcb_cleanup(0x%08x)\n", (uint32_t)pcb);
#endif
// avoid deallocating a NULL pointer
- if( pcb == NULL ) {
+ if (pcb == NULL) {
// should this be an error?
return;
}
// we need to release all the VM data structures and frames
- user_cleanup( pcb );
+ user_cleanup(pcb);
// release the PCB itself
- pcb_free( pcb );
+ pcb_free(pcb);
}
/**
@@ -487,18 +476,18 @@ void pcb_cleanup( pcb_t *pcb ) {
**
** @return Pointer to the PCB, or NULL
*/
-pcb_t *pcb_find_pid( uint_t pid ) {
-
+pcb_t *pcb_find_pid(uint_t pid)
+{
// must be a valid PID
- if( pid < 1 ) {
+ if (pid < 1) {
return NULL;
}
// scan the process table
pcb_t *p = ptable;
- for( int i = 0; i < N_PROCS; ++i, ++p ) {
- if( p->pid == pid && p->state != STATE_UNUSED ) {
+ for (int i = 0; i < N_PROCS; ++i, ++p) {
+ if (p->pid == pid && p->state != STATE_UNUSED) {
return p;
}
}
@@ -516,19 +505,19 @@ pcb_t *pcb_find_pid( uint_t pid ) {
**
** @return Pointer to the PCB, or NULL
*/
-pcb_t *pcb_find_ppid( uint_t pid ) {
-
+pcb_t *pcb_find_ppid(uint_t pid)
+{
// must be a valid PID
- if( pid < 1 ) {
+ if (pid < 1) {
return NULL;
}
// scan the process table
pcb_t *p = ptable;
- for( int i = 0; i < N_PROCS; ++i, ++p ) {
- assert1( p->parent != NULL );
- if( p->parent->pid == pid && p->parent->state != STATE_UNUSED ) {
+ for (int i = 0; i < N_PROCS; ++i, ++p) {
+ assert1(p->parent != NULL);
+ if (p->parent->pid == pid && p->parent->state != STATE_UNUSED) {
return p;
}
}
@@ -548,13 +537,13 @@ pcb_t *pcb_find_ppid( uint_t pid ) {
**
** @return status of the init request
*/
-int pcb_queue_reset( pcb_queue_t queue, enum pcb_queue_order_e style ) {
-
+int pcb_queue_reset(pcb_queue_t queue, enum pcb_queue_order_e style)
+{
// sanity check
- assert1( queue != NULL );
+ assert1(queue != NULL);
// make sure the style is valid
- if( style < O_FIRST_STYLE || style > O_LAST_STYLE ) {
+ if (style < O_FIRST_STYLE || style > O_LAST_STYLE) {
return E_BAD_PARAM;
}
@@ -575,10 +564,10 @@ int pcb_queue_reset( pcb_queue_t queue, enum pcb_queue_order_e style ) {
**
** @return true if the queue is empty, else false
*/
-bool_t pcb_queue_empty( pcb_queue_t queue ) {
-
+bool_t pcb_queue_empty(pcb_queue_t queue)
+{
// if there is no queue, blow up
- assert1( queue != NULL );
+ assert1(queue != NULL);
return PCB_QUEUE_EMPTY(queue);
}
@@ -592,16 +581,16 @@ bool_t pcb_queue_empty( pcb_queue_t queue ) {
**
** @return the count (0 if the queue is empty)
*/
-uint_t pcb_queue_length( const pcb_queue_t queue ) {
-
+uint_t pcb_queue_length(const pcb_queue_t queue)
+{
// sanity check
- assert1( queue != NULL );
+ assert1(queue != NULL);
// this is pretty simple
register pcb_t *tmp = queue->head;
register int num = 0;
-
- while( tmp != NULL ) {
+
+ while (tmp != NULL) {
++num;
tmp = tmp->next;
}
@@ -619,41 +608,41 @@ uint_t pcb_queue_length( const pcb_queue_t queue ) {
**
** @return status of the insertion request
*/
-int pcb_queue_insert( pcb_queue_t queue, pcb_t *pcb ) {
-
+int pcb_queue_insert(pcb_queue_t queue, pcb_t *pcb)
+{
// sanity checks
- assert1( queue != NULL );
- assert1( pcb != NULL );
+ assert1(queue != NULL);
+ assert1(pcb != NULL);
// if this PCB is already in a queue, we won't touch it
- if( pcb->next != NULL ) {
+ if (pcb->next != NULL) {
// what to do? we let the caller decide
return E_BAD_PARAM;
}
// is the queue empty?
- if( queue->head == NULL ) {
+ if (queue->head == NULL) {
queue->head = queue->tail = pcb;
return SUCCESS;
}
- assert1( queue->tail != NULL );
+ assert1(queue->tail != NULL);
// no, so we need to search it
pcb_t *prev = NULL;
// find the predecessor node
- switch( queue->order ) {
+ switch (queue->order) {
case O_FIFO:
prev = queue->tail;
break;
case O_PRIO:
- prev = find_prev_priority(queue,pcb);
+ prev = find_prev_priority(queue, pcb);
break;
case O_PID:
- prev = find_prev_pid(queue,pcb);
+ prev = find_prev_pid(queue, pcb);
break;
case O_WAKEUP:
- prev = find_prev_wakeup(queue,pcb);
+ prev = find_prev_wakeup(queue, pcb);
break;
default:
// do we need something more specific here?
@@ -662,29 +651,25 @@ int pcb_queue_insert( pcb_queue_t queue, pcb_t *pcb ) {
// OK, we found the predecessor node; time to do the insertion
- if( prev == NULL ) {
-
+ if (prev == NULL) {
// there is no predecessor, so we're
// inserting at the front of the queue
pcb->next = queue->head;
- if( queue->head == NULL ) {
+ if (queue->head == NULL) {
// empty queue!?! - should we panic?
queue->tail = pcb;
}
queue->head = pcb;
- } else if( prev->next == NULL ) {
-
+ } else if (prev->next == NULL) {
// append at end
prev->next = pcb;
queue->tail = pcb;
} else {
-
// insert between prev & prev->next
pcb->next = prev->next;
prev->next = pcb;
-
}
return SUCCESS;
@@ -700,14 +685,14 @@ int pcb_queue_insert( pcb_queue_t queue, pcb_t *pcb ) {
**
** @return status of the removal request
*/
-int pcb_queue_remove( pcb_queue_t queue, pcb_t **pcb ) {
-
+int pcb_queue_remove(pcb_queue_t queue, pcb_t **pcb)
+{
//sanity checks
- assert1( queue != NULL );
- assert1( pcb != NULL );
+ assert1(queue != NULL);
+ assert1(pcb != NULL);
// can't get anything if there's nothing to get!
- if( PCB_QUEUE_EMPTY(queue) ) {
+ if (PCB_QUEUE_EMPTY(queue)) {
return E_EMPTY_QUEUE;
}
@@ -719,7 +704,7 @@ int pcb_queue_remove( pcb_queue_t queue, pcb_t **pcb ) {
tmp->next = NULL;
// was this the last thing in the queue?
- if( queue->head == NULL ) {
+ if (queue->head == NULL) {
// yes, so clear the tail pointer for consistency
queue->tail = NULL;
}
@@ -744,14 +729,14 @@ int pcb_queue_remove( pcb_queue_t queue, pcb_t **pcb ) {
**
** @return status of the removal request
*/
-int pcb_queue_remove_this( pcb_queue_t queue, pcb_t *pcb ) {
-
+int pcb_queue_remove_this(pcb_queue_t queue, pcb_t *pcb)
+{
//sanity checks
- assert1( queue != NULL );
- assert1( pcb != NULL );
+ assert1(queue != NULL);
+ assert1(pcb != NULL);
// can't get anything if there's nothing to get!
- if( PCB_QUEUE_EMPTY(queue) ) {
+ if (PCB_QUEUE_EMPTY(queue)) {
return E_EMPTY_QUEUE;
}
@@ -759,7 +744,7 @@ int pcb_queue_remove_this( pcb_queue_t queue, pcb_t *pcb ) {
pcb_t *prev = NULL;
pcb_t *curr = queue->head;
- while( curr != NULL && curr != pcb ) {
+ while (curr != NULL && curr != pcb) {
prev = curr;
curr = curr->next;
}
@@ -773,15 +758,15 @@ int pcb_queue_remove_this( pcb_queue_t queue, pcb_t *pcb ) {
// 5. !0 !0 0 removing from end
// 6. !0 !0 !0 removing from middle
- if( curr == NULL ) {
+ if (curr == NULL) {
// case 1
- assert( prev != NULL );
+ assert(prev != NULL);
// case 4
return E_NOT_FOUND;
}
// connect predecessor to successor
- if( prev != NULL ) {
+ if (prev != NULL) {
// not the first element
// cases 5 and 6
prev->next = curr->next;
@@ -793,7 +778,7 @@ int pcb_queue_remove_this( pcb_queue_t queue, pcb_t *pcb ) {
// if this was the last node (cases 2 and 5),
// also need to reset the tail pointer
- if( curr->next == NULL ) {
+ if (curr->next == NULL) {
// if this was the only entry (2), prev is NULL,
// so this works for that case, too
queue->tail = prev;
@@ -806,10 +791,8 @@ int pcb_queue_remove_this( pcb_queue_t queue, pcb_t *pcb ) {
// one of the queue pointers is NULL and the other one
// is not NULL
- assert1(
- (queue->head == NULL && queue->tail == NULL) ||
- (queue->head != NULL && queue->tail != NULL)
- );
+ assert1((queue->head == NULL && queue->tail == NULL) ||
+ (queue->head != NULL && queue->tail != NULL));
return SUCCESS;
}
@@ -824,13 +807,13 @@ int pcb_queue_remove_this( pcb_queue_t queue, pcb_t *pcb ) {
**
** @return the PCB poiner, or NULL if the queue is empty
*/
-pcb_t *pcb_queue_peek( const pcb_queue_t queue ) {
-
+pcb_t *pcb_queue_peek(const pcb_queue_t queue)
+{
//sanity check
- assert1( queue != NULL );
+ assert1(queue != NULL);
// can't get anything if there's nothing to get!
- if( PCB_QUEUE_EMPTY(queue) ) {
+ if (PCB_QUEUE_EMPTY(queue)) {
return NULL;
}
@@ -849,13 +832,13 @@ pcb_t *pcb_queue_peek( const pcb_queue_t queue ) {
**
** @param pcb Pointer to the PCB of the process to be scheduled
*/
-void schedule( pcb_t *pcb ) {
-
+void schedule(pcb_t *pcb)
+{
// sanity check
- assert1( pcb != NULL );
+ assert1(pcb != NULL);
// check for a killed process
- if( pcb->state == STATE_KILLED ) {
+ if (pcb->state == STATE_KILLED) {
// TODO figure out what to do now
return;
}
@@ -864,8 +847,8 @@ void schedule( pcb_t *pcb ) {
pcb->state = STATE_READY;
// add it to the ready queue
- if( pcb_queue_insert(ready,pcb) != SUCCESS ) {
- PANIC( 0, "schedule insert fail" );
+ if (pcb_queue_insert(ready, pcb) != SUCCESS) {
+ PANIC(0, "schedule insert fail");
}
}
@@ -874,16 +857,16 @@ void schedule( pcb_t *pcb ) {
**
** Select the next process to receive the CPU
*/
-void dispatch( void ) {
-
+void dispatch(void)
+{
// verify that there is no current process
- assert( current == NULL );
+ assert(current == NULL);
// grab whoever is at the head of the queue
- int status = pcb_queue_remove( ready, &current );
- if( status != SUCCESS ) {
- sprint( b256, "dispatch queue remove failed, code %d", status );
- PANIC( 0, b256 );
+ int status = pcb_queue_remove(ready, &current);
+ if (status != SUCCESS) {
+ sprint(b256, "dispatch queue remove failed, code %d", status);
+ PANIC(0, b256);
}
// set the process up for success
@@ -891,7 +874,6 @@ void dispatch( void ) {
current->ticks = QUANTUM_STANDARD;
}
-
/*
** Debugging/tracing routines
*/
@@ -904,32 +886,32 @@ void dispatch( void ) {
** @param msg[in] An optional message to print before the dump
** @param c[in] The context to dump out
*/
-void ctx_dump( const char *msg, register context_t *c ) {
-
+void ctx_dump(const char *msg, register context_t *c)
+{
// first, the message (if there is one)
- if( msg ) {
- cio_puts( msg );
+ if (msg) {
+ cio_puts(msg);
}
// the pointer
- cio_printf( " @ %08x: ", (uint32_t) c );
+ cio_printf(" @ %08x: ", (uint32_t)c);
// if it's NULL, why did you bother calling me?
- if( c == NULL ) {
- cio_puts( " NULL???\n" );
+ if (c == NULL) {
+ cio_puts(" NULL???\n");
return;
}
// now, the contents
- cio_printf( "ss %04x gs %04x fs %04x es %04x ds %04x cs %04x\n",
- c->ss & 0xff, c->gs & 0xff, c->fs & 0xff,
- c->es & 0xff, c->ds & 0xff, c->cs & 0xff );
- cio_printf( " edi %08x esi %08x ebp %08x esp %08x\n",
- c->edi, c->esi, c->ebp, c->esp );
- cio_printf( " ebx %08x edx %08x ecx %08x eax %08x\n",
- c->ebx, c->edx, c->ecx, c->eax );
- cio_printf( " vec %08x cod %08x eip %08x eflags %08x\n",
- c->vector, c->code, c->eip, c->eflags );
+ cio_printf("ss %04x gs %04x fs %04x es %04x ds %04x cs %04x\n",
+ c->ss & 0xff, c->gs & 0xff, c->fs & 0xff, c->es & 0xff,
+ c->ds & 0xff, c->cs & 0xff);
+ cio_printf(" edi %08x esi %08x ebp %08x esp %08x\n", c->edi, c->esi,
+ c->ebp, c->esp);
+ cio_printf(" ebx %08x edx %08x ecx %08x eax %08x\n", c->ebx, c->edx,
+ c->ecx, c->eax);
+ cio_printf(" vec %08x cod %08x eip %08x eflags %08x\n", c->vector, c->code,
+ c->eip, c->eflags);
}
/**
@@ -939,19 +921,19 @@ void ctx_dump( const char *msg, register context_t *c ) {
**
** @param msg[in] Optional message to print
*/
-void ctx_dump_all( const char *msg ) {
-
- if( msg != NULL ) {
- cio_puts( msg );
+void ctx_dump_all(const char *msg)
+{
+ if (msg != NULL) {
+ cio_puts(msg);
}
int n = 0;
register pcb_t *pcb = ptable;
- for( int i = 0; i < N_PROCS; ++i, ++pcb ) {
- if( pcb->state != STATE_UNUSED ) {
+ for (int i = 0; i < N_PROCS; ++i, ++pcb) {
+ if (pcb->state != STATE_UNUSED) {
++n;
- cio_printf( "%2d(%d): ", n, pcb->pid );
- ctx_dump( NULL, pcb->context );
+ cio_printf("%2d(%d): ", n, pcb->pid);
+ ctx_dump(NULL, pcb->context);
}
}
}
@@ -965,47 +947,46 @@ void ctx_dump_all( const char *msg ) {
** @param pcb[in] The PCB to dump
** @param all[in] Dump all the contents?
*/
-void pcb_dump( const char *msg, register pcb_t *pcb, bool_t all ) {
-
+void pcb_dump(const char *msg, register pcb_t *pcb, bool_t all)
+{
// first, the message (if there is one)
- if( msg ) {
- cio_puts( msg );
+ if (msg) {
+ cio_puts(msg);
}
// the pointer
- cio_printf( " @ %08x:", (uint32_t) pcb );
+ cio_printf(" @ %08x:", (uint32_t)pcb);
// if it's NULL, why did you bother calling me?
- if( pcb == NULL ) {
- cio_puts( " NULL???\n" );
+ if (pcb == NULL) {
+ cio_puts(" NULL???\n");
return;
}
- cio_printf( " %d", pcb->pid );
- cio_printf( " %s",
- pcb->state >= N_STATES ? "???" : state_str[pcb->state] );
+ cio_printf(" %d", pcb->pid);
+ cio_printf(" %s", pcb->state >= N_STATES ? "???" : state_str[pcb->state]);
- if( !all ) {
+ if (!all) {
// just printing IDs and states on one line
return;
}
// now, the rest of the contents
- cio_printf( " %s",
- pcb->priority >= N_PRIOS ? "???" : prio_str[pcb->priority] );
+ cio_printf(" %s",
+ pcb->priority >= N_PRIOS ? "???" : prio_str[pcb->priority]);
- cio_printf( " ticks %u xit %d wake %08x\n",
- pcb->ticks, pcb->exit_status, pcb->wakeup );
+ cio_printf(" ticks %u xit %d wake %08x\n", pcb->ticks, pcb->exit_status,
+ pcb->wakeup);
- cio_printf( " parent %08x", (uint32_t)pcb->parent );
- if( pcb->parent != NULL ) {
- cio_printf( " (%u)", pcb->parent->pid );
+ cio_printf(" parent %08x", (uint32_t)pcb->parent);
+ if (pcb->parent != NULL) {
+ cio_printf(" (%u)", pcb->parent->pid);
}
- cio_printf( " next %08x context %08x pde %08x", (uint32_t) pcb->next,
- (uint32_t) pcb->context, (uint32_t) pcb->pdir );
+ cio_printf(" next %08x context %08x pde %08x", (uint32_t)pcb->next,
+ (uint32_t)pcb->context, (uint32_t)pcb->pdir);
- cio_putchar( '\n' );
+ cio_putchar('\n');
}
/**
@@ -1017,36 +998,36 @@ void pcb_dump( const char *msg, register pcb_t *pcb, bool_t all ) {
** @param queue[in] The queue to dump
** @param contents[in] Also dump (some) contents?
*/
-void pcb_queue_dump( const char *msg, pcb_queue_t queue, bool_t contents ) {
-
+void pcb_queue_dump(const char *msg, pcb_queue_t queue, bool_t contents)
+{
// report on this queue
- cio_printf( "%s: ", msg );
- if( queue == NULL ) {
- cio_puts( "NULL???\n" );
+ cio_printf("%s: ", msg);
+ if (queue == NULL) {
+ cio_puts("NULL???\n");
return;
}
// first, the basic data
- cio_printf( "head %08x tail %08x",
- (uint32_t) queue->head, (uint32_t) queue->tail );
+ cio_printf("head %08x tail %08x", (uint32_t)queue->head,
+ (uint32_t)queue->tail);
// next, how the queue is ordered
- cio_printf( " order %s\n",
- queue->order >= N_ORDERINGS ? "????" : ord_str[queue->order] );
+ cio_printf(" order %s\n",
+ queue->order >= N_ORDERINGS ? "????" : ord_str[queue->order]);
// if there are members in the queue, dump the first few PIDs
- if( contents && queue->head != NULL ) {
- cio_puts( " PIDs: " );
+ if (contents && queue->head != NULL) {
+ cio_puts(" PIDs: ");
pcb_t *tmp = queue->head;
- for( int i = 0; i < 5 && tmp != NULL; ++i, tmp = tmp->next ) {
- cio_printf( " [%u]", tmp->pid );
+ for (int i = 0; i < 5 && tmp != NULL; ++i, tmp = tmp->next) {
+ cio_printf(" [%u]", tmp->pid);
}
- if( tmp != NULL ) {
- cio_puts( " ..." );
+ if (tmp != NULL) {
+ cio_puts(" ...");
}
- cio_putchar( '\n' );
+ cio_putchar('\n');
}
}
@@ -1058,50 +1039,48 @@ void pcb_queue_dump( const char *msg, pcb_queue_t queue, bool_t contents ) {
** @param msg[in] Optional message to print
** @param all[in] Dump all or only part of the relevant data
*/
-void ptable_dump( const char *msg, bool_t all ) {
-
- if( msg ) {
- cio_puts( msg );
+void ptable_dump(const char *msg, bool_t all)
+{
+ if (msg) {
+ cio_puts(msg);
}
- cio_putchar( ' ' );
+ cio_putchar(' ');
int used = 0;
int empty = 0;
register pcb_t *pcb = ptable;
- for( int i = 0; i < N_PROCS; ++i ) {
- if( pcb->state == STATE_UNUSED ) {
-
+ for (int i = 0; i < N_PROCS; ++i) {
+ if (pcb->state == STATE_UNUSED) {
// an empty slot
++empty;
} else {
-
// a non-empty slot
++used;
// if not dumping everything, add commas if needed
- if( !all && used ) {
- cio_putchar( ',' );
+ if (!all && used) {
+ cio_putchar(',');
}
// report the table slot #
- cio_printf( " #%d:", i );
+ cio_printf(" #%d:", i);
// and dump the contents
- pcb_dump( NULL, pcb, all );
+ pcb_dump(NULL, pcb, all);
}
}
// only need this if we're doing one-line output
- if( !all ) {
- cio_putchar( '\n' );
+ if (!all) {
+ cio_putchar('\n');
}
// sanity check - make sure we saw the correct number of table slots
- if( (used + empty) != N_PROCS ) {
- cio_printf( "Table size %d, used %d + empty %d = %d???\n",
- N_PROCS, used, empty, used + empty );
+ if ((used + empty) != N_PROCS) {
+ cio_printf("Table size %d, used %d + empty %d = %d???\n", N_PROCS, used,
+ empty, used + empty);
}
}
@@ -1111,14 +1090,15 @@ void ptable_dump( const char *msg, bool_t all ) {
** Prints basic information about the process table (number of
** entries, number with each process state, etc.).
*/
-void ptable_dump_counts( void ) {
+void ptable_dump_counts(void)
+{
uint_t nstate[N_STATES] = { 0 };
uint_t unknown = 0;
int n = 0;
pcb_t *ptr = ptable;
- while( n < N_PROCS ) {
- if( ptr->state < 0 || ptr->state >= N_STATES ) {
+ while (n < N_PROCS) {
+ if (ptr->state < 0 || ptr->state >= N_STATES) {
++unknown;
} else {
++nstate[ptr->state];
@@ -1127,10 +1107,10 @@ void ptable_dump_counts( void ) {
++ptr;
}
- cio_printf( "Ptable: %u ***", unknown );
- for( n = 0; n < N_STATES; ++n ) {
- cio_printf( " %u %s", nstate[n],
- state_str[n] != NULL ? state_str[n] : "???" );
+ cio_printf("Ptable: %u ***", unknown);
+ for (n = 0; n < N_STATES; ++n) {
+ cio_printf(" %u %s", nstate[n],
+ state_str[n] != NULL ? state_str[n] : "???");
}
- cio_putchar( '\n' );
+ cio_putchar('\n');
}