Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_thread_pool.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27
28#include <unistd.h>
29#include "nilorea/n_common.h"
30#include "nilorea/n_log.h"
32#include "nilorea/n_time.h"
33
34#ifdef __linux__
35#include <sys/sysinfo.h>
36#endif
37#include <pthread.h>
38#include <string.h>
39#include <errno.h>
40
47static long parallel_budget = -1;
48
50static pthread_mutex_t parallel_lock = PTHREAD_MUTEX_INITIALIZER;
51
53typedef struct {
55 size_t begin;
57 size_t end;
61 void* user;
63
69static void* n_parallel_slice(void* arg) {
71 slice->fn(slice->begin, slice->end, slice->user);
72 return NULL;
73}
74
75int n_parallel_for(size_t begin, size_t end, size_t grain, N_PARALLEL_FN fn, void* user) {
76 N_PARALLEL_SLICE slices[64];
77 pthread_t threads[64];
78 int started[64];
79 size_t count = 0, per = 0, at = 0;
80 long want = 0, got = 0;
81 int n = 0, i = 0;
82
83 if (!fn || end <= begin) return 0;
84 if (grain == 0) grain = 1;
85 count = end - begin;
86 /* how many slices the range is worth, at least a grain each, at most 64 */
87 want = (long)(count / grain);
88 if (want > 64) want = 64;
89 if (want < 2) {
90 fn(begin, end, user);
91 return 1;
92 }
93 pthread_mutex_lock(&parallel_lock);
94 if (parallel_budget < 0) {
95 long cores = get_nb_cpu_cores();
96 parallel_budget = cores > 1 ? cores - 1 : 0;
97 }
98 /* extra threads beyond the caller's own, from what is left of the budget */
99 got = want - 1;
100 if (got > parallel_budget) got = parallel_budget;
101 parallel_budget -= got;
102 pthread_mutex_unlock(&parallel_lock);
103 if (got <= 0) {
104 fn(begin, end, user);
105 return 1;
106 }
107 n = (int)got + 1;
108 per = count / (size_t)n;
109 /* cleared, so nothing is read before it was written: the joins read only
110 * the started handles and n is at least two here, which a static
111 * analyser cannot see across the budget's lock */
112 memset(threads, 0, sizeof(threads));
113 memset(slices, 0, sizeof(slices));
114 for (i = 0; i < n; i++) {
115 slices[i].begin = begin + at;
116 at += per + ((size_t)i < count % (size_t)n ? 1 : 0);
117 slices[i].end = begin + at;
118 slices[i].fn = fn;
119 slices[i].user = user;
120 started[i] = 0;
121 }
122 /* the others on threads, the first on this one; a thread that cannot be
123 * started has its slice done here instead */
124 for (i = 1; i < n; i++)
125 started[i] = pthread_create(&threads[i], NULL, n_parallel_slice, &slices[i]) == 0;
126 fn(slices[0].begin, slices[0].end, user);
127 for (i = 1; i < n; i++) {
128 if (started[i])
129 pthread_join(threads[i], NULL);
130 else
131 fn(slices[i].begin, slices[i].end, user);
132 }
133 pthread_mutex_lock(&parallel_lock);
134 parallel_budget += got;
135 pthread_mutex_unlock(&parallel_lock);
136 return n;
137}
138
140 long int nb_procs = 0;
141#ifdef __windows__
142 SYSTEM_INFO sysinfo;
143 GetSystemInfo(&sysinfo);
144 nb_procs = (long int)sysinfo.dwNumberOfProcessors;
145#else
146 nb_procs = sysconf(_SC_NPROCESSORS_ONLN);
147#endif
148 return nb_procs;
149}
150
157 THREAD_POOL_NODE* node = (THREAD_POOL_NODE*)param;
158
159 if (!node) {
160 n_log(LOG_ERR, "Thread fatal error, no valid payload found, exiting thread function !");
161 pthread_exit(NULL);
162 return NULL;
163 }
164
165 n_log(LOG_DEBUG, "Thread %llu started", (unsigned long long)(uintptr_t)node->thr);
166
167 int thread_state = 0;
168 do {
169 n_log(LOG_DEBUG, "Thread pool processing func waiting");
170
171 // note: direct procs will automatically post th_start
172 sem_wait(&node->th_start);
173
174 pthread_mutex_lock(&node->lock);
175 thread_state = node->thread_state;
176 pthread_mutex_unlock(&node->lock);
177
178 if (thread_state == RUNNING_THREAD) {
179 n_log(LOG_DEBUG, "Thread pool running proc %p", node->func);
180 void* (*func_to_run)(void* param) = NULL;
181 void* param_to_run = NULL;
182 pthread_mutex_lock(&node->lock);
183 node->state = RUNNING_PROC;
184 func_to_run = node->func;
185 param_to_run = node->param;
186 pthread_mutex_unlock(&node->lock);
187
188 if (func_to_run) {
189 func_to_run(param_to_run);
190 }
191 n_log(LOG_DEBUG, "Thread pool end proc %p", func_to_run);
192
193 pthread_mutex_lock(&node->lock);
194 node->func = NULL;
195 node->param = NULL;
196 node->state = IDLE_PROC;
197 int type = node->type;
198 node->type = -1;
199 // NORMAL_PROC or DIRECT_PROC do not need to post th_end
200 if (type & SYNCED_PROC)
201 sem_post(&node->th_end);
202 pthread_mutex_unlock(&node->lock);
203
205 }
206 } while (thread_state != EXITING_THREAD);
207
208 n_log(LOG_DEBUG, "Thread %llu exiting...", (unsigned long long)(uintptr_t)node->thr);
209
210 pthread_mutex_lock(&node->lock);
212 pthread_mutex_unlock(&node->lock);
213
214 n_log(LOG_DEBUG, "Thread %llu exited", (unsigned long long)(uintptr_t)node->thr);
215
216 pthread_exit(NULL);
217
218 return NULL;
219} /* thread_pool_processing_function */
220
227THREAD_POOL* new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting) {
228 THREAD_POOL* thread_pool = NULL;
229
231 if (!thread_pool)
232 return NULL;
233
234 thread_pool->max_threads = nbmaxthr;
235 thread_pool->nb_max_waiting = nb_max_waiting;
236 thread_pool->nb_actives = 0;
237 thread_pool->discard = NULL;
238
239 thread_pool->thread_list = (THREAD_POOL_NODE**)malloc(nbmaxthr * sizeof(THREAD_POOL_NODE*));
240 if (!thread_pool->thread_list) {
242 return NULL;
243 }
244
246 if (!thread_pool->waiting_list) {
247 n_log(LOG_ERR, "Unable to initialize wait list");
248 Free(thread_pool->thread_list);
250 return NULL;
251 }
252
253 pthread_mutex_init(&thread_pool->lock, NULL);
254
255 if (sem_init(&thread_pool->nb_tasks, 0, 1) == -1) {
256 int error = errno;
257 n_log(LOG_ERR, "sem_init failed : %s on &thread_pool -> nb_tasks", strerror(error));
258 list_destroy(&thread_pool->waiting_list);
259 pthread_mutex_destroy(&thread_pool->lock);
260 Free(thread_pool->thread_list);
262 return NULL;
263 }
264
265 size_t it = 0;
266 for (it = 0; it < nbmaxthr; it++) {
267 Malloc(thread_pool->thread_list[it], THREAD_POOL_NODE, 1);
268 thread_pool->thread_list[it]->type = -1;
269 thread_pool->thread_list[it]->state = IDLE_PROC;
270 thread_pool->thread_list[it]->thread_state = RUNNING_THREAD;
271 thread_pool->thread_list[it]->thread_pool = thread_pool;
272
273 if (sem_init(&thread_pool->thread_list[it]->th_start, 0, 0) == -1) {
274 int error = errno;
275 n_log(LOG_ERR, "sem_init failed : %s on &thread_pool -> thread_list[ %zu ] -> th_start", strerror(error), it);
276 Free(thread_pool->thread_list[it]);
277 goto cleanup_error;
278 }
279 if (sem_init(&thread_pool->thread_list[it]->th_end, 0, 0) == -1) {
280 int error = errno;
281 n_log(LOG_ERR, "sem_init failed : %s on &thread_pool -> thread_list[ %zu] -> th_end", strerror(error), it);
282 sem_destroy(&thread_pool->thread_list[it]->th_start);
283 Free(thread_pool->thread_list[it]);
284 goto cleanup_error;
285 }
286
287 thread_pool->thread_list[it]->func = NULL;
288 thread_pool->thread_list[it]->param = NULL;
289
290 pthread_mutex_init(&thread_pool->thread_list[it]->lock, NULL);
291
292 if (pthread_create(&thread_pool->thread_list[it]->thr, NULL, thread_pool_processing_function, (void*)thread_pool->thread_list[it]) != 0) {
293 n_log(LOG_ERR, "pthread_create failed : %s for it %zu", strerror(errno), it);
294 pthread_mutex_destroy(&thread_pool->thread_list[it]->lock);
295 sem_destroy(&thread_pool->thread_list[it]->th_start);
296 sem_destroy(&thread_pool->thread_list[it]->th_end);
297 Free(thread_pool->thread_list[it]);
298 goto cleanup_error;
299 }
300 }
301 return thread_pool;
302
303cleanup_error:
304 for (size_t j = 0; j < it; j++) {
305 pthread_mutex_lock(&thread_pool->thread_list[j]->lock);
306 thread_pool->thread_list[j]->thread_state = EXITING_THREAD;
307 sem_post(&thread_pool->thread_list[j]->th_start);
308 pthread_mutex_unlock(&thread_pool->thread_list[j]->lock);
309 pthread_join(thread_pool->thread_list[j]->thr, NULL);
310 pthread_mutex_destroy(&thread_pool->thread_list[j]->lock);
311 sem_destroy(&thread_pool->thread_list[j]->th_start);
312 sem_destroy(&thread_pool->thread_list[j]->th_end);
313 Free(thread_pool->thread_list[j]);
314 }
315 list_destroy(&thread_pool->waiting_list);
316 sem_destroy(&thread_pool->nb_tasks);
317 pthread_mutex_destroy(&thread_pool->lock);
318 Free(thread_pool->thread_list);
320 return NULL;
321} /* new_thread_pool */
322
331int add_threaded_process(THREAD_POOL* thread_pool, void* (*func_ptr)(void* param), void* param, int mode) {
332 if (!thread_pool) {
333 n_log(LOG_ERR, "thread_pool is not allocated, can't add processes to it !");
334 return FALSE;
335 }
336
337 if (!thread_pool->thread_list) {
338 n_log(LOG_ERR, "thread_pool thread_list is not allocated, can't add processes to it !");
339 return FALSE;
340 }
341
342 /* validate that exactly one of NORMAL_PROC, SYNCED_PROC, DIRECT_PROC is set */
343 int proc_mode = mode & (NORMAL_PROC | SYNCED_PROC | DIRECT_PROC);
344 if (proc_mode != NORMAL_PROC && proc_mode != SYNCED_PROC && proc_mode != DIRECT_PROC) {
345 n_log(LOG_ERR, "invalid mode %d: exactly one of NORMAL_PROC, SYNCED_PROC, DIRECT_PROC must be set", mode);
346 return FALSE;
347 }
348
349 if (!(mode & NO_LOCK)) pthread_mutex_lock(&thread_pool->lock);
350
351 size_t it = 0;
352 while (it < thread_pool->max_threads) {
353 pthread_mutex_lock(&thread_pool->thread_list[it]->lock);
354 if (thread_pool->thread_list[it]->thread_state == RUNNING_THREAD && thread_pool->thread_list[it]->state == IDLE_PROC) {
355 break;
356 }
357 pthread_mutex_unlock(&thread_pool->thread_list[it]->lock);
358 it++;
359 }
360 // we have a free thread slot, and the lock on it
361 if (it < thread_pool->max_threads) {
363 thread_pool->thread_list[it]->func = func_ptr;
364 thread_pool->thread_list[it]->param = param;
365 thread_pool->thread_list[it]->state = WAITING_PROC;
366 thread_pool->thread_list[it]->type = mode;
367 } else {
368 n_log(LOG_ERR, "unknown mode %d for thread %zu", mode, it);
369 pthread_mutex_unlock(&thread_pool->thread_list[it]->lock);
370 if (!(mode & NO_LOCK))
371 pthread_mutex_unlock(&thread_pool->lock);
372 return FALSE;
373 }
374 if (mode & NORMAL_PROC || mode & DIRECT_PROC)
375 sem_post(&thread_pool->thread_list[it]->th_start);
376 pthread_mutex_unlock(&thread_pool->thread_list[it]->lock);
377 n_log(LOG_DEBUG, "proc %p(%p) added on thread %zu", func_ptr, param, it);
378 } else {
379 // all thread are occupied -> test waiting lists. not holding thread_list[ it ] lock because it was obligatory unlocked before
380
381 // if already coming from queue, or if it should be part of a synced start, do not re-add && return FALSE
382 // it's only an error if SYNCED_PROC mode
383 int cancel_and_return = FALSE;
384 if (mode & NO_QUEUE) {
385 n_log(LOG_DEBUG, "Thread pool active threads are all busy and mode is NO_QUEUE, cannot add %p(%p) to pool %p", func_ptr, param, thread_pool);
386 cancel_and_return = TRUE;
387 } else if (mode & SYNCED_PROC) {
388 n_log(LOG_ERR, "Thread pool active threads are all busy, cannot add SYNCED_PROC %p(%p) to pool %p", func_ptr, param, thread_pool);
389 cancel_and_return = TRUE;
390 } else if (mode & DIRECT_PROC) {
391 n_log(LOG_ERR, "Thread pool active threads are all busy, cannot add DIRECT_PROC %p(%p) to pool %p", func_ptr, param, thread_pool);
392 cancel_and_return = TRUE;
393 }
394
395 if (cancel_and_return) {
396 if (!(mode & NO_LOCK))
397 pthread_mutex_unlock(&thread_pool->lock);
398 return FALSE;
399 }
400
401 // try adding to wait list
402 if (thread_pool->nb_max_waiting == 0 || (thread_pool->waiting_list->nb_items < thread_pool->nb_max_waiting)) {
403 THREAD_WAITING_PROC* proc = NULL;
404 Malloc(proc, THREAD_WAITING_PROC, 1);
405 if (!proc) {
406 n_log(LOG_ERR, "Failed to allocate THREAD_WAITING_PROC");
407 if (!(mode & NO_LOCK)) pthread_mutex_unlock(&thread_pool->lock);
408 return FALSE;
409 }
410 proc->func = func_ptr;
411 proc->param = param;
412 list_push(thread_pool->waiting_list, proc, free);
413 n_log(LOG_DEBUG, "Adding %p %p to waitlist", proc->func, proc->param);
414 } else {
415 n_log(LOG_ERR, "proc %p(%p) was dropped from waitlist because waitlist of thread pool %p is full", func_ptr, param, thread_pool);
416 if (!(mode & NO_LOCK)) pthread_mutex_unlock(&thread_pool->lock);
417 return FALSE;
418 }
419 }
420
421 /* consume idle signal: pool now has work in progress */
422 if (sem_trywait(&thread_pool->nb_tasks) != 0 && errno != EAGAIN) {
423 int error = errno;
424 n_log(LOG_ERR, "sem_trywait nb_tasks error in thread_pool %p: %s", thread_pool, strerror(error));
425 }
426
427 if (!(mode & NO_LOCK)) pthread_mutex_unlock(&thread_pool->lock);
428
429 return TRUE;
430} /* add_threaded_process */
431
438 if (!thread_pool)
439 return FALSE;
440
441 if (!thread_pool->thread_list)
442 return FALSE;
443
444 int retval = TRUE;
445
446 pthread_mutex_lock(&thread_pool->lock);
447 for (size_t it = 0; it < thread_pool->max_threads; it++) {
448 int to_run = 0;
449 pthread_mutex_lock(&thread_pool->thread_list[it]->lock);
450 if ((thread_pool->thread_list[it]->type & SYNCED_PROC) && thread_pool->thread_list[it]->state == WAITING_PROC) {
451 to_run = 1;
452 }
453 pthread_mutex_unlock(&thread_pool->thread_list[it]->lock);
454 if (to_run == 1) {
455 if (sem_post(&thread_pool->thread_list[it]->th_start) != 0) {
456 int error = errno;
457 n_log(LOG_ERR, "sem_post th_start error in thread_pool %p , thread_list[ %zu ] : %s", thread_pool, it, strerror(error));
458 retval = FALSE;
459 }
460 }
461 }
462 pthread_mutex_unlock(&thread_pool->lock);
463
464 return retval;
465} /* start_threaded_pool */
466
473 __n_assert(thread_pool, return FALSE);
474 __n_assert(thread_pool->thread_list, return FALSE);
475
476 int retval = TRUE;
477 for (size_t it = 0; it < thread_pool->max_threads; it++) {
478 int is_synced = 0;
479 pthread_mutex_lock(&thread_pool->thread_list[it]->lock);
480 if (thread_pool->thread_list[it]->type & SYNCED_PROC) {
481 is_synced = 1;
482 }
483 pthread_mutex_unlock(&thread_pool->thread_list[it]->lock);
484
485 if (is_synced) {
486 if (sem_wait(&thread_pool->thread_list[it]->th_end) == -1) {
487 int error = errno;
488 n_log(LOG_ERR, "sem_wait th_end error in thread_pool %p , thread_list[ %zu ] : %s", thread_pool, it, strerror(error));
489 retval = FALSE;
490 }
491 }
492 }
493 return retval;
494} /* wait_for_synced_threaded_pool */
495
502 __n_assert(thread_pool, return FALSE);
503 __n_assert(thread_pool->thread_list, return FALSE);
504
505 /* Poll refresh_thread_pool instead of blocking on nb_tasks.
506 *
507 * The idle token is consumed by add_threaded_process and posted back ONLY by
508 * refresh_thread_pool; a worker finishing its proc does not post it. Refreshing
509 * once and then blocking therefore deadlocks whenever the pool is still busy at
510 * that moment: refresh sees a RUNNING_PROC thread and posts nothing, the wait
511 * blocks, the worker goes idle, and nobody ever signals again. It only appeared
512 * to work when the proc happened to finish before that single refresh, so the
513 * hang looked timing-dependent and grew more likely the longer the work took.
514 *
515 * Polling keeps every idleness decision inside refresh_thread_pool (which
516 * evaluates it under the pool lock) and cannot miss a wakeup. */
517 for (;;) {
519 if (sem_trywait(&thread_pool->nb_tasks) == 0) {
520 /* restore the idle signal so it can be waited on again or checked */
521 sem_post(&thread_pool->nb_tasks);
522 return TRUE;
523 }
524 if (errno != EAGAIN && errno != EINTR) {
525 int error = errno;
526 n_log(LOG_ERR, "sem_trywait nb_tasks error in thread_pool %p: %s", thread_pool, strerror(error));
527 return FALSE;
528 }
530 }
531} /* wait_for_threaded_pool */
532
549int set_thread_pool_discard(THREAD_POOL* thread_pool, void (*discard)(void* param)) {
550 __n_assert(thread_pool, return FALSE);
551 pthread_mutex_lock(&thread_pool->lock);
552 thread_pool->discard = discard;
553 pthread_mutex_unlock(&thread_pool->lock);
554 return TRUE;
555}
556
573int destroy_threaded_pool(THREAD_POOL** pool, unsigned int delay) {
574 __n_assert(pool && (*pool), return FALSE);
575 __n_assert((*pool)->thread_list, return FALSE);
576
577 int DONE = 0;
578 int max_retries = 1000;
579
580 while (!DONE) {
581 DONE = 1;
582 pthread_mutex_lock(&(*pool)->lock);
583 for (size_t it = 0; it < (*pool)->max_threads; it++) {
584 pthread_mutex_lock(&(*pool)->thread_list[it]->lock);
585 int state = (*pool)->thread_list[it]->state;
586 int thread_state = (*pool)->thread_list[it]->thread_state;
587 pthread_mutex_unlock(&(*pool)->thread_list[it]->lock);
588
589 if (thread_state == EXITING_THREAD || thread_state == EXITED_THREAD) {
590 continue;
591 }
592
593 if (state == IDLE_PROC) {
594 pthread_mutex_lock(&(*pool)->thread_list[it]->lock);
595 (*pool)->thread_list[it]->thread_state = EXITING_THREAD;
596 sem_post(&(*pool)->thread_list[it]->th_start);
597 pthread_mutex_unlock(&(*pool)->thread_list[it]->lock);
598 } else {
599 DONE = 0;
600 }
601 }
602 pthread_mutex_unlock(&(*pool)->lock);
603
604 if (!DONE) {
605 max_retries--;
606 if (max_retries <= 0) {
607 pthread_mutex_lock(&(*pool)->lock);
608 for (size_t it = 0; it < (*pool)->max_threads; it++) {
609 pthread_mutex_lock(&(*pool)->thread_list[it]->lock);
610 if ((*pool)->thread_list[it]->thread_state != EXITING_THREAD && (*pool)->thread_list[it]->thread_state != EXITED_THREAD) {
611 (*pool)->thread_list[it]->thread_state = EXITING_THREAD;
612 sem_post(&(*pool)->thread_list[it]->th_start);
613 }
614 pthread_mutex_unlock(&(*pool)->thread_list[it]->lock);
615 }
616 pthread_mutex_unlock(&(*pool)->lock);
617 break;
618 }
619 }
620
621 u_sleep(delay);
622 }
623
624 /* join threads without holding pool lock to avoid deadlock:
625 * threads may call refresh_thread_pool() which needs pool->lock */
626 for (size_t it = 0; it < (*pool)->max_threads; it++) {
627 pthread_join((*pool)->thread_list[it]->thr, NULL);
628 }
629
630 /* all threads have exited, safe to clean up without locking */
631 for (size_t it = 0; it < (*pool)->max_threads; it++) {
632 pthread_mutex_destroy(&(*pool)->thread_list[it]->lock);
633 sem_destroy(&(*pool)->thread_list[it]->th_start);
634 sem_destroy(&(*pool)->thread_list[it]->th_end);
635 Free((*pool)->thread_list[it]);
636 }
637 Free((*pool)->thread_list);
638
639 /* Whatever is still queued was never run, because no thread is left to
640 * take it. Said out loud rather than dropped with the list, and handed to
641 * the caller's discard function when there is one: the list's own
642 * destructor frees the wrapper and knows nothing about the param. */
643 if ((*pool)->waiting_list->nb_items > 0) {
644 n_log(LOG_ERR, "thread pool %p destroyed with %zu queued job(s) never run%s", (void*)(*pool),
645 (*pool)->waiting_list->nb_items,
646 (*pool)->discard ? ", handed to its discard function" : ", their params not released");
647 if ((*pool)->discard) {
648 list_foreach(node, (*pool)->waiting_list) {
649 THREAD_WAITING_PROC* proc = (THREAD_WAITING_PROC*)node->ptr;
650 if (proc) (*pool)->discard(proc->param);
651 }
652 }
653 }
654 list_destroy(&(*pool)->waiting_list);
655
656 sem_destroy(&(*pool)->nb_tasks);
657
658 pthread_mutex_destroy(&(*pool)->lock);
659
660 Free((*pool));
661
662 return TRUE;
663} /* destroy_threaded_pool */
664
671 __n_assert(thread_pool, return FALSE);
672 __n_assert(thread_pool->waiting_list, return FALSE);
673
674 /* Trying to empty the wait list */
675 int push_status = 0;
676 pthread_mutex_lock(&thread_pool->lock);
677 if (thread_pool->waiting_list && thread_pool->waiting_list->start)
678 push_status = 1;
679 while (push_status == 1) {
680 LIST_NODE* node = thread_pool->waiting_list->start;
681 if (node && node->ptr) {
683 if (proc) { // cppcheck-suppress knownConditionTrueFalse ; defensive check after cast
684 if (add_threaded_process(thread_pool, proc->func, proc->param, NORMAL_PROC | NO_QUEUE | NO_LOCK) == TRUE) {
685 THREAD_WAITING_PROC* procptr = NULL;
686 LIST_NODE* next_node = node->next;
687 procptr = remove_list_node(thread_pool->waiting_list, node, THREAD_WAITING_PROC);
688 n_log(LOG_DEBUG, "waitlist: adding %p,%p to %p", procptr->func, procptr->param, thread_pool);
689 Free(procptr);
690 (void)next_node; /* advance past removed node (consumed by next iteration) */
691 } else {
692 n_log(LOG_DEBUG, "waitlist: cannot add proc %p from waiting list of %p, all active threads are busy !", proc, thread_pool);
693 push_status = 0;
694 }
695 } else {
696 n_log(LOG_ERR, "waitlist: trying to add invalid NULL proc on thread pool %p !", thread_pool);
697 push_status = 0;
698 }
699 } else {
700 push_status = 0;
701 }
702 } // while( push_status == 1 )
703
704 // update statistics: count threads with assigned work (waiting to run or running)
705 thread_pool->nb_actives = 0;
706 for (size_t it = 0; it < thread_pool->max_threads; it++) {
707 pthread_mutex_lock(&thread_pool->thread_list[it]->lock);
708 if (thread_pool->thread_list[it]->state == RUNNING_PROC || thread_pool->thread_list[it]->state == WAITING_PROC)
709 thread_pool->nb_actives++;
710 pthread_mutex_unlock(&thread_pool->thread_list[it]->lock);
711 }
712
713 /* signal idle: no active/waiting threads and waiting list is empty */
714 if (thread_pool->nb_actives == 0 && thread_pool->waiting_list && thread_pool->waiting_list->nb_items == 0) {
715 /* Clamp the idle token to at most one WITHOUT consulting sem_getvalue.
716 * POSIX leaves sem_getvalue's result unspecified while a thread is blocked
717 * in sem_wait: an implementation may report zero (glibc) or a negative
718 * waiter count (winpthreads does). The old guard posted only when it read
719 * exactly 0, so on Windows a waiter blocked in wait_for_threaded_pool made
720 * the read non-zero, the post was skipped, and that waiter was stranded for
721 * good. Consuming any token already present and then posting exactly one is
722 * idempotent and does not depend on that value: with a waiter present the
723 * trywait simply fails and the post releases it. */
724 if (sem_trywait(&thread_pool->nb_tasks) != 0 && errno != EAGAIN) {
725 int error = errno;
726 n_log(LOG_ERR, "sem_trywait nb_tasks error in thread_pool %p: %s", thread_pool, strerror(error));
727 }
728 sem_post(&thread_pool->nb_tasks);
729 }
730
731 pthread_mutex_unlock(&thread_pool->lock);
732
733 return TRUE;
734} // refresh_thread_pool()
THREAD_POOL * thread_pool
Definition ex_fluid.c:76
int DONE
Definition ex_fluid.c:58
static int mode
static NETWORK_POOL * pool
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:204
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
void * ptr
void pointer to store
Definition n_list.h:46
struct LIST_NODE * next
pointer to the next node
Definition n_list.h:52
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:228
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
Definition n_list.h:89
#define remove_list_node(__LIST_, __NODE_, __TYPE_)
Remove macro helper for void pointer casting.
Definition n_list.h:98
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:548
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:37
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Definition n_list.h:75
Structure of a generic list node.
Definition n_list.h:44
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_DEBUG
debug-level messages
Definition n_log.h:108
#define LOG_ERR
error conditions
Definition n_log.h:100
void u_sleep(unsigned int usec)
wrapper around usleep for API consistency
Definition n_time.c:55
int state
state of the proc , RUNNING_PROC when it is busy processing func( param) , IDLE_PROC when it waits fo...
int type
SYNCED or DIRECT process start.
void * param
if not NULL , passed as argument
void *(* func)(void *param)
function to call in the thread
sem_t th_end
thread ending semaphore
pthread_mutex_t lock
mutex to prevent mutual access of node parameters
struct THREAD_POOL * thread_pool
pointer to assigned thread pool
sem_t th_start
thread starting semaphore
pthread_t thr
thread id
void *(* func)(void *param)
function to call in the thread
int thread_state
state of the managing thread , RUNNING_THREAD, EXITING_THREAD, EXITED_THREAD
void * param
if not NULL , passed as argument
#define NORMAL_PROC
processing mode for added func, synced start, can be queued
int start_threaded_pool(THREAD_POOL *thread_pool)
Launch the process waiting for execution in the thread pool.
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
#define EXITED_THREAD
indicate that the pool is off, all jobs have been consumed
#define SYNCED_PROC
processing mode for added func, synced start, not queued
int n_parallel_for(size_t begin, size_t end, size_t grain, N_PARALLEL_FN fn, void *user)
Run a loop over a range on several cores, and return when all of it is done.
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
#define NO_QUEUE
special processing mode for waiting_list: do not add the work in queue since it' coming from the queu...
int refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
#define IDLE_PROC
status of a thread which is waiting for some proc
int wait_for_threaded_pool(THREAD_POOL *thread_pool)
Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without pol...
void(* N_PARALLEL_FN)(size_t begin, size_t end, void *user)
work done by n_parallel_for() on one slice [begin, end) of a range
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
Stop every thread of a pool and free it.
int wait_for_synced_threaded_pool(THREAD_POOL *thread_pool)
wait for all the launched process, blocking but light on the CPU as there is no polling
long int get_nb_cpu_cores()
get number of core of current system
#define DIRECT_PROC
processing mode for added func, direct start, not queued
#define RUNNING_PROC
status of a thread which proc is currently running
int set_thread_pool_discard(THREAD_POOL *thread_pool, void(*discard)(void *param))
Set what destroy_threaded_pool() does with the param of a queued job it discards.
#define NO_LOCK
if passed to add_threaded_process, skip main table lock in case we are in a func which is already loc...
#define EXITING_THREAD
indicate that the pool is exiting, unfinished jobs will finish and the pool will exit the threads and...
#define RUNNING_THREAD
indicate that the pool is running and ready to use
#define WAITING_PROC
status of a thread who have proc waiting to be processed
#define N_THREAD_POOL_WAIT_POLL_US
A thread pool node.
Structure of a thread pool.
Structure of a waiting process item.
Common headers and low-level functions & define.
Generic log system.
void * user
passed to the work
static pthread_mutex_t parallel_lock
Guards parallel_budget.
size_t end
one past its last
size_t begin
first index of the slice
static long parallel_budget
get number of core of current system
static void * n_parallel_slice(void *arg)
Thread entry of one slice of a parallel loop.
void * thread_pool_processing_function(void *param)
Internal thread pool processing function.
N_PARALLEL_FN fn
the work
One slice of a parallel loop, as handed to its thread.
Thread pool declaration.
Timing utilities.