/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "shell/ShellThreads.h" #include "utilities/AsyncTaskQueue.h" #include #define stemobject_implementation AsyncTaskQueue stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); struct AsyncTaskQueue_task { AsyncTaskQueue_callback workCallback; AsyncTaskQueue_callback completeCallback; void * context1; void * context2; }; struct AsyncTaskQueue_thread { ShellThread thread; ShellSemaphore semaphore; bool canceled; unsigned int taskCountMax; volatile unsigned int taskQueueHead; // Writable only by main thread volatile unsigned int taskQueueStartedTail; // Writable only by worker thread volatile unsigned int taskQueueCompletedTail; // Writable only by main thread struct AsyncTaskQueue_task * tasks; }; AsyncTaskQueue * AsyncTaskQueue_create(unsigned int threadCount, unsigned int taskCountMaxPerThread) { stemobject_create_implementation(init, threadCount, taskCountMaxPerThread) } bool AsyncTaskQueue_init(AsyncTaskQueue * self, unsigned int threadCount, unsigned int taskCountMaxPerThread) { call_super(init, self); self->threadCount = threadCount; self->taskCountMaxPerThread = taskCountMaxPerThread; self->threads = NULL; self->nextThreadIndex = 0; return true; } void AsyncTaskQueue_dispose(AsyncTaskQueue * self) { if (self->threads != NULL) { for (unsigned int threadIndex = 0; threadIndex < self->threadCount; threadIndex++) { self->threads[threadIndex].canceled = true; Shell_joinThread(self->threads[threadIndex].thread); } free(self->threads); } call_super_virtual(dispose, self); } static void threadWorkComplete(void * context) { struct AsyncTaskQueue_thread * thread = context; struct AsyncTaskQueue_task * task = &thread->tasks[thread->taskQueueCompletedTail]; if (task->completeCallback != NULL) { task->completeCallback(task->context1, task->context2); } thread->taskQueueCompletedTail = (thread->taskQueueCompletedTail + 1) % thread->taskCountMax; } static int workerThreadFunction(void * context) { struct AsyncTaskQueue_thread * thread = context; for (;;) { Shell_waitSemaphore(thread->semaphore); if (thread->canceled) { break; } struct AsyncTaskQueue_task * task = &thread->tasks[thread->taskQueueStartedTail]; task->workCallback(task->context1, task->context2); Shell_executeOnMainThread(threadWorkComplete, thread); thread->taskQueueStartedTail = (thread->taskQueueStartedTail + 1) % thread->taskCountMax; } return 0; } void AsyncTaskQueue_createWorkerThreads(AsyncTaskQueue * self) { if (self->threads == NULL) { self->threads = malloc(self->threadCount * sizeof(*self->threads)); for (unsigned int threadIndex = 0; threadIndex < self->threadCount; threadIndex++) { self->threads[threadIndex].semaphore = Shell_createSemaphore(0); self->threads[threadIndex].canceled = false; self->threads[threadIndex].taskCountMax = self->taskCountMaxPerThread; self->threads[threadIndex].taskQueueHead = 0; self->threads[threadIndex].taskQueueStartedTail = 0; self->threads[threadIndex].taskQueueCompletedTail = 0; self->threads[threadIndex].tasks = malloc(self->taskCountMaxPerThread * sizeof(*self->threads[threadIndex].tasks)); self->threads[threadIndex].thread = Shell_createThread(workerThreadFunction, &self->threads[threadIndex]); } } } static bool canThreadAcceptWork(struct AsyncTaskQueue_thread * thread) { return (thread->taskQueueHead + 1) % thread->taskCountMax != thread->taskQueueCompletedTail; } void AsyncTaskQueue_queueTask(AsyncTaskQueue * self, AsyncTaskQueue_callback workCallback, AsyncTaskQueue_callback completeCallback, void * context1, void * context2) { AsyncTaskQueue_createWorkerThreads(self); for (unsigned int threadOffset = 0; threadOffset < self->threadCount; threadOffset++) { unsigned int threadIndex = (self->nextThreadIndex + threadOffset) % self->threadCount; if (canThreadAcceptWork(&self->threads[threadIndex])) { struct AsyncTaskQueue_task task = {workCallback, completeCallback, context1, context2}; self->threads[threadIndex].tasks[self->threads[threadIndex].taskQueueHead] = task; self->threads[threadIndex].taskQueueHead++; Shell_postSemaphore(self->threads[threadIndex].semaphore); self->nextThreadIndex = (threadIndex + 1) % self->threadCount; return; } } #ifdef DEBUG fprintf(stderr, "Warning: All AsyncTaskQueue worker threads' queues are full; performing requested task immediately on main thread\n"); #endif workCallback(context1, context2); if (completeCallback != NULL) { completeCallback(context1, context2); } }