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

@ -21,7 +21,10 @@
#include "noff.h"
#include "syscall.h"
#include "new"
#ifdef CHANGED
#include "synch.h"
#include "bitmap.h"
#endif //CHANGED
//----------------------------------------------------------------------
// SwapHeader
// Do little endian to big endian conversion on the bytes in the
@ -129,8 +132,17 @@ AddrSpace::AddrSpace (OpenFile * executable)
pageTable[0].valid = FALSE; // Catch NULL dereference
#ifdef CHANGED
int bitmapSize = UserStacksAreaSize / UserStackSize;
DEBUG('x', "Initialise thread counter\n");
Threads = 1;
threads = 1;
DEBUG('x', "Initialise semaphores\n");
semThreadsCounter = new Semaphore("AddrSpace_thread_counter", 1);
semAllocateUserStack = new Semaphore("Stack Avaiable", 1);
DEBUG('x', "Initialize memory map size:%d", bitmapSize);
memoryMap = new BitMap(bitmapSize);
memoryMap->Mark(0);
#endif //CHANGED
AddrSpaceList.Append(this);
@ -147,6 +159,11 @@ AddrSpace::~AddrSpace ()
pageTable = NULL;
AddrSpaceList.Remove(this);
#ifdef CHANGED
delete semThreadsCounter;
delete semAllocateUserStack;
delete memoryMap;
#endif
}
//----------------------------------------------------------------------
@ -303,6 +320,25 @@ int
AddrSpace::AllocateUserStack()
{
int memory = numPages * PageSize;
return memory - 256;
int bit;
/* 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();
}
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 bit = (memory - addr) / UserStackSize;
DEBUG('x', "Deallocate User Stack bit %d, addr:%x\n", bit, addr);
memoryMap->Clear(bit);
semAllocateUserStack->V();
}
#endif