TD2 I.6+ Implement thread counter

This commit is contained in:
Yorick Barbanneau 2021-11-16 21:51:00 +01:00
parent 1a97a8aef5
commit 615d121a76
4 changed files with 26 additions and 9 deletions

View file

@ -41,8 +41,8 @@ CFLAGS += -fsanitize=undefined
LDFLAGS += -fsanitize=undefined LDFLAGS += -fsanitize=undefined
# décommenté TD1 - partie V # décommenté TD1 - partie V
#CFLAGS += -fsanitize=address CFLAGS += -fsanitize=address
#LDFLAGS += -fsanitize=address -lpthread LDFLAGS += -fsanitize=address -lpthread
endif endif
ifeq ($(NACHOS_SYS),MAC_OS_SYS) ifeq ($(NACHOS_SYS),MAC_OS_SYS)

View file

@ -128,6 +128,11 @@ AddrSpace::AddrSpace (OpenFile * executable)
pageTable[0].valid = FALSE; // Catch NULL dereference pageTable[0].valid = FALSE; // Catch NULL dereference
#ifdef CHANGED
DEBUG('x', "Initialise thread counter\n");
Threads = 1;
#endif //CHANGED
AddrSpaceList.Append(this); AddrSpaceList.Append(this);
} }

View file

@ -41,6 +41,7 @@ class AddrSpace:public dontcopythis
// Dump program layout as SVG // Dump program layout as SVG
unsigned NumPages() { return numPages; } unsigned NumPages() { return numPages; }
#ifdef CHANGED #ifdef CHANGED
int Threads; // count number of threads into address space
int AllocateUserStack(); int AllocateUserStack();
#endif #endif
private: private:

View file

@ -7,7 +7,7 @@
static void StartUserThread( void * args ){ static void StartUserThread( void * args ){
DEBUG('x', "Enter StartUserThread function\n"); DEBUG('x', "Enter StartUserThread function\n");
// retrieve function and arguments // retrieve function and arguments
// create a ThreadArgs_t struct and cast args content into it // create a ThreadArgs_t struct and cast args content into it
@ -15,7 +15,7 @@ static void StartUserThread( void * args ){
ThreadArgs_t * cpy_args; ThreadArgs_t * cpy_args;
cpy_args = (ThreadArgs_t *) args; cpy_args = (ThreadArgs_t *) args;
int stack_addr = currentThread->space->AllocateUserStack(); int stack_addr = currentThread->space->AllocateUserStack();
// init register // init register
for (int i = 0; i < NumTotalRegs; i++ ) { for (int i = 0; i < NumTotalRegs; i++ ) {
machine->WriteRegister(i, 0); machine->WriteRegister(i, 0);
@ -25,7 +25,7 @@ static void StartUserThread( void * args ){
machine->WriteRegister(PCReg, cpy_args->f); machine->WriteRegister(PCReg, cpy_args->f);
machine->WriteRegister (NextPCReg, machine->ReadRegister(PCReg) + 4); machine->WriteRegister (NextPCReg, machine->ReadRegister(PCReg) + 4);
DEBUG('x',"PCReg: 0x%x, NextPCReg: 0x%x\n", cpy_args->f, cpy_args->f + 4); DEBUG('x',"PCReg: 0x%x, NextPCReg: 0x%x\n", cpy_args->f, cpy_args->f + 4);
// init register 4: arguments // init register 4: arguments
machine->WriteRegister(4, cpy_args->arg); machine->WriteRegister(4, cpy_args->arg);
DEBUG('x',"Register 4: 0x%x\n", cpy_args->arg); DEBUG('x',"Register 4: 0x%x\n", cpy_args->arg);
@ -34,6 +34,10 @@ static void StartUserThread( void * args ){
machine->WriteRegister(StackReg, stack_addr); machine->WriteRegister(StackReg, stack_addr);
DEBUG('x',"StackRegister: 0x%x\n", stack_addr); DEBUG('x',"StackRegister: 0x%x\n", stack_addr);
// All our registers have values, we can desallocate our cpy_args
free(cpy_args);
cpy_args = NULL;
// run our thread // run our thread
machine->Run(); machine->Run();
} }
@ -41,24 +45,31 @@ static void StartUserThread( void * args ){
int do_ThreadCreate(int f, int arg){ int do_ThreadCreate(int f, int arg){
DEBUG('x',"Enter do_ThreadCreate function\n"); DEBUG('x',"Enter do_ThreadCreate function\n");
// Initialize our structure we'll pass to StartUserThread // Initialize our structure we'll pass to StartUserThread
ThreadArgs_t * args = (ThreadArgs_t *) malloc(sizeof(ThreadArgs_t)); ThreadArgs_t * args = (ThreadArgs_t *) malloc(sizeof(ThreadArgs_t));
args->f = f; args->f = f;
args->arg = arg; args->arg = arg;
// create a new Thread and start it // create a new Thread and start it
Thread * newThread = new Thread("new thread"); Thread * newThread = new Thread("new thread");
// increment number of threads
currentThread->space->Threads++;
DEBUG('x', "Increase numbers of Threads:%d\n",currentThread->space->Threads);
newThread->space = currentThread->space; newThread->space = currentThread->space;
newThread->Start(StartUserThread, args); newThread->Start(StartUserThread, args);
return 0; return 0;
} }
void do_ThreadExit(){ void do_ThreadExit(){
DEBUG('x', "Enter do_ThreadExit function\n"); DEBUG('x', "Enter do_ThreadExit function\n");
// TODO: what should we do with thread space? // TODO: what should we do with thread space?
// Probalely desallocate it... // Probalely desallocate it... if no threads remain
currentThread->space->Threads--;
DEBUG('x', "Decrease numbers of Threads:%d\n",currentThread->space->Threads);
currentThread->Finish(); currentThread->Finish();
} }
#endif #endif