done
[fbgfx] / 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 thread_pool;
9
10 /* type of the function accepted as work or completion callback */
11 typedef void (*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 thread_pool *tpool_create(int num_threads);
19 void tpool_destroy(struct 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 tpool_begin_batch(struct thread_pool *tpool);
25 void tpool_end_batch(struct 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 tpool_enqueue(struct thread_pool *tpool, void *data,
31                 tpool_callback work_func, tpool_callback done_func);
32 /* clear the work queue. does not cancel any currently running jobs */
33 void tpool_clear(struct thread_pool *tpool);
34
35 /* returns the number of queued work items */
36 int tpool_queued_jobs(struct thread_pool *tpool);
37 /* returns the number of active (working) threads */
38 int tpool_active_jobs(struct thread_pool *tpool);
39 /* returns the number of pending jobs, both in queue and active */
40 int tpool_pending_jobs(struct thread_pool *tpool);
41
42 /* wait for all pending jobs to be completed */
43 void tpool_wait(struct 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  *   tpool_wait_pending(tpool, tpool_pending_jobs(tpool) - 1);
47  * this interface is slightly awkward to avoid race conditions. */
48 void tpool_wait_pending(struct thread_pool *tpool, int pending_target);
49 /* wait for all pending jobs to be completed for up to "timeout" milliseconds */
50 long tpool_timedwait(struct 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 tpool_num_processors(void);
56
57 #ifdef __cplusplus
58 }
59 #endif
60
61 #endif  /* THREADPOOL_H_ */