ui/common/threadpool.c

Sun, 16 Jun 2024 11:36:27 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 16 Jun 2024 11:36:27 +0200
changeset 42
9af327d0e0e4
parent 35
834d9c15a69f
permissions
-rw-r--r--

update toolkit

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

mercurial