source/myhtml/thread.c

filter: Statementstmt 42.6% 229 / 537 Branchbran 25.0% 66 / 264 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 36.8% 295 / 801 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« source/myhtml/utils/mchar_async.c ↑ Summary source/myhtml/utils/mcobject_async.c »
1 /*
2 Copyright 2015-2016 Alexander Borisov
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Author: lex.borisov@gmail.com (Alexander Borisov)
17 */
18  
19 #include "myhtml/thread.h"
20  
21 #ifndef MyHTML_BUILD_WITHOUT_THREADS
22  
23 #if defined(IS_OS_WINDOWS)
24 /***********************************************************************************
25 *
26 * For Windows
27 *
28 ***********************************************************************************/
29 myhtml_status_t myhtml_thread_create(mythread_t *mythread, mythread_list_t *thr, void *work_func)
30 {
31 thr->pth = CreateThread(NULL, // default security attributes
32 0, // use default stack size
33 work_func, // thread function name
34 &thr->data, // argument to thread function
35 0, // use default creation flags
36 NULL); // returns the thread identifier
37  
38
39 return MyHTML_STATUS_OK;
40 }
41  
42 myhtml_status_t myhtml_thread_join(mythread_t *mythread, mythread_list_t *thr)
43 {
44 WaitForSingleObject(thr->pth, INFINITE);
45
46 return MyHTML_STATUS_OK;
47 }
48  
49 myhtml_status_t myhtml_thread_cancel(mythread_t *mythread, mythread_list_t *thr)
50 {
51 TerminateThread(thr->pth, 0);
52
53 return MyHTML_STATUS_OK;
54 }
55  
56 myhtml_status_t myhtml_thread_attr_init(mythread_t *mythread)
57 {
58 return MyHTML_STATUS_OK;
59 }
60  
61 myhtml_status_t myhtml_thread_attr_clean(mythread_t *mythread)
62 {
63 return MyHTML_STATUS_OK;
64 }
65  
66 myhtml_status_t myhtml_thread_attr_destroy(mythread_t *mythread)
67 {
68 return MyHTML_STATUS_OK;
69 }
70  
71 myhtml_status_t myhtml_hread_mutex_create(mythread_t *mythread, mythread_context_t *ctx, size_t prefix_id)
72 {
73 ctx->mutex = CreateSemaphore(NULL, 0, 1, NULL);
74
75 return MyHTML_STATUS_OK;
76 }
77  
78 myhtml_status_t myhtml_hread_mutex_post(mythread_t *mythread, mythread_context_t *ctx)
79 {
80 ReleaseSemaphore(ctx->mutex, 1, NULL);
81
82 return MyHTML_STATUS_OK;
83 }
84  
85 myhtml_status_t myhtml_hread_mutex_wait(mythread_t *mythread, mythread_context_t *ctx)
86 {
87 WaitForSingleObject(ctx->mutex, INFINITE);
88
89 return MyHTML_STATUS_OK;
90 }
91  
92 myhtml_status_t myhtml_hread_mutex_try_wait(mythread_t *mythread, mythread_context_t *ctx)
93 {
94 return MyHTML_STATUS_OK;
95 }
96  
97 myhtml_status_t myhtml_hread_mutex_close(mythread_t *mythread, mythread_context_t *ctx)
98 {
99 CloseHandle(ctx->mutex);
100
101 return MyHTML_STATUS_OK;
102 }
103  
104 void myhtml_thread_nanosleep(const struct timespec *tomeout)
105 {
106 Sleep(0);
107 }
108  
109 #else /* defined(IS_OS_WINDOWS) */
110 /***********************************************************************************
111 *
112 * For all unix system. POSIX pthread
113 *
114 ***********************************************************************************/
115  
116 3 myhtml_status_t myhtml_thread_create(mythread_t *mythread, mythread_list_t *thr, void *work_func)
117 {
118 3 pthread_create(&thr->pth, mythread->attr,
119 work_func,
120 3 (void*)(&thr->data));
121
122 3 return MyHTML_STATUS_OK;
123 }
124  
125 3 myhtml_status_t myhtml_thread_join(mythread_t *mythread, mythread_list_t *thr)
126 {
127 3 pthread_join(thr->pth, NULL);
128 3 return MyHTML_STATUS_OK;
129 }
130  
131 0 myhtml_status_t myhtml_thread_cancel(mythread_t *mythread, mythread_list_t *thr)
132 {
133 0 pthread_cancel(thr->pth);
134 0 return MyHTML_STATUS_OK;
135 }
136  
137 1 myhtml_status_t myhtml_thread_attr_init(mythread_t *mythread)
138 {
139 1 mythread->attr = (pthread_attr_t*)calloc(1, sizeof(pthread_attr_t));
140
141 1 ▶ if(mythread->attr == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->attr == NULL) 0 1 1
142 0 return MyHTML_STATUS_THREAD_ERROR_ATTR_MALLOC;
143
144 1 mythread->sys_last_error = pthread_attr_init(mythread->attr);
145 1 ▶ if(mythread->sys_last_error)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->sys_last_error) 0 1 1
146 0 return MyHTML_STATUS_THREAD_ERROR_ATTR_INIT;
147
148 1 mythread->sys_last_error = pthread_attr_setdetachstate(mythread->attr, PTHREAD_CREATE_JOINABLE);
149 1 ▶ if(mythread->sys_last_error)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->sys_last_error) 0 1 1
150 0 return MyHTML_STATUS_THREAD_ERROR_ATTR_SET;
151
152 1 return MyHTML_STATUS_OK;
153 }
154  
155 0 myhtml_status_t myhtml_thread_attr_clean(mythread_t *mythread)
156 {
157 0 mythread->attr = NULL;
158 0 return MyHTML_STATUS_OK;
159 }
160  
161 1 myhtml_status_t myhtml_thread_attr_destroy(mythread_t *mythread)
162 {
163 1 ▶ if(mythread->attr) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->attr) { 1 0 1
164 1 mythread->sys_last_error = pthread_attr_destroy(mythread->attr);
165
166 1 free(mythread->attr);
167 1 mythread->attr = NULL;
168
169 1 ▶ if(mythread->sys_last_error)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->sys_last_error) 0 1 1
170 0 return MyHTML_STATUS_THREAD_ERROR_ATTR_DESTROY;
171 }
172
173 1 return MyHTML_STATUS_OK;
174 }
175  
176 3 myhtml_status_t myhtml_hread_mutex_create(mythread_t *mythread, mythread_context_t *ctx, size_t prefix_id)
177 {
178 3 ctx->mutex = (pthread_mutex_t*)calloc(1, sizeof(pthread_mutex_t));
179
180 3 ▶ if(ctx->mutex == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->mutex == NULL) 0 3 3
181 0 return MyHTML_STATUS_THREAD_ERROR_MUTEX_MALLOC;
182
183 3 ▶ if(pthread_mutex_init(ctx->mutex, NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if(pthread_mutex_init(ctx->mutex, NULL)) { 0 3 3
184 0 mythread->sys_last_error = errno;
185 0 return MyHTML_STATUS_THREAD_ERROR_MUTEX_INIT;
186 }
187
188 3 ▶ if(pthread_mutex_lock(ctx->mutex)) {
Branch: 1 branch
BranchTrueFalseTotal
if(pthread_mutex_lock(ctx->mutex)) { 0 3 3
189 0 mythread->sys_last_error = errno;
190 0 return MyHTML_STATUS_THREAD_ERROR_MUTEX_LOCK;
191 }
192
193 3 return MyHTML_STATUS_OK;
194 }
195  
196 3 myhtml_status_t myhtml_hread_mutex_post(mythread_t *mythread, mythread_context_t *ctx)
197 {
198 3 ▶ if(pthread_mutex_unlock(ctx->mutex)) {
Branch: 1 branch
BranchTrueFalseTotal
if(pthread_mutex_unlock(ctx->mutex)) { 0 3 3
199 0 mythread->sys_last_error = errno;
200 0 return MyHTML_STATUS_THREAD_ERROR_MUTEX_UNLOCK;
201 }
202
203 3 return MyHTML_STATUS_OK;
204 }
205  
206 3 myhtml_status_t myhtml_hread_mutex_wait(mythread_t *mythread, mythread_context_t *ctx)
207 {
208 3 ▶ if(pthread_mutex_lock(ctx->mutex)) {
Branch: 1 branch
BranchTrueFalseTotal
if(pthread_mutex_lock(ctx->mutex)) { 0 3 3
209 0 mythread->sys_last_error = errno;
210 0 return MyHTML_STATUS_THREAD_ERROR_MUTEX_LOCK;
211 }
212
213 3 return MyHTML_STATUS_OK;
214 }
215  
216 0 myhtml_status_t myhtml_hread_mutex_try_wait(mythread_t *mythread, mythread_context_t *ctx)
217 {
218 0 ▶ if(pthread_mutex_trylock(ctx->mutex)) {
Branch: 1 branch
BranchTrueFalseTotal
if(pthread_mutex_trylock(ctx->mutex)) { 0 0 0
219 0 mythread->sys_last_error = errno;
220 0 return MyHTML_STATUS_THREAD_ERROR_MUTEX_LOCK;
221 }
222
223 0 return MyHTML_STATUS_OK;
224 }
225  
226 3 myhtml_status_t myhtml_hread_mutex_close(mythread_t *mythread, mythread_context_t *ctx)
227 {
228 3 ▶ if(ctx->mutex) {
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->mutex) { 3 0 3
229 3 pthread_mutex_destroy(ctx->mutex);
230 3 free(ctx->mutex);
231
232 3 ctx->mutex = NULL;
233 }
234
235 3 return MyHTML_STATUS_OK;
236 }
237  
238 0 void myhtml_thread_nanosleep(const struct timespec *tomeout)
239 {
240 0 nanosleep(tomeout, NULL);
241 0 }
242  
243 #endif /* !defined(IS_OS_WINDOWS) */
244 #endif /* MyHTML_BUILD_WITHOUT_THREADS */
245  
246 /*
247 *
248 * MyTHREAD logic
249 *
250 */
251  
252 1 mythread_t * mythread_create(void)
253 {
254 1 return calloc(1, sizeof(mythread_t));
255 }
256  
257 #ifdef MyHTML_BUILD_WITHOUT_THREADS
258  
259 myhtml_status_t mythread_init(mythread_t *mythread, const char *sem_prefix, size_t thread_count, size_t queue_size)
260 {
261 return MyHTML_STATUS_OK;
262 }
263  
264 #else /* MyHTML_BUILD_WITHOUT_THREADS */
265  
266 1 myhtml_status_t mythread_init(mythread_t *mythread, const char *sem_prefix, size_t thread_count, size_t queue_size)
267 {
268 1 mythread->batch_count = 0;
269 1 mythread->batch_first_id = 0;
270 1 mythread->stream_opt = MyTHREAD_OPT_STOP;
271 1 mythread->batch_opt = MyTHREAD_OPT_STOP;
272
273 1 ▶ if(thread_count)
Branch: 1 branch
BranchTrueFalseTotal
if(thread_count) 1 0 1
274 {
275 1 myhtml_status_t status = myhtml_thread_attr_init(mythread);
276 1 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 1 1
277 0 return status;
278
279 1 mythread->pth_list_root = 1;
280 1 mythread->pth_list_length = 1;
281 1 mythread->pth_list_size = thread_count + 1;
282 1 mythread->pth_list = (mythread_list_t*)calloc(mythread->pth_list_size, sizeof(mythread_list_t));
283
284 1 ▶ if(mythread->pth_list == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->pth_list == NULL) 0 1 1
285 0 return MyHTML_STATUS_THREAD_ERROR_LIST_INIT;
286 }
287 else {
288 0 myhtml_thread_attr_clean(mythread);
289
290 0 mythread->sys_last_error = 0;
291 0 mythread->pth_list_root = 1;
292 0 mythread->pth_list_length = 1;
293 0 mythread->pth_list_size = 0;
294 0 mythread->pth_list = NULL;
295 }
296
297 myhtml_status_t status;
298 1 mythread->queue_list = mythread_queue_list_create(mythread, 1024, &status);
299
300 1 ▶ if(mythread->queue_list == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->queue_list == NULL) 0 1 1
301 0 return status;
302
303 1 ▶ if(sem_prefix)
Branch: 1 branch
BranchTrueFalseTotal
if(sem_prefix) 1 0 1
304 {
305 1 mythread->sem_prefix_length = strlen(sem_prefix);
306
307 1 ▶ if(mythread->sem_prefix_length) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->sem_prefix_length) { 1 0 1
308 1 mythread->sem_prefix = calloc((mythread->sem_prefix_length + 1), sizeof(char));
309
310 1 ▶ if(mythread->sem_prefix == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->sem_prefix == NULL) { 0 1 1
311 0 mythread->sem_prefix_length = 0;
312 0 return MyHTML_STATUS_THREAD_ERROR_SEM_PREFIX_MALLOC;
313 }
314
315 1 myhtml_string_raw_copy(mythread->sem_prefix, sem_prefix, mythread->sem_prefix_length);
316 }
317 }
318
319 1 return MyHTML_STATUS_OK;
320 }
321  
322 #endif /* MyHTML_BUILD_WITHOUT_THREADS */
323  
324 1 void mythread_clean(mythread_t *mythread)
325 {
326 1 mythread->sys_last_error = 0;
327 1 }
328  
329 1 mythread_t * mythread_destroy(mythread_t *mythread, bool self_destroy)
330 {
331 1 ▶ if(mythread == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread == NULL) 0 1 1
332 0 return NULL;
333
334 #ifndef MyHTML_BUILD_WITHOUT_THREADS
335
336 1 myhtml_thread_attr_destroy(mythread);
337
338 1 ▶ if(mythread->pth_list) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->pth_list) { 1 0 1
339 1 mythread_resume_all(mythread);
340 1 mythread_stream_quit_all(mythread);
341 1 mythread_batch_quit_all(mythread);
342 1 mythread_wait_all_for_done(mythread);
343
344 4 ▶ for (size_t i = mythread->pth_list_root; i < mythread->pth_list_length; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = mythread->pth_list_root; i < mythread->pth_list_length; i++) 3 1 4
345 {
346 3 myhtml_thread_join(mythread, &mythread->pth_list[i]);
347 }
348
349 1 free(mythread->pth_list);
350 1 mythread->pth_list = NULL;
351 }
352
353 1 ▶ if(mythread->queue_list) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->queue_list) { 1 0 1
354 1 free(mythread->queue_list);
355 }
356
357 1 ▶ if(mythread->sem_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->sem_prefix) { 1 0 1
358 1 free(mythread->sem_prefix);
359
360 1 mythread->sem_prefix = NULL;
361 1 mythread->sem_prefix_length = 0;
362 }
363
364 #endif /* MyHTML_BUILD_WITHOUT_THREADS */
365
366 1 ▶ if(self_destroy) {
Branch: 1 branch
BranchTrueFalseTotal
if(self_destroy) { 1 0 1
367 1 free(mythread);
368 1 return NULL;
369 }
370
371 0 return mythread;
372 }
373  
374 #ifndef MyHTML_BUILD_WITHOUT_THREADS
375  
376 3 mythread_id_t _myhread_create_stream_raw(mythread_t *mythread, mythread_f func, void *work_func, myhtml_status_t *status, size_t total_count)
377 {
378 3 mythread->sys_last_error = 0;
379
380 3 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 3 0 3
381 3 *status = MyHTML_STATUS_OK;
382
383 3 ▶ if(mythread->pth_list_length >= mythread->pth_list_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->pth_list_length >= mythread->pth_list_size) { 0 3 3
384 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
385 0 *status = MyHTML_STATUS_THREAD_ERROR_NO_SLOTS;
386
387 0 return 0;
388 }
389
390 3 mythread_list_t *thr = &mythread->pth_list[mythread->pth_list_length];
391
392 3 thr->data.mythread = mythread;
393 3 thr->data.func = func;
394 3 thr->data.id = mythread->pth_list_length;
395 3 thr->data.t_count = total_count;
396 3 thr->data.opt = MyTHREAD_OPT_STOP;
397
398 3 myhtml_status_t m_status = myhtml_hread_mutex_create(mythread, &thr->data, 0);
399
400 3 ▶ if(m_status != MyHTML_STATUS_OK && status) {
Branch: 2 branches
BranchTrueFalseTotal
if(m_status != MyHTML_STATUS_OK && status) { 0 3 3
if(m_status != MyHTML_STATUS_OK && status) { 0 0 0
401 0 *status = m_status;
402 0 return 0;
403 }
404
405 3 m_status = myhtml_thread_create(mythread, thr, work_func);
406 3 ▶ if(m_status != MyHTML_STATUS_OK)
Branch: 1 branch
BranchTrueFalseTotal
if(m_status != MyHTML_STATUS_OK) 0 3 3
407 0 return 0;
408
409 3 mythread->pth_list_length++;
410 3 return thr->data.id;
411 }
412  
413 2 mythread_id_t myhread_create_stream(mythread_t *mythread, mythread_f func, myhtml_status_t *status)
414 {
415 2 return _myhread_create_stream_raw(mythread, func, mythread_function_stream, status, 0);
416 }
417  
418 1 mythread_id_t myhread_create_batch(mythread_t *mythread, mythread_f func, myhtml_status_t *status, size_t count)
419 {
420 1 ▶ if(mythread->batch_count) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->batch_count) { 0 1 1
421 0 *status = MyHTML_STATUS_THREAD_ERROR_BATCH_INIT;
422 0 return 0;
423 }
424 1 ▶ else if((mythread->pth_list_length + count) > mythread->pth_list_size) {
Branch: 1 branch
BranchTrueFalseTotal
else if((mythread->pth_list_length + count) > mythread->pth_list_size) { 0 1 1
425 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
426 0 *status = MyHTML_STATUS_THREAD_ERROR_NO_SLOTS;
427
428 0 return 0;
429 }
430
431 1 ▶ if(count == 0)
Branch: 1 branch
BranchTrueFalseTotal
if(count == 0) 0 1 1
432 0 count = 1;
433
434 1 mythread->batch_first_id = 0;
435 1 mythread->batch_count = count;
436
437 1 size_t start = mythread->pth_list_length;
438 1 *status = MyHTML_STATUS_OK;
439
440 1 bool init_first = false;
441
442 2 ▶ for (size_t i = 0; i < count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < count; i++) 1 1 2
443 {
444 1 mythread_id_t curr_id = _myhread_create_stream_raw(mythread, func, mythread_function_batch, status, i);
445
446 1 ▶ if(init_first == false) {
Branch: 1 branch
BranchTrueFalseTotal
if(init_first == false) { 1 0 1
447 1 mythread->batch_first_id = curr_id;
448 1 init_first = true;
449 }
450
451 1 ▶ if(*status)
Branch: 1 branch
BranchTrueFalseTotal
if(*status) 0 1 1
452 {
453 0 ▶ for (size_t n = start; n < (start + i); n++)
Branch: 1 branch
BranchTrueFalseTotal
for (size_t n = start; n < (start + i); n++) 0 0 0
454 {
455 0 mythread_list_t *thr = &mythread->pth_list[n];
456
457 0 myhtml_thread_cancel(mythread, thr);
458
459 0 myhtml_hread_mutex_post(mythread, &thr->data);
460 0 myhtml_hread_mutex_close(mythread, &thr->data);
461 }
462
463 0 mythread->batch_first_id = 0;
464 0 mythread->batch_count = 0;
465
466 0 break;
467 }
468 }
469
470 1 return mythread->batch_first_id;
471 }
472  
473 #endif /* MyHTML_BUILD_WITHOUT_THREADS */
474  
475 // mythread queue functions
476 #ifndef MyHTML_BUILD_WITHOUT_THREADS
477 1 mythread_queue_list_t * mythread_queue_list_create(mythread_t *mythread, size_t size, myhtml_status_t *status)
478 {
479 1 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 1 0 1
480 1 *status = MyHTML_STATUS_OK;
481
482 1 ▶ if(size < 1024)
Branch: 1 branch
BranchTrueFalseTotal
if(size < 1024) 0 1 1
483 0 size = 1024;
484
485 1 mythread_queue_list_t* queue_list = (mythread_queue_list_t*)mycalloc(1, sizeof(mythread_queue_list_t));
486
487 1 ▶ if(queue_list == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue_list == NULL) { 0 1 1
488 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
489 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_MALLOC;
490 0 return NULL;
491 }
492
493 1 return queue_list;
494 }
495  
496 0 mythread_queue_list_entry_t * mythread_queue_list_entry_push(mythread_t *mythread, mythread_queue_t *queue, myhtml_status_t *status)
497 {
498 0 mythread_queue_list_t *queue_list = mythread->queue_list;
499
500 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
501 0 *status = MyHTML_STATUS_OK;
502
503 0 mythread_queue_list_entry_t* entry = (mythread_queue_list_entry_t*)mycalloc(1, sizeof(mythread_queue_list_entry_t));
504
505 0 ▶ if(entry == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(entry == NULL) { 0 0 0
506 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
507 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_MALLOC;
508 0 return NULL;
509 }
510
511 0 entry->thread_param = (mythread_queue_thread_param_t*)mycalloc(mythread->pth_list_size, sizeof(mythread_queue_thread_param_t));
512
513 0 ▶ if(entry == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(entry == NULL) { 0 0 0
514 0 free(entry);
515
516 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
517 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_MALLOC;
518 0 return NULL;
519 }
520
521 size_t idx;
522 0 ▶ for (idx = mythread->batch_first_id; idx < (mythread->batch_first_id + mythread->batch_count); idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = mythread->batch_first_id; idx < (mythread->batch_first_id + mythread->batch_count); idx++) { 0 0 0
523 0 entry->thread_param[idx].use = mythread->pth_list[idx].data.t_count;
524 }
525
526 0 entry->queue = queue;
527
528 0 ▶ if(mythread->stream_opt == MyTHREAD_OPT_UNDEF) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->stream_opt == MyTHREAD_OPT_UNDEF) { 0 0 0
529 0 mythread_suspend_all(mythread);
530 }
531 0 ▶ else if(mythread->stream_opt == MyTHREAD_OPT_STOP) {
Branch: 1 branch
BranchTrueFalseTotal
else if(mythread->stream_opt == MyTHREAD_OPT_STOP) { 0 0 0
532 0 mythread_stop_all(mythread);
533 }
534
535 0 ▶ if(queue_list->first) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue_list->first) { 0 0 0
536 0 queue_list->last->next = entry;
537 0 entry->prev = queue_list->last;
538
539 0 queue_list->last = entry;
540 }
541 else {
542 0 queue_list->first = entry;
543 0 queue_list->last = entry;
544 }
545
546 0 queue_list->count++;
547
548 0 ▶ if(mythread->stream_opt != MyTHREAD_OPT_STOP)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->stream_opt != MyTHREAD_OPT_STOP) 0 0 0
549 0 mythread_resume_all(mythread);
550
551 0 return entry;
552 }
553  
554 0 mythread_queue_list_entry_t * mythread_queue_list_entry_delete(mythread_t *mythread, mythread_queue_list_entry_t *entry, bool destroy_queue)
555 {
556 0 mythread_queue_list_t *queue_list = mythread->queue_list;
557
558 0 mythread_queue_list_entry_t *next = entry->next;
559 0 mythread_queue_list_entry_t *prev = entry->prev;
560
561 0 ▶ if(mythread->stream_opt == MyTHREAD_OPT_UNDEF) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->stream_opt == MyTHREAD_OPT_UNDEF) { 0 0 0
562 0 mythread_suspend_all(mythread);
563 }
564 0 ▶ else if(mythread->stream_opt == MyTHREAD_OPT_STOP) {
Branch: 1 branch
BranchTrueFalseTotal
else if(mythread->stream_opt == MyTHREAD_OPT_STOP) { 0 0 0
565 0 mythread_stop_all(mythread);
566 }
567
568 0 ▶ if(prev)
Branch: 1 branch
BranchTrueFalseTotal
if(prev) 0 0 0
569 0 prev->next = next;
570
571 0 ▶ if(next)
Branch: 1 branch
BranchTrueFalseTotal
if(next) 0 0 0
572 0 next->prev = prev;
573
574 0 ▶ if(queue_list->first == entry)
Branch: 1 branch
BranchTrueFalseTotal
if(queue_list->first == entry) 0 0 0
575 0 queue_list->first = next;
576
577 0 ▶ if(queue_list->last == entry)
Branch: 1 branch
BranchTrueFalseTotal
if(queue_list->last == entry) 0 0 0
578 0 queue_list->last = prev;
579
580 0 ▶ if(mythread->stream_opt != MyTHREAD_OPT_STOP)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->stream_opt != MyTHREAD_OPT_STOP) 0 0 0
581 0 mythread_resume_all(mythread);
582
583 0 ▶ if(destroy_queue && entry->queue)
Branch: 2 branches
BranchTrueFalseTotal
if(destroy_queue && entry->queue) 0 0 0
if(destroy_queue && entry->queue) 0 0 0
584 0 mythread_queue_destroy(entry->queue);
585
586 0 ▶ if(entry->thread_param)
Branch: 1 branch
BranchTrueFalseTotal
if(entry->thread_param) 0 0 0
587 0 free(entry->thread_param);
588
589 0 free(entry);
590
591 0 queue_list->count--;
592
593 0 return NULL;
594 }
595  
596 1 void mythread_queue_list_entry_clean(mythread_t *mythread, mythread_queue_list_entry_t *entry)
597 {
598 1 ▶ if(entry == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(entry == NULL) 1 0 1
599 1 return;
600
601 0 mythread_queue_clean(entry->queue);
602
603 size_t idx;
604 0 ▶ for (idx = mythread->pth_list_root; idx < mythread->batch_first_id; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = mythread->pth_list_root; idx < mythread->batch_first_id; idx++) { 0 0 0
605 0 entry->thread_param[idx].use = 0;
606 }
607
608 0 ▶ for (idx = mythread->batch_first_id; idx < (mythread->batch_first_id + mythread->batch_count); idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = mythread->batch_first_id; idx < (mythread->batch_first_id + mythread->batch_count); idx++) { 0 0 0
609 0 entry->thread_param[idx].use = mythread->pth_list[idx].data.t_count;
610 }
611 }
612  
613 0 void mythread_queue_list_entry_wait_for_done(mythread_t *mythread, mythread_queue_list_entry_t *entry)
614 {
615 0 ▶ if(entry == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(entry == NULL) 0 0 0
616 0 return;
617
618 size_t idx;
619 0 const struct timespec tomeout = {0, 0};
620
621 0 ▶ for (idx = mythread->pth_list_root; idx < mythread->pth_list_size; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = mythread->pth_list_root; idx < mythread->pth_list_size; idx++) { 0 0 0
622 0 mythread_queue_thread_param_t *thread_param = &entry->thread_param[ idx ];
623 0 ▶ while(thread_param->use < entry->queue->nodes_uses) {
Branch: 1 branch
BranchTrueFalseTotal
while(thread_param->use < entry->queue->nodes_uses) { 0 0 0
624 0 myhtml_thread_nanosleep(&tomeout);
625 }
626 }
627 }
628  
629 #endif /* MyHTML_BUILD_WITHOUT_THREADS */
630  
631 1 mythread_queue_t * mythread_queue_create(size_t size, myhtml_status_t *status)
632 {
633 1 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 1 0 1
634 1 *status = MyHTML_STATUS_OK;
635
636 1 ▶ if(size < 4096)
Branch: 1 branch
BranchTrueFalseTotal
if(size < 4096) 0 1 1
637 0 size = 4096;
638
639 1 mythread_queue_t* queue = (mythread_queue_t*)mymalloc(sizeof(mythread_queue_t));
640
641 1 ▶ if(queue == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue == NULL) { 0 1 1
642 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
643 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_MALLOC;
644 0 return NULL;
645 }
646
647 1 queue->nodes_pos_size = 512;
648 1 queue->nodes_size = size;
649 1 queue->nodes = (mythread_queue_node_t**)mycalloc(queue->nodes_pos_size, sizeof(mythread_queue_node_t*));
650
651 1 ▶ if(queue->nodes == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes == NULL) { 0 1 1
652 0 free(queue);
653
654 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
655 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_NODES_MALLOC;
656 0 return NULL;
657 }
658
659 1 mythread_queue_clean(queue);
660
661 1 queue->nodes[queue->nodes_pos] = (mythread_queue_node_t*)mymalloc(sizeof(mythread_queue_node_t) * queue->nodes_size);
662
663 1 ▶ if(queue->nodes[queue->nodes_pos] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes[queue->nodes_pos] == NULL) { 0 1 1
664 0 free(queue->nodes);
665 0 free(queue);
666
667 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
668 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_NODE_MALLOC;
669 0 return NULL;
670 }
671
672 1 return queue;
673 }
674  
675 2 void mythread_queue_clean(mythread_queue_t* queue)
676 {
677 2 queue->nodes_length = 0;
678 2 queue->nodes_pos = 0;
679 2 queue->nodes_root = 0;
680 2 queue->nodes_uses = 0;
681
682 2 ▶ if(queue->nodes[queue->nodes_pos])
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes[queue->nodes_pos]) 1 1 2
683 1 mythread_queue_node_clean(&queue->nodes[queue->nodes_pos][queue->nodes_length]);
684 2 }
685  
686 1 mythread_queue_t * mythread_queue_destroy(mythread_queue_t* queue)
687 {
688 1 ▶ if(queue == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(queue == NULL) 0 1 1
689 0 return NULL;
690
691 1 ▶ if(queue->nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes) { 1 0 1
692 2 ▶ for (size_t i = 0; i <= queue->nodes_pos; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i <= queue->nodes_pos; i++) { 1 1 2
693 1 free(queue->nodes[i]);
694 }
695
696 1 free(queue->nodes);
697 }
698
699 1 free(queue);
700
701 1 return NULL;
702 }
703  
704 1 void mythread_queue_node_clean(mythread_queue_node_t* qnode)
705 {
706 1 memset(qnode, 0, sizeof(mythread_queue_node_t));
707 1 }
708  
709 0 mythread_queue_node_t * mythread_queue_get_prev_node(mythread_queue_node_t* qnode)
710 {
711 0 return qnode->prev;
712 }
713  
714 0 mythread_queue_node_t * mythread_queue_get_current_node(mythread_queue_t* queue)
715 {
716 0 return &queue->nodes[queue->nodes_pos][queue->nodes_length];
717 }
718  
719 0 mythread_queue_node_t * mythread_queue_get_first_node(mythread_queue_t* queue)
720 {
721 0 return &queue->nodes[0][0];
722 }
723  
724 0 size_t mythread_queue_count_used_node(mythread_queue_t* queue)
725 {
726 0 return queue->nodes_uses;
727 }
728  
729 0 mythread_queue_node_t * mythread_queue_node_malloc(mythread_t *mythread, mythread_queue_t* queue, const char* text, size_t begin, myhtml_status_t *status)
730 {
731 0 queue->nodes_length++;
732
733 0 ▶ if(queue->nodes_length >= queue->nodes_size)
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes_length >= queue->nodes_size) 0 0 0
734 {
735 0 queue->nodes_pos++;
736
737 0 ▶ if(queue->nodes_pos >= queue->nodes_pos_size)
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes_pos >= queue->nodes_pos_size) 0 0 0
738 {
739 0 mythread_wait_all_for_done(mythread);
740
741 0 queue->nodes_pos_size <<= 1;
742 0 mythread_queue_node_t** tmp = realloc(queue->nodes, sizeof(mythread_queue_node_t*) * queue->nodes_pos_size);
743
744 0 ▶ if(tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) { 0 0 0
745 0 memset(&tmp[queue->nodes_pos], 0, sizeof(mythread_queue_node_t*) * (queue->nodes_pos_size - queue->nodes_pos));
746
747 0 queue->nodes = tmp;
748 }
749 else {
750 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
751 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_NODES_MALLOC;
752
753 0 return NULL;
754 }
755 }
756
757 0 ▶ if(queue->nodes[queue->nodes_pos] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes[queue->nodes_pos] == NULL) { 0 0 0
758 0 queue->nodes[queue->nodes_pos] = (mythread_queue_node_t*)malloc(sizeof(mythread_queue_node_t) * queue->nodes_size);
759
760 0 ▶ if(queue->nodes[queue->nodes_pos] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes[queue->nodes_pos] == NULL) { 0 0 0
761 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
762 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_NODE_MALLOC;
763
764 0 return NULL;
765 }
766 }
767
768 0 queue->nodes_length = 0;
769 }
770
771 0 queue->nodes_uses++;
772
773 0 mythread_queue_node_t *qnode = &queue->nodes[queue->nodes_pos][queue->nodes_length];
774
775 0 qnode->text = text;
776 0 qnode->begin = begin;
777
778 0 return qnode;
779 }
780  
781 0 mythread_queue_node_t * mythread_queue_node_malloc_limit(mythread_t *mythread, mythread_queue_t* queue, const char* text, size_t begin, size_t limit, myhtml_status_t *status)
782 {
783 0 queue->nodes_length++;
784
785 0 ▶ if(queue->nodes_uses >= limit) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes_uses >= limit) { 0 0 0
786 0 queue->nodes_uses++;
787 0 mythread_wait_all_for_done(mythread);
788
789 0 queue->nodes_length = 0;
790 0 queue->nodes_pos = 0;
791 0 queue->nodes_root = 0;
792 0 queue->nodes_uses = 0;
793 }
794 0 ▶ else if(queue->nodes_length >= queue->nodes_size)
Branch: 1 branch
BranchTrueFalseTotal
else if(queue->nodes_length >= queue->nodes_size) 0 0 0
795 {
796 0 queue->nodes_pos++;
797
798 0 ▶ if(queue->nodes_pos >= queue->nodes_pos_size)
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes_pos >= queue->nodes_pos_size) 0 0 0
799 {
800 0 mythread_wait_all_for_done(mythread);
801
802 0 queue->nodes_pos_size <<= 1;
803 0 mythread_queue_node_t** tmp = realloc(queue->nodes, sizeof(mythread_queue_node_t*) * queue->nodes_pos_size);
804
805 0 ▶ if(tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) { 0 0 0
806 0 memset(&tmp[queue->nodes_pos], 0, sizeof(mythread_queue_node_t*) * (queue->nodes_pos_size - queue->nodes_pos));
807
808 0 queue->nodes = tmp;
809 }
810 else {
811 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
812 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_NODES_MALLOC;
813
814 0 return NULL;
815 }
816 }
817
818 0 ▶ if(queue->nodes[queue->nodes_pos] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes[queue->nodes_pos] == NULL) { 0 0 0
819 0 queue->nodes[queue->nodes_pos] = (mythread_queue_node_t*)malloc(sizeof(mythread_queue_node_t) * queue->nodes_size);
820
821 0 ▶ if(queue->nodes[queue->nodes_pos] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes[queue->nodes_pos] == NULL) { 0 0 0
822 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
823 0 *status = MyHTML_STATUS_THREAD_ERROR_QUEUE_NODE_MALLOC;
824
825 0 return NULL;
826 }
827 }
828
829 0 queue->nodes_length = 0;
830 }
831
832 0 queue->nodes_uses++;
833
834 0 mythread_queue_node_t *qnode = &queue->nodes[queue->nodes_pos][queue->nodes_length];
835
836 0 qnode->text = text;
837 0 qnode->begin = begin;
838
839 0 return qnode;
840 }
841  
842 #ifndef MyHTML_BUILD_WITHOUT_THREADS
843  
844 0 mythread_queue_node_t * mythread_queue_node_malloc_round(mythread_t *mythread, mythread_queue_list_entry_t *entry,
845 const char* text, size_t begin, myhtml_status_t *status)
846 {
847 0 mythread_queue_t* queue = entry->queue;
848
849 0 queue->nodes_length++;
850
851 0 ▶ if(queue->nodes_length >= queue->nodes_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(queue->nodes_length >= queue->nodes_size) { 0 0 0
852 0 queue->nodes_uses++;
853
854 0 mythread_queue_list_entry_wait_for_done(mythread, entry);
855 0 mythread_queue_list_entry_clean(mythread, entry);
856 }
857 else
858 0 queue->nodes_uses++;
859
860 0 mythread_queue_node_t *qnode = &queue->nodes[queue->nodes_pos][queue->nodes_length];
861
862 0 qnode->text = text;
863 0 qnode->begin = begin;
864
865 0 return qnode;
866 }
867  
868 #endif /* MyHTML_BUILD_WITHOUT_THREADS */
869  
870 #ifdef MyHTML_BUILD_WITHOUT_THREADS
871  
872 void mythread_stream_quit_all(mythread_t *mythread) {}
873 void mythread_batch_quit_all(mythread_t *mythread) {}
874 void mythread_stream_stop_all(mythread_t *mythread) {};
875 void mythread_batch_stop_all(mythread_t *mythread) {};
876 void mythread_stop_all(mythread_t *mythread) {};
877 void mythread_resume_all(mythread_t *mythread) {};
878 void mythread_wait_all_for_done(mythread_t *mythread) {};
879 void mythread_suspend_all(mythread_t *mythread) {};
880  
881 #else /* MyHTML_BUILD_WITHOUT_THREADS */
882  
883 // mythread functions
884 1 void mythread_stream_quit_all(mythread_t *mythread)
885 {
886 1 mythread->stream_opt = MyTHREAD_OPT_QUIT;
887 1 }
888  
889 1 void mythread_batch_quit_all(mythread_t *mythread)
890 {
891 1 mythread->batch_opt = MyTHREAD_OPT_QUIT;
892 1 }
893  
894 0 void mythread_stream_stop_all(mythread_t *mythread)
895 {
896 0 ▶ if(mythread->stream_opt != MyTHREAD_OPT_STOP)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->stream_opt != MyTHREAD_OPT_STOP) 0 0 0
897 0 mythread->stream_opt = MyTHREAD_OPT_STOP;
898
899 size_t idx;
900 0 const struct timespec tomeout = {0, 0};
901
902 0 ▶ for (idx = mythread->pth_list_root; idx < mythread->batch_first_id; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = mythread->pth_list_root; idx < mythread->batch_first_id; idx++) { 0 0 0
903 0 ▶ while(mythread->pth_list[idx].data.opt != MyTHREAD_OPT_STOP) {
Branch: 1 branch
BranchTrueFalseTotal
while(mythread->pth_list[idx].data.opt != MyTHREAD_OPT_STOP) { 0 0 0
904 0 myhtml_thread_nanosleep(&tomeout);
905 }
906 }
907 0 }
908  
909 0 void mythread_batch_stop_all(mythread_t *mythread)
910 {
911 0 ▶ if(mythread->batch_opt != MyTHREAD_OPT_STOP)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->batch_opt != MyTHREAD_OPT_STOP) 0 0 0
912 0 mythread->batch_opt = MyTHREAD_OPT_STOP;
913
914 size_t idx;
915 0 const struct timespec tomeout = {0, 0};
916
917 0 ▶ for (idx = mythread->batch_first_id; idx < (mythread->batch_first_id + mythread->batch_count); idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = mythread->batch_first_id; idx < (mythread->batch_first_id + mythread->batch_count); idx++) { 0 0 0
918 0 ▶ while(mythread->pth_list[idx].data.opt != MyTHREAD_OPT_STOP) {
Branch: 1 branch
BranchTrueFalseTotal
while(mythread->pth_list[idx].data.opt != MyTHREAD_OPT_STOP) { 0 0 0
919 0 myhtml_thread_nanosleep(&tomeout);
920 }
921 }
922 0 }
923  
924 0 void mythread_stop_all(mythread_t *mythread)
925 {
926 0 mythread_stream_stop_all(mythread);
927 0 mythread_batch_stop_all(mythread);
928 0 }
929  
930 1 void mythread_resume_all(mythread_t *mythread)
931 {
932 1 ▶ if(mythread->stream_opt == MyTHREAD_OPT_UNDEF &&
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->stream_opt == MyTHREAD_OPT_UNDEF && 0 1 1
933 0 ▶ mythread->batch_opt == MyTHREAD_OPT_UNDEF)
Branch: 1 branch
BranchTrueFalseTotal
mythread->batch_opt == MyTHREAD_OPT_UNDEF) 0 0 0
934 0 return;
935
936 1 ▶ if(mythread->stream_opt == MyTHREAD_OPT_WAIT ||
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->stream_opt == MyTHREAD_OPT_WAIT || 1 0 1
937 1 ▶ mythread->batch_opt == MyTHREAD_OPT_WAIT)
Branch: 1 branch
BranchTrueFalseTotal
mythread->batch_opt == MyTHREAD_OPT_WAIT) 0 1 1
938 {
939 0 mythread->stream_opt = MyTHREAD_OPT_UNDEF;
940 0 mythread->batch_opt = MyTHREAD_OPT_UNDEF;
941 }
942 else {
943 1 mythread->stream_opt = MyTHREAD_OPT_UNDEF;
944 1 mythread->batch_opt = MyTHREAD_OPT_UNDEF;
945
946 4 ▶ for (size_t idx = mythread->pth_list_root; idx < mythread->pth_list_size; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t idx = mythread->pth_list_root; idx < mythread->pth_list_size; idx++) { 3 1 4
947 3 myhtml_hread_mutex_post(mythread, &mythread->pth_list[idx].data);
948 }
949 }
950 }
951  
952 1 void mythread_wait_all_for_done(mythread_t *mythread)
953 {
954 1 const struct timespec tomeout = {0, 0};
955
956 1 mythread_queue_list_t *queue_list = mythread->queue_list;
957 1 mythread_queue_list_entry_t *entry = queue_list->first;
958
959 1 ▶ while(entry)
Branch: 1 branch
BranchTrueFalseTotal
while(entry) 0 1 1
960 {
961 0 ▶ for (size_t idx = mythread->pth_list_root; idx < mythread->pth_list_size; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t idx = mythread->pth_list_root; idx < mythread->pth_list_size; idx++) { 0 0 0
962 0 ▶ while(entry->thread_param[idx].use < entry->queue->nodes_uses) {
Branch: 1 branch
BranchTrueFalseTotal
while(entry->thread_param[idx].use < entry->queue->nodes_uses) { 0 0 0
963 0 myhtml_thread_nanosleep(&tomeout);
964 }
965 }
966
967 0 entry = entry->next;
968 }
969 1 }
970  
971 0 void mythread_suspend_all(mythread_t *mythread)
972 {
973 0 ▶ if(mythread->stream_opt != MyTHREAD_OPT_WAIT)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->stream_opt != MyTHREAD_OPT_WAIT) 0 0 0
974 0 mythread->stream_opt = MyTHREAD_OPT_WAIT;
975
976 0 ▶ if(mythread->batch_opt != MyTHREAD_OPT_WAIT)
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->batch_opt != MyTHREAD_OPT_WAIT) 0 0 0
977 0 mythread->batch_opt = MyTHREAD_OPT_WAIT;
978
979 0 const struct timespec tomeout = {0, 0};
980
981 0 ▶ for (size_t idx = mythread->pth_list_root; idx < mythread->pth_list_size; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t idx = mythread->pth_list_root; idx < mythread->pth_list_size; idx++) { 0 0 0
982 0 myhtml_hread_mutex_try_wait(mythread, &mythread->pth_list[idx].data);
983
984 0 ▶ while(mythread->pth_list[idx].data.opt != MyTHREAD_OPT_WAIT) {
Branch: 1 branch
BranchTrueFalseTotal
while(mythread->pth_list[idx].data.opt != MyTHREAD_OPT_WAIT) { 0 0 0
985 0 myhtml_thread_nanosleep(&tomeout);
986 }
987 }
988 0 }
989  
990 3 bool mythread_function_see_for_all_done(mythread_queue_list_t *queue_list, size_t thread_id)
991 {
992 3 size_t done_count = 0;
993
994 3 mythread_queue_list_entry_t *entry = queue_list->first;
995 3 ▶ while(entry)
Branch: 1 branch
BranchTrueFalseTotal
while(entry) 0 3 3
996 {
997 0 ▶ if(entry->thread_param[ thread_id ].use >= entry->queue->nodes_uses) {
Branch: 1 branch
BranchTrueFalseTotal
if(entry->thread_param[ thread_id ].use >= entry->queue->nodes_uses) { 0 0 0
998 0 done_count++;
999 0 entry = entry->next;
1000 }
1001 else
1002 0 break;
1003 }
1004
1005 3 return done_count == queue_list->count;
1006 }
1007  
1008 3 bool mythread_function_see_opt(mythread_context_t *ctx, volatile mythread_thread_opt_t opt, size_t done_count, const struct timespec *timeout)
1009 {
1010 3 mythread_t * mythread = ctx->mythread;
1011 3 mythread_queue_list_t *queue_list = mythread->queue_list;
1012
1013 3 ▶ if(done_count != queue_list->count)
Branch: 1 branch
BranchTrueFalseTotal
if(done_count != queue_list->count) 0 3 3
1014 0 return false;
1015
1016 3 ▶ if(opt & MyTHREAD_OPT_STOP)
Branch: 1 branch
BranchTrueFalseTotal
if(opt & MyTHREAD_OPT_STOP) 0 3 3
1017 {
1018 0 ▶ if(mythread_function_see_for_all_done(queue_list, ctx->id))
Branch: 1 branch
BranchTrueFalseTotal
if(mythread_function_see_for_all_done(queue_list, ctx->id)) 0 0 0
1019 {
1020 0 ctx->opt = MyTHREAD_OPT_STOP;
1021 0 myhtml_hread_mutex_wait(mythread, ctx);
1022 0 ctx->opt = MyTHREAD_OPT_UNDEF;
1023
1024 0 return false;
1025 }
1026 }
1027 3 ▶ else if(opt & MyTHREAD_OPT_QUIT)
Branch: 1 branch
BranchTrueFalseTotal
else if(opt & MyTHREAD_OPT_QUIT) 3 0 3
1028 {
1029 3 ▶ if(mythread_function_see_for_all_done(queue_list, ctx->id))
Branch: 1 branch
BranchTrueFalseTotal
if(mythread_function_see_for_all_done(queue_list, ctx->id)) 3 0 3
1030 {
1031 3 myhtml_hread_mutex_close(mythread, ctx);
1032 3 ctx->opt = MyTHREAD_OPT_QUIT;
1033 3 return true;
1034 }
1035 }
1036
1037 0 myhtml_thread_nanosleep(timeout);
1038
1039 0 return false;
1040 }
1041  
1042 1 void mythread_function_batch(void *arg)
1043 {
1044 1 mythread_context_t *ctx = (mythread_context_t*)arg;
1045 1 mythread_t * mythread = ctx->mythread;
1046 1 mythread_queue_list_t *queue_list = mythread->queue_list;
1047
1048 1 const struct timespec timeout = {0, 0};
1049 1 myhtml_hread_mutex_wait(mythread, ctx);
1050
1051 0 do {
1052 1 ▶ if(mythread->batch_opt & MyTHREAD_OPT_WAIT) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->batch_opt & MyTHREAD_OPT_WAIT) { 0 1 1
1053 0 ctx->opt = MyTHREAD_OPT_WAIT;
1054
1055 0 ▶ while (mythread->batch_opt & MyTHREAD_OPT_WAIT) {
Branch: 1 branch
BranchTrueFalseTotal
while (mythread->batch_opt & MyTHREAD_OPT_WAIT) { 0 0 0
1056 0 myhtml_thread_nanosleep(&timeout);
1057 }
1058
1059 0 ctx->opt = MyTHREAD_OPT_UNDEF;
1060 }
1061
1062 1 mythread_queue_list_entry_t *entry = queue_list->first;
1063 1 size_t done_count = 0;
1064
1065 1 ▶ while(entry)
Branch: 1 branch
BranchTrueFalseTotal
while(entry) 0 1 1
1066 {
1067 0 mythread_queue_thread_param_t *thread_param = &entry->thread_param[ ctx->id ];
1068
1069 0 ▶ if(thread_param->use < entry->queue->nodes_uses)
Branch: 1 branch
BranchTrueFalseTotal
if(thread_param->use < entry->queue->nodes_uses) 0 0 0
1070 {
1071 0 size_t pos = thread_param->use / entry->queue->nodes_size;
1072 0 size_t len = thread_param->use % entry->queue->nodes_size;
1073
1074 0 mythread_queue_node_t *qnode = &entry->queue->nodes[pos][len];
1075
1076 0 ▶ if((qnode->tree->flags & MyHTML_TREE_FLAGS_SINGLE_MODE) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if((qnode->tree->flags & MyHTML_TREE_FLAGS_SINGLE_MODE) == 0) 0 0 0
1077 0 ctx->func(ctx->id, qnode);
1078
1079 0 thread_param->use += mythread->batch_count;
1080 }
1081 else
1082 0 done_count++;
1083
1084 0 entry = entry->next;
1085 }
1086
1087 2 if(done_count == queue_list->count &&
1088 1 mythread_function_see_opt(ctx, mythread->batch_opt, done_count, &timeout))
1089 1 break;
1090 }
1091 while (1);
1092 1 }
1093  
1094 2 void mythread_function_stream(void *arg)
1095 {
1096 2 mythread_context_t *ctx = (mythread_context_t*)arg;
1097 2 mythread_t * mythread = ctx->mythread;
1098 2 mythread_queue_list_t *queue_list = mythread->queue_list;
1099
1100 2 const struct timespec timeout = {0, 0};
1101 2 myhtml_hread_mutex_wait(mythread, ctx);
1102
1103 0 do {
1104 2 ▶ if(mythread->stream_opt & MyTHREAD_OPT_WAIT) {
Branch: 1 branch
BranchTrueFalseTotal
if(mythread->stream_opt & MyTHREAD_OPT_WAIT) { 0 2 2
1105 0 ctx->opt = MyTHREAD_OPT_WAIT;
1106
1107 0 ▶ while (mythread->stream_opt & MyTHREAD_OPT_WAIT) {
Branch: 1 branch
BranchTrueFalseTotal
while (mythread->stream_opt & MyTHREAD_OPT_WAIT) { 0 0 0
1108 0 myhtml_thread_nanosleep(&timeout);
1109 }
1110
1111 0 ctx->opt = MyTHREAD_OPT_UNDEF;
1112 }
1113
1114 2 mythread_queue_list_entry_t *entry = queue_list->first;
1115 2 size_t done_count = 0;
1116
1117 2 ▶ while(entry)
Branch: 1 branch
BranchTrueFalseTotal
while(entry) 0 2 2
1118 {
1119 0 mythread_queue_thread_param_t *thread_param = &entry->thread_param[ ctx->id ];
1120
1121 0 ▶ if(thread_param->use < entry->queue->nodes_uses)
Branch: 1 branch
BranchTrueFalseTotal
if(thread_param->use < entry->queue->nodes_uses) 0 0 0
1122 {
1123 0 size_t pos = thread_param->use / entry->queue->nodes_size;
1124 0 size_t len = thread_param->use % entry->queue->nodes_size;
1125
1126 0 mythread_queue_node_t *qnode = &entry->queue->nodes[pos][len];
1127
1128 0 ▶ if((qnode->tree->flags & MyHTML_TREE_FLAGS_SINGLE_MODE) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if((qnode->tree->flags & MyHTML_TREE_FLAGS_SINGLE_MODE) == 0) 0 0 0
1129 0 ctx->func(ctx->id, qnode);
1130
1131 0 thread_param->use++;
1132 }
1133 else
1134 0 done_count++;
1135
1136 0 entry = entry->next;
1137 }
1138
1139 4 if(done_count == queue_list->count &&
1140 2 mythread_function_see_opt(ctx, mythread->stream_opt, done_count, &timeout))
1141 2 break;
1142 }
1143 while (1);
1144 2 }
1145  
1146 #endif /* MyHTML_BUILD_WITHOUT_THREADS */
1147  
1148  
« source/myhtml/utils/mchar_async.c ↑ Summary source/myhtml/utils/mcobject_async.c »