ui/common/threadpool.c

Sun, 15 Dec 2024 22:16:12 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 15 Dec 2024 22:16:12 +0100
branch
newapi
changeset 418
a57268d20ed9
parent 404
384f6d1f5784
permissions
-rw-r--r--

add menu separator (Motif)

280
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "threadpool.h"
288
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
30 #include "context.h"
280
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #ifndef _WIN32
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 static threadpool_job kill_job;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 UiThreadpool* threadpool_new(int min, int max) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 UiThreadpool *pool = malloc(sizeof(UiThreadpool));
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 pool->queue = NULL;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 pool->queue_len = 0;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 pool->num_idle = 0;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 pool->min_threads = min;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 pool->max_threads = max;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 pthread_mutex_init(&pool->queue_lock, NULL);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 pthread_mutex_init(&pool->avlbl_lock, NULL);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 pthread_cond_init(&pool->available, NULL);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 return pool;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 int threadpool_start(UiThreadpool *pool) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 /* create pool threads */
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 for(int i=0;i<pool->min_threads;i++) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 pthread_t t;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 if (pthread_create(&t, NULL, threadpool_func, pool) != 0) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 fprintf(stderr, "uic: threadpool_start: pthread_create failed: %s", strerror(errno));
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 return 1;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 return 0;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 void* threadpool_func(void *data) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 UiThreadpool *pool = (UiThreadpool*)data;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 for(;;) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 threadpool_job *job = threadpool_get_job(pool);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 if(job == &kill_job) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 break;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
288
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
76
280
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 job->callback(job->data);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 free(job);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 return NULL;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 threadpool_job* threadpool_get_job(UiThreadpool *pool) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 pthread_mutex_lock(&pool->queue_lock);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 threadpool_job *job = NULL;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 pool->num_idle++;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 while(job == NULL) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 if(pool->queue_len == 0) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 pthread_cond_wait(&pool->available, &pool->queue_lock);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 continue;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 } else {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 pool_queue_t *q = pool->queue;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 job = q->job;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 pool->queue = q->next;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 pool->queue_len--;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 free(q);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 pool->num_idle--;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 pthread_mutex_unlock(&pool->queue_lock);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 return job;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 void threadpool_run(UiThreadpool *pool, job_callback_f func, void *data) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 // TODO: handle errors
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 threadpool_job *job = malloc(sizeof(threadpool_job));
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 job->callback = func;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 job->data = data;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 pthread_mutex_lock(&pool->queue_lock);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 threadpool_enqueue_job(pool, job);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 int create_thread = 0;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 int destroy_thread = 0;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 int diff = pool->queue_len - pool->num_idle;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 //if(pool->queue_len == 1) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 pthread_cond_signal(&pool->available);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 //}
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 pthread_mutex_unlock(&pool->queue_lock);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 void threadpool_enqueue_job(UiThreadpool *pool, threadpool_job *job) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 pool_queue_t *q = malloc(sizeof(pool_queue_t));
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 q->job = job;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 q->next = NULL;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 if(pool->queue == NULL) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 pool->queue = q;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 } else {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 pool_queue_t *last_elem = pool->queue;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 while(last_elem->next != NULL) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 last_elem = last_elem->next;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 last_elem->next = q;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 pool->queue_len++;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 UiThreadpool* ui_threadpool_create(int nthreads) {
288
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
151 UiThreadpool *pool = threadpool_new(nthreads, nthreads);
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
152 threadpool_start(pool); // TODO: check return value
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
153 return pool;
280
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 void ui_threadpool_destroy(UiThreadpool* pool) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159
288
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
160 static int ui_threadpool_job_finish(void *data) {
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
161 UiJob *job = data;
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
162 UiEvent event;
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
163 event.obj = job->obj;
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
164 event.window = job->obj->window;
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
165 event.document = job->obj->ctx->document;
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
166 event.intval = 0;
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
167 event.eventdata = NULL;
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
168 job->finish_callback(&event, job->finish_data);
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
169 free(job);
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
170 return 0;
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
171 }
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
172
280
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 static void* ui_threadpool_job_func(void *data) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 UiJob *job = data;
288
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
175 if (!job->job_func(job->job_data) && job->finish_callback) {
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
176 ui_call_mainthread(ui_threadpool_job_finish, job);
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
177 } else {
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
178 free(job);
c5e89affb2ea fix threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
179 }
280
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 return NULL;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 void ui_threadpool_job(UiThreadpool* pool, UiObject* obj, ui_threadfunc tf, void* td, ui_callback f, void* fd) {
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 UiJob* job = malloc(sizeof(UiJob));
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 job->obj = obj;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 job->job_func = tf;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 job->job_data = td;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 job->finish_callback = f;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 job->finish_data = fd;
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 threadpool_run(pool, ui_threadpool_job_func, job);
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 }
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 #endif
e3565cf7c831 add threadpool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195

mercurial