-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool.h
41 lines (34 loc) · 1.1 KB
/
threadpool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <pthread.h>
#include "queue.h"
typedef struct tpool_handle tpool_handle;
typedef struct tpool_pool tpool_pool;
typedef void *(*tpool_work)(void *);
/**
* Initializes the pool size threads. 0 for default.
* Returns NULL on failure.
*/
tpool_pool *tpool_init(size_t size);
/**
* Closes pool by joining threads and freeing all allocated memory.
* Waits until all workers are waiting for tasks, then exits.
* May never return if tasks add other tasks.
* Behavior is unspecified if tasks are added while this is running.
*/
void tpool_close(tpool_pool *pool);
/**
* @brief Yields execution to the threadpool, enqueueing a resume task so that
* the threadpool can resume execution of the current task eventually.
*
* Assumes the calling thread is a threadpool thread.
*
* May return in different thread than the caller, but returns exactly once.
*/
void tpool_yield();
/**
* Gets the result of a future.
*/
void *tpool_task_await(tpool_handle *handle);
/**
* Enqueues a task with a task handle which can awaited.
*/
tpool_handle *tpool_task_enqueue(tpool_pool *pool, tpool_work work, void *arg);