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 #ifndef UIC_THREADPOOL_H
30 #define UIC_THREADPOOL_H
31
32 #include "../ui/toolkit.h"
33
34 #ifndef _WIN32
35 #include <pthread.h>
36 #endif
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 typedef struct UiQueueElm UiQueueElm;
43 typedef struct UiQueue UiQueue;
44
45 typedef struct UiJob {
46 UiObject *obj;
47 ui_threadfunc job_func;
48 void *job_data;
49 ui_callback finish_callback;
50 void *finish_data;
51 } UiJob;
52
53 struct UiQueueElm {
54 void *data;
55 UiQueueElm *next;
56 };
57
58 struct UiQueue {
59 UiQueueElm *elements;
60 size_t length;
61 pthread_mutex_t lock;
62 pthread_mutex_t avlbl_lock;
63 pthread_cond_t available;
64 };
65
66 typedef struct _threadpool_job threadpool_job;
67 typedef void*(*job_callback_f)(
void *data);
68
69 typedef struct _pool_queue
pool_queue_t;
70 struct UiThreadpool {
71 UiQueue *queue;
72 uint32_t num_idle;
73 int min_threads;
74 int max_threads;
75 pthread_t *threads;
76 int nthreads;
77 };
78
79 struct _threadpool_job {
80 job_callback_f callback;
81 void *data;
82 };
83
84 struct _pool_queue {
85 threadpool_job *job;
86 pool_queue_t *next;
87 };
88
89 UiThreadpool* threadpool_new(
int min,
int max);
90 int threadpool_start(UiThreadpool *pool);
91 int threadpool_join(UiThreadpool *pool);
92 void* threadpool_func(
void *data);
93 threadpool_job* threadpool_get_job(UiThreadpool *pool);
94 void threadpool_run(UiThreadpool *pool, job_callback_f func,
void *data);
95
96 UiQueue* ui_queue_create(
void);
97 void ui_queue_free(UiQueue *queue);
98 void ui_queue_put(UiQueue *queue,
void *data);
99 void* ui_queue_get_wait(UiQueue *queue);
100
101 #ifdef __cplusplus
102 }
103 #endif
104
105 #endif
106
107