| 28 |
28 |
| 29 #ifndef _WIN32 |
29 #ifndef _WIN32 |
| 30 |
30 |
| 31 #include "threadpool.h" |
31 #include "threadpool.h" |
| 32 #include "context.h" |
32 #include "context.h" |
| |
33 #include <cx/linked_list.h> |
| 33 |
34 |
| 34 #include <pthread.h> |
35 #include <pthread.h> |
| 35 #include <stdio.h> |
36 #include <stdio.h> |
| 36 #include <string.h> |
37 #include <string.h> |
| 37 #include <errno.h> |
38 #include <errno.h> |
| 38 |
39 |
| 39 static threadpool_job kill_job; |
40 static threadpool_job kill_job; |
| |
41 |
| |
42 |
| |
43 |
| |
44 static pthread_mutex_t mc_buffer_mutex; |
| |
45 static CxList *mainthread_call_buffer; |
| |
46 static volatile int mainthread_call_buffered = 0; |
| |
47 |
| |
48 typedef struct UiMainCall { |
| |
49 ui_threadfunc func; |
| |
50 void *data; |
| |
51 } UiMainCall; |
| |
52 |
| |
53 void uic_init_threads(void) { |
| |
54 pthread_mutex_init(&mc_buffer_mutex, NULL); |
| |
55 mainthread_call_buffer = cxLinkedListCreate(NULL, sizeof(UiMainCall)); |
| |
56 } |
| |
57 |
| |
58 int uic_mainthread_calls_is_buffered(void) { |
| |
59 return mainthread_call_buffered; |
| |
60 } |
| |
61 |
| |
62 void uic_add_buffered_mainthread_call(ui_threadfunc func, void *data) { |
| |
63 pthread_mutex_lock(&mc_buffer_mutex); |
| |
64 UiMainCall call; |
| |
65 call.func = func; |
| |
66 call.data = data; |
| |
67 cxListAdd(mainthread_call_buffer, &call); |
| |
68 pthread_mutex_unlock(&mc_buffer_mutex); |
| |
69 } |
| |
70 |
| |
71 |
| |
72 void ui_buffer_mainthread_calls(UiBool enable_buffering) { |
| |
73 mainthread_call_buffered = enable_buffering; |
| |
74 if(!enable_buffering) { |
| |
75 ui_exec_buffered_mainthread_calls(); |
| |
76 } |
| |
77 } |
| |
78 void ui_exec_buffered_mainthread_calls(void) { |
| |
79 pthread_mutex_lock(&mc_buffer_mutex); |
| |
80 CxIterator i = cxListIterator(mainthread_call_buffer); |
| |
81 cx_foreach(UiMainCall *, call, i) { |
| |
82 if(call->func) { |
| |
83 call->func(call->data); |
| |
84 } |
| |
85 } |
| |
86 cxListClear(mainthread_call_buffer); |
| |
87 pthread_mutex_unlock(&mc_buffer_mutex); |
| |
88 } |
| |
89 |
| |
90 |
| 40 |
91 |
| 41 UiThreadpool* threadpool_new(int min, int max) { |
92 UiThreadpool* threadpool_new(int min, int max) { |
| 42 UiThreadpool *pool = malloc(sizeof(UiThreadpool)); |
93 UiThreadpool *pool = malloc(sizeof(UiThreadpool)); |
| 43 pool->queue = ui_queue_create(); |
94 pool->queue = ui_queue_create(); |
| 44 pool->num_idle = 0; |
95 pool->num_idle = 0; |