From 39b1f14b29ba578cc75475f42a5cee864394a97d Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 8 Sep 2022 23:25:59 -0400 Subject: [PATCH] minor refactoring --- tasks.c | 87 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/tasks.c b/tasks.c index b54d65d..6cb1ca3 100644 --- a/tasks.c +++ b/tasks.c @@ -36,7 +36,6 @@ typedef struct { static pthread_mutex_t ScheduleLock; static pthread_cond_t ScheduleCond; static __thread int CpuID = 0; -static long int TaskCount = 0; static TaskQueue_T ReadyQueue = {0}; static TaskQueue_T DeadQueue = {0}; static CpuState_T* Running = NULL; @@ -178,12 +177,12 @@ static void Yield(void) SwapTask(prev, Running[CpuID].task); } -void FinalizeTask(void) +void ExitTask(void) { puts("exiting"); AcquireLock(); - TaskCount--; - Running[CpuID].task = NULL; // TODO: Enqueue to dead queue + Enqueue(&DeadQueue, Running[CpuID].task); + Running[CpuID].task = NULL; Running[CpuID].task = Select(); printf("starting task %p\n", Running[CpuID].task); ReleaseLock(); @@ -196,7 +195,7 @@ static Task_T* CreateTask(void (*task_fn)(void*), void* arg, int stacksize) Task_T* task = calloc(1, sizeof(Task_T)); task->stack_base = calloc(stacksize/sizeof(long), sizeof(long)); task->stack_top = &task->stack_base[stacksize/sizeof(long)-1]; // top of stack - *(--task->stack_top) = (long)FinalizeTask; // coroutine cleanup + *(--task->stack_top) = (long)ExitTask; // coroutine cleanup *(--task->stack_top) = (long)task_fn; // user's function to run (rop style!) *(--task->stack_top) = (long)arg; // user's function argument (rdi) for (int saved = 0; saved < 6; saved++) @@ -212,7 +211,6 @@ void SpawnTask(void (*task_fn)(void*), void* arg, int stacksize) Task_T* task = CreateTask(task_fn, arg, stacksize); /* put task in the ready queue */ AcquireLock(); - TaskCount++; Enter(task); ReleaseLock(); } @@ -225,7 +223,7 @@ static void CpuIdle(void* arg) Yield(); printf("CPU %d is idle\n", CpuID); AcquireLock(); - WaitForCondition(&ScheduleCond, &ScheduleLock, 1); + WaitForCondition(&ScheduleCond, &ScheduleLock, 10); ReleaseLock(); } } @@ -240,6 +238,40 @@ static void* CpuMain(void* arg) return NULL; /* unreachable */ } +void Init(void) +{ + pthread_mutex_init(&ScheduleLock, 0); + pthread_cond_init(&ScheduleCond, NULL); + CpuCount = sysconf(_SC_NPROCESSORS_ONLN); + Running = calloc(CpuCount, sizeof(CpuState_T)); + for (long int i = 0; i < CpuCount; i++) + { + pthread_create(&Running[i].thread, NULL, CpuMain, (void*)i); + } +} + +void WaitForExit(void) +{ + while(1) + { + AcquireLock(); + WaitForCondition(&ScheduleCond, &ScheduleLock, 10); + if (!ReadyQueue.head) + { + bool done = true; + for (long int i = 0; i < CpuCount; i++) + { + done = done && (Running[i].task == Running[i].idle); + } + if (done) + { + break; + } + } + ReleaseLock(); + } +} + /*************************************** Main Routine ***************************************/ @@ -267,49 +299,14 @@ int main(int argc, char** argv) (void)argc; (void)argv; - pthread_mutex_init(&ScheduleLock, 0); - pthread_cond_init(&ScheduleCond, NULL); - CpuCount = sysconf(_SC_NPROCESSORS_ONLN); -// CpuCount = 1; - Running = calloc(CpuCount, sizeof(CpuState_T)); - for (long int i = 0; i < CpuCount; i++) - { - pthread_create(&Running[i].thread, NULL, CpuMain, (void*)i); - } + Init(); SpawnTask(task1, 0, 0); SpawnTask(task2, 0, 0); /* wait for all jobs to be done */ - while(1) - { - AcquireLock(); - WaitForCondition(&ScheduleCond, &ScheduleLock, 10); -// printf("%ld %d %p\n", TaskCount, CpuCount, ReadyQueue.head); - if (TaskCount == 0) - { - break; - } -// else - { - printf("count: %ld\n", TaskCount); - printf("head: %p\n", ReadyQueue.head); - printf("tasks:\n"); - for (long int i = 0; i < CpuCount; i++) - { - if (Running[i].task == Running[i].idle) - { - printf(" %02ld idle\n", i); - } - else - { - printf(" %02ld %p\n", i, Running[i].task); - } - } - - } - ReleaseLock(); - } + WaitForExit(); puts("done"); + return 0; } \ No newline at end of file -- 2.54.0