From 5cb81cb3f368af9f8aae596682f207addbc803e7 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 9 Sep 2022 09:47:08 -0400 Subject: [PATCH] added task activation spinlock --- tasks.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/tasks.c b/tasks.c index 6cb1ca3..0ebe9f8 100644 --- a/tasks.c +++ b/tasks.c @@ -42,7 +42,7 @@ static CpuState_T* Running = NULL; static int CpuCount; /*************************************** - Lock Operations + Lock and Condition Operations ***************************************/ static void AcquireLock(void) { @@ -62,6 +62,16 @@ static void WaitForCondition(pthread_cond_t* cond, pthread_mutex_t* mutex, int m pthread_cond_timedwait(cond, mutex, &expire_time); } +static void WaitForTaskFree(Task_T* prev, Task_T* next) +{ + if (prev != next) + { + while (!next->stack_top) + { + } + } +} + /*************************************** Queue Operations ***************************************/ @@ -169,11 +179,13 @@ static void Yield(void) printf("CPU %d is yielding\n", CpuID); Task_T* prev = Running[CpuID].task; Running[CpuID].task = NULL; + prev->stack_top = NULL; Enter(prev); printf("CPU %d is picking task\n", CpuID); Running[CpuID].task = Select(); printf("CPU %d switching to task %p\n", CpuID, Running[CpuID].task); ReleaseLock(); + WaitForTaskFree(prev, Running[CpuID].task); SwapTask(prev, Running[CpuID].task); } @@ -181,12 +193,16 @@ void ExitTask(void) { puts("exiting"); AcquireLock(); - Enqueue(&DeadQueue, Running[CpuID].task); + printf("CPU %d is yielding\n", CpuID); + Task_T* prev = Running[CpuID].task; Running[CpuID].task = NULL; + prev->stack_top = NULL; + printf("CPU %d is picking task\n", CpuID); Running[CpuID].task = Select(); - printf("starting task %p\n", Running[CpuID].task); + printf("CPU %d switching to task %p\n", CpuID, Running[CpuID].task); ReleaseLock(); - StartTask(Running[CpuID].task); + WaitForTaskFree(prev, Running[CpuID].task); + SwapTask(prev, Running[CpuID].task); } static Task_T* CreateTask(void (*task_fn)(void*), void* arg, int stacksize) -- 2.54.0