TD2 II.3-4 Manage UserStack with Bitmap

Add a Semaphore to manage threads waiting list
This commit is contained in:
Yorick Barbanneau 2021-11-18 23:12:42 +01:00
parent 6e0d91918a
commit 4da093ca38
4 changed files with 67 additions and 11 deletions

View file

@ -4,6 +4,7 @@
#include "userthread.h"
#include "syscall.h"
#include "addrspace.h"
#include "synch.h"
static void StartUserThread( void * args ){
@ -55,8 +56,10 @@ int do_ThreadCreate(int f, int arg){
Thread * newThread = new Thread("new thread");
// increment number of threads
currentThread->space->Threads++;
DEBUG('x', "Increase numbers of Threads:%d\n",currentThread->space->Threads);
currentThread->space->semThreadsCounter->P();
currentThread->space->threads++;
currentThread->space->semThreadsCounter->V();
DEBUG('x', "Increase numbers of Threads:%d\n",currentThread->space->threads);
newThread->space = currentThread->space;
newThread->Start(StartUserThread, args);
@ -69,8 +72,14 @@ void do_ThreadExit(){
// TODO: what should we do with thread space?
// Probalely desallocate it... if no threads remain
// currentThread->space->Threads--;
DEBUG('x', "Decrease numbers of Threads:%d\n",currentThread->space->Threads);
if ( --currentThread->space->Threads == 0){
currentThread->space->semThreadsCounter->P();
currentThread->space->threads--;
currentThread->space->semThreadsCounter->V();
int addr = machine->ReadRegister(StackReg);
currentThread->space->DeAllocateUserStack(addr);
DEBUG('x', "Decrease numbers of Threads:%d\n",currentThread->space->threads);
if ( currentThread->space->threads == 0 ){
// No threads remains, desallocate addrspace
delete currentThread->space;
interrupt->Powerdown();