Sun, 07 Dec 2025 15:50:20 +0100
rename tree.h to list.h
| 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)); | |
|
950
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
43 | pool->queue = ui_queue_create(); |
| 280 | 44 | pool->num_idle = 0; |
| 45 | pool->min_threads = min; | |
| 46 | pool->max_threads = max; | |
| 47 | ||
| 48 | return pool; | |
| 49 | } | |
| 50 | ||
| 51 | int threadpool_start(UiThreadpool *pool) { | |
|
949
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
52 | pool->nthreads = pool->min_threads; |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
53 | pool->threads = calloc(pool->max_threads, sizeof(pthread_t)); |
| 280 | 54 | /* create pool threads */ |
|
949
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
55 | for(int i=0;i<pool->nthreads;i++) { |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
56 | if (pthread_create(&pool->threads[i], NULL, threadpool_func, pool) != 0) { |
| 280 | 57 | fprintf(stderr, "uic: threadpool_start: pthread_create failed: %s", strerror(errno)); |
| 58 | return 1; | |
| 59 | } | |
| 60 | } | |
| 61 | return 0; | |
| 62 | } | |
| 63 | ||
|
949
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
64 | int threadpool_join(UiThreadpool *pool) { |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
65 | int err = 0; |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
66 | for(int i=0;i<pool->nthreads;i++) { |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
67 | if(pthread_join(pool->threads[i], NULL)) { |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
68 | err = 1; |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
69 | } |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
70 | } |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
71 | return err; |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
72 | } |
|
ef8f13c8c08f
add threadpool_join
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
659
diff
changeset
|
73 | |
| 280 | 74 | void* threadpool_func(void *data) { |
| 75 | UiThreadpool *pool = (UiThreadpool*)data; | |
| 76 | ||
| 77 | for(;;) { | |
| 78 | threadpool_job *job = threadpool_get_job(pool); | |
| 79 | if(job == &kill_job) { | |
| 80 | break; | |
| 81 | } | |
| 288 | 82 | |
| 280 | 83 | job->callback(job->data); |
| 84 | ||
| 85 | free(job); | |
| 86 | } | |
| 87 | return NULL; | |
| 88 | } | |
| 89 | ||
| 90 | threadpool_job* threadpool_get_job(UiThreadpool *pool) { | |
|
950
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
91 | threadpool_job *job = ui_queue_get_wait(pool->queue); |
| 280 | 92 | return job; |
| 93 | } | |
| 94 | ||
|
950
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
95 | void threadpool_run(UiThreadpool *pool, job_callback_f func, void *data) { |
| 280 | 96 | threadpool_job *job = malloc(sizeof(threadpool_job)); |
| 97 | job->callback = func; | |
| 98 | job->data = data; | |
|
950
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
99 | ui_queue_put(pool->queue, job); |
| 280 | 100 | } |
| 101 | ||
| 102 | ||
| 103 | ||
| 104 | UiThreadpool* ui_threadpool_create(int nthreads) { | |
| 288 | 105 | UiThreadpool *pool = threadpool_new(nthreads, nthreads); |
| 106 | threadpool_start(pool); // TODO: check return value | |
| 107 | return pool; | |
| 280 | 108 | } |
| 109 | ||
| 110 | void ui_threadpool_destroy(UiThreadpool* pool) { | |
| 111 | ||
| 112 | } | |
| 113 | ||
| 288 | 114 | static int ui_threadpool_job_finish(void *data) { |
| 115 | UiJob *job = data; | |
| 116 | UiEvent event; | |
| 117 | event.obj = job->obj; | |
| 118 | event.window = job->obj->window; | |
| 119 | event.document = job->obj->ctx->document; | |
| 120 | event.intval = 0; | |
| 121 | event.eventdata = NULL; | |
|
659
d6baaa93f7be
add UiEvent eventdatatype
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
510
diff
changeset
|
122 | event.eventdatatype = 0; |
| 288 | 123 | job->finish_callback(&event, job->finish_data); |
| 124 | free(job); | |
| 125 | return 0; | |
| 126 | } | |
| 127 | ||
| 280 | 128 | static void* ui_threadpool_job_func(void *data) { |
| 129 | UiJob *job = data; | |
| 288 | 130 | if (!job->job_func(job->job_data) && job->finish_callback) { |
| 131 | ui_call_mainthread(ui_threadpool_job_finish, job); | |
| 132 | } else { | |
| 133 | free(job); | |
| 134 | } | |
| 280 | 135 | return NULL; |
| 136 | } | |
| 137 | ||
| 138 | void ui_threadpool_job(UiThreadpool* pool, UiObject* obj, ui_threadfunc tf, void* td, ui_callback f, void* fd) { | |
| 139 | UiJob* job = malloc(sizeof(UiJob)); | |
| 140 | job->obj = obj; | |
| 141 | job->job_func = tf; | |
| 142 | job->job_data = td; | |
| 143 | job->finish_callback = f; | |
| 144 | job->finish_data = fd; | |
| 145 | threadpool_run(pool, ui_threadpool_job_func, job); | |
| 146 | } | |
| 147 | ||
|
950
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
148 | /* --------------------------------- Queue --------------------------------- */ |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
149 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
150 | UiQueue* ui_queue_create(void) { |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
151 | UiQueue *queue = calloc(1, sizeof(UiQueue)); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
152 | pthread_mutex_init(&queue->lock, NULL); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
153 | pthread_mutex_init(&queue->avlbl_lock, NULL); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
154 | pthread_cond_init(&queue->available, NULL); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
155 | return queue; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
156 | } |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
157 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
158 | void ui_queue_free(UiQueue *queue) { |
|
955
ea9a999b4fc8
implement ui_queue_free
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
950
diff
changeset
|
159 | // The queue must be empty, we could free UiQueueElm, |
|
ea9a999b4fc8
implement ui_queue_free
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
950
diff
changeset
|
160 | // but not the payload data |
|
ea9a999b4fc8
implement ui_queue_free
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
950
diff
changeset
|
161 | pthread_mutex_destroy(&queue->lock); |
|
ea9a999b4fc8
implement ui_queue_free
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
950
diff
changeset
|
162 | pthread_mutex_destroy(&queue->avlbl_lock); |
|
ea9a999b4fc8
implement ui_queue_free
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
950
diff
changeset
|
163 | pthread_cond_destroy(&queue->available); |
|
ea9a999b4fc8
implement ui_queue_free
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
950
diff
changeset
|
164 | free(queue); |
|
950
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
165 | } |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
166 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
167 | void ui_queue_put(UiQueue *queue, void *data) { |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
168 | // create queue element |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
169 | UiQueueElm *elm = malloc(sizeof(UiQueueElm)); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
170 | elm->data = data; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
171 | elm->next = NULL; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
172 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
173 | pthread_mutex_lock(&queue->lock); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
174 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
175 | // put queue element at the end of the linked list |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
176 | if(queue->elements) { |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
177 | UiQueueElm *end = queue->elements; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
178 | while(end->next) { |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
179 | end = end->next; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
180 | } |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
181 | end->next = elm; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
182 | } else { |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
183 | queue->elements = elm; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
184 | } |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
185 | queue->length++; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
186 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
187 | // signal new available data |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
188 | pthread_cond_signal(&queue->available); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
189 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
190 | pthread_mutex_unlock(&queue->lock); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
191 | } |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
192 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
193 | void* ui_queue_get_wait(UiQueue *queue) { |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
194 | pthread_mutex_lock(&queue->lock); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
195 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
196 | void *data = NULL; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
197 | while(data == NULL) { |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
198 | if(queue->length == 0) { |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
199 | pthread_cond_wait(&queue->available, &queue->lock); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
200 | continue; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
201 | } else { |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
202 | UiQueueElm *q = queue->elements; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
203 | data = q->data; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
204 | queue->elements = q->next; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
205 | queue->length--; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
206 | free(q); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
207 | } |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
208 | } |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
209 | |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
210 | pthread_mutex_unlock(&queue->lock); |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
211 | return data; |
|
39641cf150eb
add main event loop (Server)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
949
diff
changeset
|
212 | } |
| 280 | 213 | |
| 214 | #endif | |
| 215 |