TD2 II.4 Call exit if there is no space avaible on Stack

Put Thread in sleep mode was a bad idea beause of synch (barrier for example)
This commit is contained in:
Yorick Barbanneau 2021-11-19 14:12:49 +01:00
parent 4da093ca38
commit 3877e32b59
3 changed files with 22 additions and 10 deletions

View file

@ -132,7 +132,7 @@ AddrSpace::AddrSpace (OpenFile * executable)
pageTable[0].valid = FALSE; // Catch NULL dereference pageTable[0].valid = FALSE; // Catch NULL dereference
#ifdef CHANGED #ifdef CHANGED
int bitmapSize = UserStacksAreaSize / UserStackSize; int bitmapSize =( UserStacksAreaSize / UserStackSize)-1;
DEBUG('x', "Initialise thread counter\n"); DEBUG('x', "Initialise thread counter\n");
threads = 1; threads = 1;
@ -325,20 +325,26 @@ AddrSpace::AllocateUserStack()
/* If we don't have any free slot, we can wait for one to be /* If we don't have any free slot, we can wait for one to be
* freed by DeAllocateUserstack * freed by DeAllocateUserstack
*/ */
while ((bit = memoryMap->Find()) == - 1){
DEBUG('x', "Can't allocate User Stack Wainting...\n"); semAllocateUserStack->P();
semAllocateUserStack->P(); bit = memoryMap->Find();
semAllocateUserStack->V();
if ( bit == - 1 ) {
DEBUG('x', "No slot avaible on User Stack\n");
return -1;
} }
int addr = memory - UserStackSize * bit; int addr = memory - UserStackSize * bit;
DEBUG('x', "Allocate User Stack bit %d, addr:%x\n", bit, addr); DEBUG('x', "Allocate User Stack bit %d, addr:%x\n", bit, addr);
return addr; return addr;
} }
void void
AddrSpace::DeAllocateUserStack(int addr){ AddrSpace::DeAllocateUserStack(int addr){
int memory = numPages * PageSize; int memory = numPages * PageSize;
int bit = (memory - addr) / UserStackSize; int bit = (memory - addr) / UserStackSize;
DEBUG('x', "Deallocate User Stack bit %d, addr:%x\n", bit, addr); DEBUG('x', "Deallocate User Stack bit %d, addr:%x\n", bit, addr);
semAllocateUserStack->P();
memoryMap->Clear(bit); memoryMap->Clear(bit);
semAllocateUserStack->V(); semAllocateUserStack->P();
} }
#endif #endif

View file

@ -15,7 +15,6 @@ static void StartUserThread( void * args ){
// because we only have a void in our function definition // because we only have a void in our function definition
ThreadArgs_t * cpy_args; ThreadArgs_t * cpy_args;
cpy_args = (ThreadArgs_t *) args; cpy_args = (ThreadArgs_t *) args;
int stack_addr = currentThread->space->AllocateUserStack();
// init register // init register
for (int i = 0; i < NumTotalRegs; i++ ) { for (int i = 0; i < NumTotalRegs; i++ ) {
@ -32,8 +31,8 @@ static void StartUserThread( void * args ){
DEBUG('x',"Register 4: 0x%x\n", cpy_args->arg); DEBUG('x',"Register 4: 0x%x\n", cpy_args->arg);
// init stack // init stack
machine->WriteRegister(StackReg, stack_addr); machine->WriteRegister(StackReg, cpy_args->stackAddr);
DEBUG('x',"StackRegister: 0x%x\n", stack_addr); DEBUG('x',"StackRegister: 0x%x\n", cpy_args->stackAddr);
// All our registers have values, we can desallocate our cpy_args // All our registers have values, we can desallocate our cpy_args
free(cpy_args); free(cpy_args);
@ -51,7 +50,13 @@ int do_ThreadCreate(int f, int arg){
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;
// Check if we can allocate Stack for our son
int stackAddr = currentThread->space->AllocateUserStack();
if ( stackAddr == -1 ) {
fprintf(stderr, "Segmentation Fault - no space avaible on stack\n");
Exit(1);
}
// 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");

View file

@ -6,6 +6,7 @@
typedef struct { typedef struct {
int f; int f;
int arg; int arg;
int stackAddr;
} ThreadArgs_t; } ThreadArgs_t;
extern int do_ThreadCreate(int f, int arg); extern int do_ThreadCreate(int f, int arg);