ui/common/threadpool.h

changeset 115
e57ca2747782
parent 100
d2bd73d28ff1
equal deleted inserted replaced
114:3da24640513a 115:e57ca2747782
37 37
38 #ifdef __cplusplus 38 #ifdef __cplusplus
39 extern "C" { 39 extern "C" {
40 #endif 40 #endif
41 41
42 typedef struct UiQueueElm UiQueueElm;
43 typedef struct UiQueue UiQueue;
42 44
43 typedef struct UiJob { 45 typedef struct UiJob {
44 UiObject *obj; 46 UiObject *obj;
45 ui_threadfunc job_func; 47 ui_threadfunc job_func;
46 void *job_data; 48 void *job_data;
47 ui_callback finish_callback; 49 ui_callback finish_callback;
48 void *finish_data; 50 void *finish_data;
49 } UiJob; 51 } UiJob;
50 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 };
51 65
52 typedef struct _threadpool_job threadpool_job; 66 typedef struct _threadpool_job threadpool_job;
53 typedef void*(*job_callback_f)(void *data); 67 typedef void*(*job_callback_f)(void *data);
54 68
55 typedef struct _pool_queue pool_queue_t; 69 typedef struct _pool_queue pool_queue_t;
56 struct UiThreadpool { 70 struct UiThreadpool {
57 pthread_mutex_t queue_lock; 71 UiQueue *queue;
58 pthread_mutex_t avlbl_lock; 72 uint32_t num_idle;
59 pthread_cond_t available; 73 int min_threads;
60 pool_queue_t *queue; 74 int max_threads;
61 uint32_t queue_len; 75 pthread_t *threads;
62 uint32_t num_idle; 76 int nthreads;
63 int min_threads;
64 int max_threads;
65 }; 77 };
66 78
67 struct _threadpool_job { 79 struct _threadpool_job {
68 job_callback_f callback; 80 job_callback_f callback;
69 void *data; 81 void *data;
74 pool_queue_t *next; 86 pool_queue_t *next;
75 }; 87 };
76 88
77 UiThreadpool* threadpool_new(int min, int max); 89 UiThreadpool* threadpool_new(int min, int max);
78 int threadpool_start(UiThreadpool *pool); 90 int threadpool_start(UiThreadpool *pool);
91 int threadpool_join(UiThreadpool *pool);
79 void* threadpool_func(void *data); 92 void* threadpool_func(void *data);
80 threadpool_job* threadpool_get_job(UiThreadpool *pool); 93 threadpool_job* threadpool_get_job(UiThreadpool *pool);
81 void threadpool_run(UiThreadpool *pool, job_callback_f func, void *data); 94 void threadpool_run(UiThreadpool *pool, job_callback_f func, void *data);
82 void threadpool_enqueue_job(UiThreadpool *pool, threadpool_job *job); 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);
83 100
84 #ifdef __cplusplus 101 #ifdef __cplusplus
85 } 102 }
86 #endif 103 #endif
87 104

mercurial