Sat, 06 Dec 2025 14:12:11 +0100
add threadpool_join
| 280 | 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 | ||
|
510
9f562a7de4a2
prepare buildsystem for win32
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
404
diff
changeset
|
29 | #ifndef _WIN32 |
|
9f562a7de4a2
prepare buildsystem for win32
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
404
diff
changeset
|
30 | |
| 280 | 31 | #include "threadpool.h" |
| 288 | 32 | #include "context.h" |
| 280 | 33 | |
|
404
384f6d1f5784
add first working cocoa code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
288
diff
changeset
|
34 | #include <pthread.h> |
|
384f6d1f5784
add first working cocoa code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
288
diff
changeset
|
35 | #include <stdio.h> |
|
384f6d1f5784
add first working cocoa code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
288
diff
changeset
|
36 | #include <string.h> |
|
384f6d1f5784
add first working cocoa code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
288
diff
changeset
|
37 | #include <errno.h> |
| 280 | 38 | |
| 39 | static threadpool_job kill_job; | |
| 40 | ||
| 41 | UiThreadpool* threadpool_new(int min, int max) { | |
| 42 | UiThreadpool *pool = malloc(sizeof(UiThreadpool)); | |
| 43 | pool->queue = NULL; | |
| 44 | pool->queue_len = 0; | |
| 45 | pool->num_idle = 0; | |
| 46 | pool->min_threads = min; | |
| 47 | pool->max_threads = max; | |
| 48 | ||
| 49 | pthread_mutex_init(&pool->queue_lock, NULL); | |
| 50 | pthread_mutex_init(&pool->avlbl_lock, NULL); | |
| 51 | pthread_cond_init(&pool->available, NULL); | |
| 52 | ||
| 53 | return pool; | |
| 54 | } | |
| 55 | ||
| 56 | int threadpool_start(UiThreadpool *pool) { | |
|
949
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
57 | pool->nthreads = pool->min_threads; |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
58 | pool->threads = calloc(pool->max_threads, sizeof(pthread_t)); |
| 280 | 59 | /* create pool threads */ |
|
949
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
60 | for(int i=0;i<pool->nthreads;i++) { |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
61 | if (pthread_create(&pool->threads[i], NULL, threadpool_func, pool) != 0) { |
| 280 | 62 | fprintf(stderr, "uic: threadpool_start: pthread_create failed: %s", strerror(errno)); |
| 63 | return 1; | |
| 64 | } | |
| 65 | } | |
| 66 | return 0; | |
| 67 | } | |
| 68 | ||
|
949
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
69 | int threadpool_join(UiThreadpool *pool) { |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
70 | int err = 0; |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
71 | for(int i=0;i<pool->nthreads;i++) { |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
72 | if(pthread_join(pool->threads[i], NULL)) { |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
73 | err = 1; |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
74 | } |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
75 | } |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
76 | return err; |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
77 | } |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
78 | |
| 280 | 79 | void* threadpool_func(void *data) { |
| 80 | UiThreadpool *pool = (UiThreadpool*)data; | |
| 81 | ||
| 82 | for(;;) { | |
| 83 | threadpool_job *job = threadpool_get_job(pool); | |
| 84 | if(job == &kill_job) { | |
| 85 | break; | |
| 86 | } | |
| 288 | 87 | |
| 280 | 88 | job->callback(job->data); |
| 89 | ||
| 90 | free(job); | |
| 91 | } | |
| 92 | return NULL; | |
| 93 | } | |
| 94 | ||
| 95 | threadpool_job* threadpool_get_job(UiThreadpool *pool) { | |
| 96 | pthread_mutex_lock(&pool->queue_lock); | |
| 97 | ||
| 98 | threadpool_job *job = NULL; | |
| 99 | pool->num_idle++; | |
| 100 | while(job == NULL) { | |
| 101 | if(pool->queue_len == 0) { | |
| 102 | pthread_cond_wait(&pool->available, &pool->queue_lock); | |
| 103 | continue; | |
| 104 | } else { | |
| 105 | pool_queue_t *q = pool->queue; | |
| 106 | job = q->job; | |
| 107 | pool->queue = q->next; | |
| 108 | pool->queue_len--; | |
| 109 | free(q); | |
| 110 | } | |
| 111 | } | |
| 112 | pool->num_idle--; | |
| 113 | ||
| 114 | pthread_mutex_unlock(&pool->queue_lock); | |
| 115 | return job; | |
| 116 | } | |
| 117 | ||
| 118 | void threadpool_run(UiThreadpool *pool, job_callback_f func, void *data) { | |
| 119 | // TODO: handle errors | |
| 120 | ||
| 121 | threadpool_job *job = malloc(sizeof(threadpool_job)); | |
| 122 | job->callback = func; | |
| 123 | job->data = data; | |
| 124 | ||
| 125 | pthread_mutex_lock(&pool->queue_lock); | |
| 126 | threadpool_enqueue_job(pool, job); | |
| 127 | ||
| 128 | int create_thread = 0; | |
| 129 | int destroy_thread = 0; | |
| 130 | int diff = pool->queue_len - pool->num_idle; | |
| 131 | ||
| 132 | //if(pool->queue_len == 1) { | |
| 133 | pthread_cond_signal(&pool->available); | |
| 134 | //} | |
| 135 | ||
| 136 | pthread_mutex_unlock(&pool->queue_lock); | |
| 137 | } | |
| 138 | ||
| 139 | void threadpool_enqueue_job(UiThreadpool *pool, threadpool_job *job) { | |
| 140 | pool_queue_t *q = malloc(sizeof(pool_queue_t)); | |
| 141 | q->job = job; | |
| 142 | q->next = NULL; | |
| 143 | ||
| 144 | if(pool->queue == NULL) { | |
| 145 | pool->queue = q; | |
| 146 | } else { | |
| 147 | pool_queue_t *last_elem = pool->queue; | |
| 148 | while(last_elem->next != NULL) { | |
| 149 | last_elem = last_elem->next; | |
| 150 | } | |
| 151 | last_elem->next = q; | |
| 152 | } | |
| 153 | pool->queue_len++; | |
| 154 | } | |
| 155 | ||
| 156 | ||
| 157 | ||
| 158 | ||
| 159 | ||
| 160 | ||
| 161 | UiThreadpool* ui_threadpool_create(int nthreads) { | |
| 288 | 162 | UiThreadpool *pool = threadpool_new(nthreads, nthreads); |
| 163 | threadpool_start(pool); // TODO: check return value | |
| 164 | return pool; | |
| 280 | 165 | } |
| 166 | ||
| 167 | void ui_threadpool_destroy(UiThreadpool* pool) { | |
| 168 | ||
| 169 | } | |
| 170 | ||
| 288 | 171 | static int ui_threadpool_job_finish(void *data) { |
| 172 | UiJob *job = data; | |
| 173 | UiEvent event; | |
| 174 | event.obj = job->obj; | |
| 175 | event.window = job->obj->window; | |
| 176 | event.document = job->obj->ctx->document; | |
| 177 | event.intval = 0; | |
| 178 | event.eventdata = NULL; | |
|
659
d6baaa93f7be
add UiEvent eventdatatype
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
510
diff
changeset
|
179 | event.eventdatatype = 0; |
| 288 | 180 | job->finish_callback(&event, job->finish_data); |
| 181 | free(job); | |
| 182 | return 0; | |
| 183 | } | |
| 184 | ||
| 280 | 185 | static void* ui_threadpool_job_func(void *data) { |
| 186 | UiJob *job = data; | |
| 288 | 187 | if (!job->job_func(job->job_data) && job->finish_callback) { |
| 188 | ui_call_mainthread(ui_threadpool_job_finish, job); | |
| 189 | } else { | |
| 190 | free(job); | |
| 191 | } | |
| 280 | 192 | return NULL; |
| 193 | } | |
| 194 | ||
| 195 | void ui_threadpool_job(UiThreadpool* pool, UiObject* obj, ui_threadfunc tf, void* td, ui_callback f, void* fd) { | |
| 196 | UiJob* job = malloc(sizeof(UiJob)); | |
| 197 | job->obj = obj; | |
| 198 | job->job_func = tf; | |
| 199 | job->job_data = td; | |
| 200 | job->finish_callback = f; | |
| 201 | job->finish_data = fd; | |
| 202 | threadpool_run(pool, ui_threadpool_job_func, job); | |
| 203 | } | |
| 204 | ||
| 205 | ||
| 206 | #endif | |
| 207 |