Added descriptor class ID generator-and-reserver

This commit is contained in:
Tristan B. Velloza Kildaire 2022-01-13 00:07:48 +02:00
parent d657c36ec6
commit 0d677f8610
1 changed files with 83 additions and 0 deletions

View File

@ -100,6 +100,89 @@ public abstract class Descriptor
}
/**
* TODO: Add comment
*
* This method is not thread safe, it is only to
* be called from thread safe functions that
* correctly lock the queue
*/
private static bool isDescIDInUse(ulong descID)
{
foreach(ulong descIDCurr; descQueue)
{
if(descID == descIDCurr)
{
return true;
}
}
return false;
}
unittest
{
try
{
while(true)
{
addDescQueue();
}
assert(false);
}
catch(JobException e)
{
assert(true);
}
}
/**
* Finds the next valid descriptor class ID,
* reserves it and returns it
*/
private static ulong addDescQueue()
{
ulong chosenID = 0;
bool zeroHit = true;
descQueueLock.lock();
while(true)
{
if(chosenID)
{
if(zeroHit)
{
zeroHit = false;
}
/* Overflowed, no space available */
else
{
descQueueLock.unlock();
throw new JobException("No descriptor class IDs available");
}
}
if(isDescIDInUse(chosenID))
{
chosenID++;
}
else
{
break;
}
}
descQueue ~= chosenID;
descQueueLock.unlock();
return chosenID;
}
private ulong descriptorClass;