]> git.mdlowis.com Git - proto/cerise-os.git/commitdiff
added task activation spinlock
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 9 Sep 2022 13:47:08 +0000 (09:47 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 9 Sep 2022 13:47:08 +0000 (09:47 -0400)
tasks.c

diff --git a/tasks.c b/tasks.c
index 6cb1ca3f7637195e9497b120b94edc20b17d4bba..0ebe9f82988ff3ebfc68d98136ca9652ac2ec8bc 100644 (file)
--- 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)