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
#ifdef CHANGED
int bitmapSize = UserStacksAreaSize / UserStackSize;
int bitmapSize =( UserStacksAreaSize / UserStackSize)-1;
DEBUG('x', "Initialise thread counter\n");
threads = 1;
@ -325,20 +325,26 @@ AddrSpace::AllocateUserStack()
/* If we don't have any free slot, we can wait for one to be
* 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;
DEBUG('x', "Allocate User Stack bit %d, addr:%x\n", bit, addr);
return addr;
}
void
AddrSpace::DeAllocateUserStack(int addr){
int memory = numPages * PageSize;
int memory = numPages * PageSize;
int bit = (memory - addr) / UserStackSize;
DEBUG('x', "Deallocate User Stack bit %d, addr:%x\n", bit, addr);
semAllocateUserStack->P();
memoryMap->Clear(bit);
semAllocateUserStack->V();
semAllocateUserStack->P();
}
#endif

View file

@ -15,7 +15,6 @@ static void StartUserThread( void * args ){
// because we only have a void in our function definition
ThreadArgs_t * cpy_args;
cpy_args = (ThreadArgs_t *) args;
int stack_addr = currentThread->space->AllocateUserStack();
// init register
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);
// init stack
machine->WriteRegister(StackReg, stack_addr);
DEBUG('x',"StackRegister: 0x%x\n", stack_addr);
machine->WriteRegister(StackReg, cpy_args->stackAddr);
DEBUG('x',"StackRegister: 0x%x\n", cpy_args->stackAddr);
// All our registers have values, we can desallocate our cpy_args
free(cpy_args);
@ -52,6 +51,12 @@ int do_ThreadCreate(int f, int arg){
args->f = f;
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
Thread * newThread = new Thread("new thread");

View file

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