void thread_cleanup( void ) { head->isTerminated = 1; if( head->joining != NULL ) { TCB* temp = head; head = head->next; temp->next = NULL; TCB* cur = head; while( cur->next != NULL ) cur = cur->next; cur->next = temp->joining; asm_yield( NULL, head ); return; } else { TCB* temp = head; head = head->next; temp->next = NULL; if( nextFreeingStack != NULL ) free( nextFreeingStack ); nextFreeingStack = temp->stk_base; free( temp ); asm_yield( NULL, head ); } }
int thread_join(long thread_ID) { isCritical = 1; if( !isCreated ) { initialize_head( ); } TCB* cur = (TCB*)thread_ID; if( cur == NULL ) return -3; if( cur->joining != NULL ) return -2; TCB* temp = head; while( temp != NULL ) { if( temp == cur ) return -1; temp = temp->joining; } if( !(cur->isTerminated) ) { TCB* prev = head; prev = head; cur->joining = head; head = head->next; asm_yield( prev, head ); } isCritical = 0; return 0; }
void thread_yield(void) { isCritical = 1; if( !isCreated ) { initialize_head( ); } if( head -> next == NULL ) return; TCB *temp = head; head = head->next; TCB* cur = head; while( cur->next != NULL ) cur = cur->next; cur->next = temp; temp->next = NULL; asm_yield( temp, head ); isCritical = 0; }
/*This function moves the current running thread to the end of the ready queue. The new thread at the head of the queue is given the cpu*/ void thread_yield(void) { critFlag = 1; if(!initDone) { doInit(); initDone = 1; } // if ready queue is empty, return (how do I check if the ready queue is empty?) head -> next == NULL? end == head? if(head -> next == NULL) { //I want the thread to keep running and simply not yield return; } else { //rotate ready queue. first element needs to be at the end and last element needs to be at the beginning struct TCB * endingVal = head; struct TCB * p = head -> next; struct TCB * traverse = head; while(traverse -> next != NULL) { traverse = traverse -> next; } traverse -> next = endingVal; traverse = traverse -> next; traverse -> next = NULL; end = traverse; head = p; struct TCB * cur = endingVal; struct TCB * next = head; //save current thread's state(register contents) in it's TCB and restore the next thread's state from it's TCB //asm-yield()? critFlag = 0; asm_yield(cur, next); } }