UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2024 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef UIC_THREADPOOL_H 30 #define UIC_THREADPOOL_H 31 32 #include "../ui/toolkit.h" 33 34 #ifndef _WIN32 35 #include <pthread.h> 36 #endif 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 typedef struct UiQueueElm UiQueueElm; 43 typedef struct UiQueue UiQueue; 44 45 typedef struct UiJob { 46 UiObject *obj; 47 ui_threadfunc job_func; 48 void *job_data; 49 ui_callback finish_callback; 50 void *finish_data; 51 } UiJob; 52 53 struct UiQueueElm { 54 void *data; 55 UiQueueElm *next; 56 }; 57 58 struct UiQueue { 59 UiQueueElm *elements; 60 size_t length; 61 pthread_mutex_t lock; 62 pthread_mutex_t avlbl_lock; 63 pthread_cond_t available; 64 }; 65 66 typedef struct _threadpool_job threadpool_job; 67 typedef void*(*job_callback_f)(void *data); 68 69 typedef struct _pool_queue pool_queue_t; 70 struct UiThreadpool { 71 UiQueue *queue; 72 uint32_t num_idle; 73 int min_threads; 74 int max_threads; 75 pthread_t *threads; 76 int nthreads; 77 }; 78 79 struct _threadpool_job { 80 job_callback_f callback; 81 void *data; 82 }; 83 84 struct _pool_queue { 85 threadpool_job *job; 86 pool_queue_t *next; 87 }; 88 89 UiThreadpool* threadpool_new(int min, int max); 90 int threadpool_start(UiThreadpool *pool); 91 int threadpool_join(UiThreadpool *pool); 92 void* threadpool_func(void *data); 93 threadpool_job* threadpool_get_job(UiThreadpool *pool); 94 void threadpool_run(UiThreadpool *pool, job_callback_f func, void *data); 95 96 UiQueue* ui_queue_create(void); 97 void ui_queue_free(UiQueue *queue); 98 void ui_queue_put(UiQueue *queue, void *data); 99 void* ui_queue_get_wait(UiQueue *queue); 100 101 #ifdef __cplusplus 102 } 103 #endif 104 105 #endif /* UIC_THREADPOOL_H */ 106 107