- added libdrawtext
[demo_prior] / libs / drawtext / src / tpool.h
1 /* worker thread pool based on POSIX threads
2  * author: John Tsiombikas <nuclear@member.fsf.org>
3  * This code is public domain.
4  */
5 #ifndef THREADPOOL_H_
6 #define THREADPOOL_H_
7
8 struct dtx_thread_pool;
9
10 /* type of the function accepted as work or completion callback */
11 typedef void (*dtx_tpool_callback)(void*);
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /* if num_threads == 0, auto-detect how many threads to spawn */
18 struct dtx_thread_pool *dtx_tpool_create(int num_threads);
19 void dtx_tpool_destroy(struct dtx_thread_pool *tpool);
20
21 /* if begin_batch is called before an enqueue, the worker threads will not be
22  * signalled to start working until end_batch is called.
23  */
24 void dtx_tpool_begin_batch(struct dtx_thread_pool *tpool);
25 void dtx_tpool_end_batch(struct dtx_thread_pool *tpool);
26
27 /* if enqueue is called without calling begin_batch first, it will immediately
28  * wake up the worker threads to start working on the enqueued item
29  */
30 int dtx_tpool_enqueue(struct dtx_thread_pool *tpool, void *data,
31                 dtx_tpool_callback work_func, dtx_tpool_callback done_func);
32 /* clear the work queue. does not cancel any currently running jobs */
33 void dtx_tpool_clear(struct dtx_thread_pool *tpool);
34
35 /* returns the number of queued work items */
36 int dtx_tpool_queued_jobs(struct dtx_thread_pool *tpool);
37 /* returns the number of active (working) threads */
38 int dtx_tpool_active_jobs(struct dtx_thread_pool *tpool);
39 /* returns the number of pending jobs, both in queue and active */
40 int dtx_tpool_pending_jobs(struct dtx_thread_pool *tpool);
41
42 /* wait for all pending jobs to be completed */
43 void dtx_tpool_wait(struct dtx_thread_pool *tpool);
44 /* wait until the pending jobs are down to the target specified
45  * for example, to wait until a single job has been completed:
46  *   dtx_tpool_wait_pending(tpool, dtx_tpool_pending_jobs(tpool) - 1);
47  * this interface is slightly awkward to avoid race conditions. */
48 void dtx_tpool_wait_pending(struct dtx_thread_pool *tpool, int pending_target);
49 /* wait for all pending jobs to be completed for up to "timeout" milliseconds */
50 long dtx_tpool_timedwait(struct dtx_thread_pool *tpool, long timeout);
51
52 /* returns the number of processors on the system.
53  * individual cores in multi-core processors are counted as processors.
54  */
55 int dtx_tpool_num_processors(void);
56
57 #ifdef __cplusplus
58 }
59 #endif
60
61 #endif  /* THREADPOOL_H_ */