/* 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 */ #ifndef __AsyncTaskQueue_H__ #define __AsyncTaskQueue_H__ typedef struct AsyncTaskQueue AsyncTaskQueue; #define AsyncTaskQueue_superclass StemObject #include "stemobject/StemObject.h" typedef void (* AsyncTaskQueue_callback)(void * context1, void * context2); #define AsyncTaskQueue_ivars \ StemObject_ivars \ \ unsigned int threadCount; \ unsigned int taskCountMaxPerThread; \ struct AsyncTaskQueue_thread * threads; \ unsigned int nextThreadIndex; #define AsyncTaskQueue_vtable(self_type) \ StemObject_vtable(self_type) stemobject_declare(AsyncTaskQueue) AsyncTaskQueue * AsyncTaskQueue_create(unsigned int threadCount, unsigned int taskCountMaxPerThread); bool AsyncTaskQueue_init(AsyncTaskQueue * self, unsigned int threadCount, unsigned int taskCountMaxPerThread); void AsyncTaskQueue_dispose(AsyncTaskQueue * self); // Worker threads will be created automatically when the first task is queued. This function can be called to create them // ahead of time if desired. void AsyncTaskQueue_createWorkerThreads(AsyncTaskQueue * self); // workCallback must be safely callable from a secondary thread; completeCallback will always be called on the main thread // AsyncTaskQueue_queueTask() itself must only be called from the main thread void AsyncTaskQueue_queueTask(AsyncTaskQueue * self, AsyncTaskQueue_callback workCallback, AsyncTaskQueue_callback completeCallback, void * context1, void * context2); #endif