Tue, 29 Oct 2024 19:07:11 +0100
implement repo Generate Key button
32 | 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 | #include "threadpool.h" | |
35
834d9c15a69f
some toolkit fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
30 | #include "context.h" |
32 | 31 | |
32 | #include <pthread.h> | |
33 | ||
34 | #ifndef _WIN32 | |
35 | ||
36 | ||
37 | static threadpool_job kill_job; | |
38 | ||
39 | UiThreadpool* threadpool_new(int min, int max) { | |
40 | UiThreadpool *pool = malloc(sizeof(UiThreadpool)); | |
41 | pool->queue = NULL; | |
42 | pool->queue_len = 0; | |
43 | pool->num_idle = 0; | |
44 | pool->min_threads = min; | |
45 | pool->max_threads = max; | |
46 | ||
47 | pthread_mutex_init(&pool->queue_lock, NULL); | |
48 | pthread_mutex_init(&pool->avlbl_lock, NULL); | |
49 | pthread_cond_init(&pool->available, NULL); | |
50 | ||
51 | return pool; | |
52 | } | |
53 | ||
54 | int threadpool_start(UiThreadpool *pool) { | |
55 | /* create pool threads */ | |
56 | for(int i=0;i<pool->min_threads;i++) { | |
57 | pthread_t t; | |
58 | if (pthread_create(&t, NULL, threadpool_func, pool) != 0) { | |
59 | fprintf(stderr, "uic: threadpool_start: pthread_create failed: %s", strerror(errno)); | |
60 | return 1; | |
61 | } | |
62 | } | |
63 | return 0; | |
64 | } | |
65 | ||
66 | void* threadpool_func(void *data) { | |
67 | UiThreadpool *pool = (UiThreadpool*)data; | |
68 | ||
69 | for(;;) { | |
70 | threadpool_job *job = threadpool_get_job(pool); | |
71 | if(job == &kill_job) { | |
72 | break; | |
73 | } | |
35
834d9c15a69f
some toolkit fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
74 | |
32 | 75 | job->callback(job->data); |
76 | ||
77 | free(job); | |
78 | } | |
79 | return NULL; | |
80 | } | |
81 | ||
82 | threadpool_job* threadpool_get_job(UiThreadpool *pool) { | |
83 | pthread_mutex_lock(&pool->queue_lock); | |
84 | ||
85 | threadpool_job *job = NULL; | |
86 | pool->num_idle++; | |
87 | while(job == NULL) { | |
88 | if(pool->queue_len == 0) { | |
89 | pthread_cond_wait(&pool->available, &pool->queue_lock); | |
90 | continue; | |
91 | } else { | |
92 | pool_queue_t *q = pool->queue; | |
93 | job = q->job; | |
94 | pool->queue = q->next; | |
95 | pool->queue_len--; | |
96 | free(q); | |
97 | } | |
98 | } | |
99 | pool->num_idle--; | |
100 | ||
101 | pthread_mutex_unlock(&pool->queue_lock); | |
102 | return job; | |
103 | } | |
104 | ||
105 | void threadpool_run(UiThreadpool *pool, job_callback_f func, void *data) { | |
106 | // TODO: handle errors | |
107 | ||
108 | threadpool_job *job = malloc(sizeof(threadpool_job)); | |
109 | job->callback = func; | |
110 | job->data = data; | |
111 | ||
112 | pthread_mutex_lock(&pool->queue_lock); | |
113 | threadpool_enqueue_job(pool, job); | |
114 | ||
115 | int create_thread = 0; | |
116 | int destroy_thread = 0; | |
117 | int diff = pool->queue_len - pool->num_idle; | |
118 | ||
119 | //if(pool->queue_len == 1) { | |
120 | pthread_cond_signal(&pool->available); | |
121 | //} | |
122 | ||
123 | pthread_mutex_unlock(&pool->queue_lock); | |
124 | } | |
125 | ||
126 | void threadpool_enqueue_job(UiThreadpool *pool, threadpool_job *job) { | |
127 | pool_queue_t *q = malloc(sizeof(pool_queue_t)); | |
128 | q->job = job; | |
129 | q->next = NULL; | |
130 | ||
131 | if(pool->queue == NULL) { | |
132 | pool->queue = q; | |
133 | } else { | |
134 | pool_queue_t *last_elem = pool->queue; | |
135 | while(last_elem->next != NULL) { | |
136 | last_elem = last_elem->next; | |
137 | } | |
138 | last_elem->next = q; | |
139 | } | |
140 | pool->queue_len++; | |
141 | } | |
142 | ||
143 | ||
144 | ||
145 | ||
146 | ||
147 | ||
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 | 152 | } |
153 | ||
154 | void ui_threadpool_destroy(UiThreadpool* pool) { | |
155 | ||
156 | } | |
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 | 171 | static void* ui_threadpool_job_func(void *data) { |
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 | 178 | return NULL; |
179 | } | |
180 | ||
181 | void ui_threadpool_job(UiThreadpool* pool, UiObject* obj, ui_threadfunc tf, void* td, ui_callback f, void* fd) { | |
182 | UiJob* job = malloc(sizeof(UiJob)); | |
183 | job->obj = obj; | |
184 | job->job_func = tf; | |
185 | job->job_data = td; | |
186 | job->finish_callback = f; | |
187 | job->finish_data = fd; | |
188 | threadpool_run(pool, ui_threadpool_job_func, job); | |
189 | } | |
190 | ||
191 | ||
192 | #endif | |
193 |