Skip to content

Commit

Permalink
Fix a momentum problem that forced scheduler to call the same thread …
Browse files Browse the repository at this point in the history
…2x even in priority 0

New Insolation method directly on destination
  • Loading branch information
solariun committed Mar 22, 2020
1 parent 49f8326 commit 9d0aa92
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 40 deletions.
56 changes: 34 additions & 22 deletions CorePartition/CorePartition.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ bool CorePartition_CreateThread_ (void(*pFunction)(void*), void* pValue, size_t
pThreadLight[nThread].mem.func.pFunction = pFunction;

pThreadLight[nThread].nNice = 1;

pThreadLight [nThread].nLastMomentun = getCTime();

pThreadLight [nThread].nExecTime = 0;

Expand All @@ -209,6 +207,9 @@ bool CorePartition_CreateThread_ (void(*pFunction)(void*), void* pValue, size_t
pThreadLight [nThread].nIsolation = nTaskIsolation;

pThreadLight [nThread].nLastBackup = getCTime ();

pThreadLight [nThread].nLastMomentun = getCTime();

nThreadCount++;

return true;
Expand All @@ -226,31 +227,40 @@ bool CorePartition_CreateThread (void(*pFunction)(void*), void* pValue, size_t n
}


inline static void fastmemcpy (void* pDestine, const void* pSource, size_t nSize)

static void CryptBlob (uint8_t* pBlob, size_t nSize)
{
const void* nTop = (const void*) pSource + nSize;

if (pThreadLight [nCurrentThread].nIsolation != 0)
if (pThreadLight [nCurrentThread].nIsolation)
{
uint32_t nKey [4];

srand(pThreadLight [nCurrentThread].nLastBackup);

for (;pSource <= nTop; pSource++, pDestine++) *((uint8_t*) pDestine) = *((uint8_t*) pSource) ^ ((uint8_t) (rand() % 255) );
nKey[0] = (uint32_t) rand();
nKey[1] = (uint32_t) rand();
nKey[2] = (uint32_t) rand();
nKey[3] = (uint32_t) rand();

while (nSize-- > 0)
{
*pBlob = *pBlob ^ ((uint8_t*)nKey) [(nSize % sizeof (nKey))];
pBlob++;
}
}
else
memcpy(pDestine, pSource, nSize);
}

inline static void BackupStack(void)

static void BackupStack(void)
{
memcpy (pThreadLight [nCurrentThread].pnStackPage, pThreadLight [nCurrentThread].pLastStack, pThreadLight [nCurrentThread].nStackSize);

//pThreadLight [nCurrentThread].nProcTime = getCTime () - pThreadLight [nCurrentThread].nLastBackup;
CryptBlob(pThreadLight [nCurrentThread].pnStackPage, pThreadLight [nCurrentThread].nStackSize);
}


inline static void RestoreStack(void)
static void RestoreStack(void)
{
memcpy (pThreadLight [nCurrentThread].pLastStack, pThreadLight [nCurrentThread].pnStackPage, pThreadLight [nCurrentThread].nStackSize);
CryptBlob(pThreadLight [nCurrentThread].pLastStack, pThreadLight [nCurrentThread].nStackSize);
}


Expand All @@ -271,13 +281,13 @@ inline static void SleepBeforeTask (uint32_t nCurTime)
else if (__NEXTIME (nCThread) <= nCurTime)
{
nMin = 0;
nCurrentThread = nCThread;
break;
//nCurrentThread = nCThread;
return;
}
else if (nMin > __CALC (nCThread))
{
nCurrentThread = nCThread;
nMin = __CALC (nCThread) + 1;
//nCurrentThread = nCThread;
nMin = __CALC (nCThread);
}
}

Expand All @@ -288,16 +298,19 @@ inline static void SleepBeforeTask (uint32_t nCurTime)

inline static size_t Scheduler (void)
{
static uint32_t nCounter = 0;
static uint32_t nTime = 0;

if (nTime == 0) nTime = getTime();

pThreadLight [nCurrentThread].nExecTime = getTime() - pThreadLight [nCurrentThread].nLastMomentun;

nCurrentThread++;

while (1)
{
if (nCounter < nMaxThreads)
//printf ("nMaxThread: [%zu] - nCurrentThread: [%zu]: LastMomentun + nice: [%u], time: [%u] -> greater: [%s]\n", nMaxThreads, nCurrentThread, pThreadLight [nCurrentThread].nLastMomentun + pThreadLight [nCurrentThread].nNice, nTime, nTime >= pThreadLight [nCurrentThread].nLastMomentun + pThreadLight [nCurrentThread].nNice ? "TRUE" : "FASE");

if (nCurrentThread < nMaxThreads)
{
if (nTime >= ((uint32_t)(pThreadLight [nCurrentThread].nLastMomentun + pThreadLight [nCurrentThread].nNice)))
{
Expand All @@ -306,12 +319,11 @@ inline static size_t Scheduler (void)
return nCurrentThread;
}

nCurrentThread = (nCurrentThread + 1) >= nMaxThreads ? 0 : nCurrentThread+1;
nCounter++;
nCurrentThread++;
}
else
{
nCounter = 0;
nCurrentThread = 0;
SleepBeforeTask (nTime);
nTime = getTime();
srand(nTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ void __attribute__ ((noinline)) ShowRunningThreads ()
Serial.print (nCount);
Serial.print (F("\t"));
Serial.print (CorePartition_GetStatusByID (nCount));
Serial.print (CorePartition_IsSecureByID (nCount));
Serial.print (F("\t"));
Serial.print (CorePartition_GetNiceByID (nCount));
Serial.print (F("\t"));
Expand Down Expand Up @@ -501,7 +502,7 @@ void setup()

CorePartition_SetStackOverflowHandler (StackOverflowHandler);

CorePartition_CreateThread (Thread1, NULL, 150, 0);
CorePartition_CreateSecureThread (Thread1, NULL, 150, 0);

CorePartition_CreateThread (Thread3, NULL, 150, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ void __attribute__ ((noinline)) ShowRunningThreads ()
Serial.print (nCount);
Serial.print (F("\t"));
Serial.print (CorePartition_GetStatusByID (nCount));
Serial.print (CorePartition_IsSecureByID (nCount));
Serial.print (F("\t"));
Serial.print (CorePartition_GetNiceByID (nCount));
Serial.print (F("\t"));
Expand Down Expand Up @@ -726,7 +727,7 @@ void setup()

CorePartition_CreateThread (WalkerSign, NULL, 30 * sizeof (size_t), 500);

CorePartition_CreateThread (TraficLightKernel, NULL, 30 * sizeof (size_t), 250);
CorePartition_CreateSecureThread (TraficLightKernel, NULL, 30 * sizeof (size_t), 250);

CorePartition_CreateThread (Terminal, NULL, 42 * sizeof (size_t), 50);
}
Expand Down
25 changes: 16 additions & 9 deletions CorePartition/Resources/Thread/Thread.ino
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,15 @@ void ShowRunningThreads ()
Serial.print (F("\t"));
Serial.print (CorePartition_GetLastDutyCycleByID (nCount));
Serial.println ("ms");

Serial.flush ();
}
}


void ThreadTOP (void* pValue)
{
uint8_t nCounter = 0;
int nCounter = 0;

while (CorePartition_Yield ())
{
Expand Down Expand Up @@ -440,8 +442,13 @@ void StackOverflowHandler ()
ShowRunningThreads ();
Serial.flush ();

while (true);
}
while (true)
{
Serial.print (".");
delay (1);
}
};



void setup()
Expand Down Expand Up @@ -494,15 +501,15 @@ void setup()



CorePartition_CreateSecureThread (ThreadTOP, NULL, 13 * sizeof (size_t), 500);

CorePartition_CreateThread (Thread1, NULL, 25 * sizeof (size_t), 300);
CorePartition_CreateThread (Thread1, NULL, 25 * sizeof (size_t), 0);

CorePartition_CreateThread (Thread2, NULL, 30 * sizeof (size_t), 400);
CorePartition_CreateSecureThread (ThreadTOP, NULL, 13 * sizeof (size_t), 0);

CorePartition_CreateThread (Thread2, NULL, 30 * sizeof (size_t), 0);

CorePartition_CreateSecureThread (Thread3, NULL, 25 * sizeof (size_t), 1000);
CorePartition_CreateSecureThread (Thread3, NULL, 25 * sizeof (size_t), 0);

CorePartition_CreateThread (Thread4, NULL, 25 * sizeof (size_t), 2000);
CorePartition_CreateThread (Thread4, NULL, 25 * sizeof (size_t), 0);


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ void __attribute__ ((noinline)) ShowRunningThreads ()
Serial.print (nCount);
Serial.print (F("\t"));
Serial.print (CorePartition_GetStatusByID (nCount));
Serial.print (CorePartition_IsSecureByID (nCount));
Serial.print (F("\t"));
Serial.print (CorePartition_GetNiceByID (nCount));
Serial.print (F("\t"));
Expand Down
10 changes: 3 additions & 7 deletions CorePartition/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ void Thread3 (void* pValue)

static void sleepMSTicks (uint32_t nSleepTime)
{
printf ("\n%s: sleeping [%lu]\n", __FUNCTION__, nSleepTime);

usleep ((useconds_t) nSleepTime * 1000);

printf ("Returning....");
}

static uint32_t getMsTicks(void)
Expand All @@ -150,9 +146,9 @@ int main(int argc, const char * argv[])
CorePartition_SetSleepTimeInterface (sleepMSTicks);
CorePartition_SetStackOverflowHandler (StackOverflowHandler);

CorePartition_CreateThread (Thread1, NULL, 256, 3000);
CorePartition_CreateThread (Thread2, NULL, 256, 1000);
CorePartition_CreateSecureThread (Thread3, NULL, 256, 500);
CorePartition_CreateThread (Thread1, NULL, 256, 1000);
CorePartition_CreateThread (Thread2, NULL, 256, 600);
CorePartition_CreateSecureThread (Thread3, NULL, 256, 1212);

CorePartition_Join();

Expand Down

0 comments on commit 9d0aa92

Please sign in to comment.