summaryrefslogtreecommitdiff
path: root/src/ot_workqueue.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ot_workqueue.c')
-rw-r--r--src/ot_workqueue.c248
1 files changed, 248 insertions, 0 deletions
diff --git a/src/ot_workqueue.c b/src/ot_workqueue.c
new file mode 100644
index 0000000..cdefddc
--- /dev/null
+++ b/src/ot_workqueue.c
@@ -0,0 +1,248 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <pthread.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/uio.h>
9#include <unistd.h>
10
11/* Opentracker */
12#include "ot_fdstate.h"
13#include "ot_iovec.h"
14#include "ot_workqueue.h"
15#include "trackerlogic.h"
16
17/* Pushing a result wakes the thread that will send it */
18extern int g_self_pipe[2];
19
20struct ot_task {
21 ot_taskid taskid;
22 ot_tasktype tasktype;
23 int64_t sock;
24 uint32_t generation;
25 size_t iovec_entries;
26 struct iovec *iovec;
27 struct ot_task *next;
28};
29
30static ot_taskid next_free_taskid = 1;
31static struct ot_task *tasklist;
32static pthread_mutex_t tasklist_mutex;
33static pthread_cond_t tasklist_being_filled;
34
35int workqueue_pushtask(int64_t sock, ot_tasktype tasktype) {
36 struct ot_task **tmptask, *task;
37
38 task = malloc(sizeof(struct ot_task));
39 if (!task)
40 return -1;
41
42 task->taskid = 0;
43 task->tasktype = tasktype;
44 task->sock = sock;
45 task->generation = fdstate_generation((int)sock);
46 task->iovec_entries = 0;
47 task->iovec = NULL;
48 task->next = 0;
49
50 /* Want exclusive access to tasklist */
51 pthread_mutex_lock(&tasklist_mutex);
52
53 /* Skip to end of list */
54 tmptask = &tasklist;
55 while (*tmptask)
56 tmptask = &(*tmptask)->next;
57 *tmptask = task;
58
59 /* Inform waiting workers and release lock */
60 pthread_cond_broadcast(&tasklist_being_filled);
61 pthread_mutex_unlock(&tasklist_mutex);
62 return 0;
63}
64
65void workqueue_canceltask(int64_t sock) {
66 struct ot_task **task;
67
68 /* Want exclusive access to tasklist */
69 pthread_mutex_lock(&tasklist_mutex);
70
71 task = &tasklist;
72 while (*task) {
73 struct ot_task *ptask = *task;
74 size_t i;
75
76 if (ptask->sock != sock) {
77 task = &ptask->next;
78 continue;
79 }
80
81 /* Free task's iovec, the buffers it points at and the vector itself */
82 for (i = 0; i < ptask->iovec_entries; ++i)
83 free(ptask->iovec[i].iov_base);
84 free(ptask->iovec);
85
86 *task = ptask->next;
87 free(ptask);
88 }
89
90 /* Release lock */
91 pthread_mutex_unlock(&tasklist_mutex);
92}
93
94ot_taskid workqueue_poptask(ot_tasktype *tasktype) {
95 struct ot_task *task;
96 ot_taskid taskid = 0;
97
98 /* Want exclusive access to tasklist */
99 pthread_mutex_lock(&tasklist_mutex);
100
101 while (!taskid) {
102 /* Skip to the first unassigned task this worker wants to do */
103 for (task = tasklist; task; task = task->next)
104 if (!task->taskid && (TASK_CLASS_MASK & task->tasktype) == *tasktype) {
105 /* If we found an outstanding task, assign a taskid to it
106 and leave the loop */
107 task->taskid = taskid = ++next_free_taskid;
108 *tasktype = task->tasktype;
109 break;
110 }
111
112 /* Wait until the next task is being fed, unless the tracker is on its
113 way out, in which case the worker is meant to leave rather than wait */
114 if (!taskid) {
115 if (!g_opentracker_running)
116 break;
117 pthread_cond_wait(&tasklist_being_filled, &tasklist_mutex);
118 }
119 }
120
121 /* Release lock */
122 pthread_mutex_unlock(&tasklist_mutex);
123
124 return taskid;
125}
126
127void workqueue_pushsuccess(ot_taskid taskid) {
128 struct ot_task **task;
129
130 /* Want exclusive access to tasklist */
131 pthread_mutex_lock(&tasklist_mutex);
132
133 for (task = &tasklist; *task; task = &((*task)->next))
134 if ((*task)->taskid == taskid) {
135 struct ot_task *ptask = *task;
136 *task = (*task)->next;
137 free(ptask);
138 break;
139 }
140
141 /* Release lock */
142 pthread_mutex_unlock(&tasklist_mutex);
143}
144
145int workqueue_pushresult(ot_taskid taskid, size_t iovec_entries, struct iovec *iovec) {
146 struct ot_task *task;
147 const char byte = 'o';
148
149 /* Want exclusive access to tasklist */
150 pthread_mutex_lock(&tasklist_mutex);
151
152 for (task = tasklist; task; task = task->next)
153 if (task->taskid == taskid) {
154 task->iovec_entries = iovec_entries;
155 task->iovec = iovec;
156 task->tasktype = TASK_DONE;
157 break;
158 }
159
160 /* Release lock */
161 pthread_mutex_unlock(&tasklist_mutex);
162
163 /* A full pipe has already done this nudge's job, so a failure is fine */
164 (void)!write(g_self_pipe[1], &byte, 1);
165
166 /* Indicate whether the worker has to throw away results */
167 return task ? 0 : -1;
168}
169
170int workqueue_pushchunked(ot_taskid taskid, struct iovec *iovec) {
171 struct ot_task *task;
172 const char byte = 'o';
173
174 /* Want exclusive access to tasklist */
175 pthread_mutex_lock(&tasklist_mutex);
176
177 for (task = tasklist; task; task = task->next)
178 if (task->taskid == taskid) {
179 if (iovec) {
180 if (iovec_append(&task->iovec_entries, &task->iovec, iovec))
181 task->tasktype = TASK_DONE_PARTIAL;
182 else
183 task = NULL;
184 } else
185 task->tasktype = TASK_DONE;
186 break;
187 }
188
189 /* Release lock */
190 pthread_mutex_unlock(&tasklist_mutex);
191
192 /* A full pipe has already done this nudge's job, so a failure is fine */
193 (void)!write(g_self_pipe[1], &byte, 1);
194
195 /* Indicate whether the worker has to throw away results */
196 return task ? 0 : -1;
197}
198
199int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovec, int *is_partial, uint32_t *generation) {
200 struct ot_task **task;
201 int64_t sock = -1;
202
203 *is_partial = 0;
204
205 /* Want exclusive access to tasklist */
206 pthread_mutex_lock(&tasklist_mutex);
207
208 for (task = &tasklist; *task; task = &((*task)->next))
209 if (((*task)->tasktype & TASK_CLASS_MASK) == TASK_DONE) {
210 struct ot_task *ptask = *task;
211 *iovec_entries = ptask->iovec_entries;
212 *iovec = ptask->iovec;
213 sock = ptask->sock;
214 *generation = ptask->generation;
215
216 if ((*task)->tasktype == TASK_DONE) {
217 *task = ptask->next;
218 free(ptask);
219 } else {
220 ptask->iovec_entries = 0;
221 ptask->iovec = NULL;
222 *is_partial = 1;
223 /* Prevent task from showing up immediately again unless new data was added */
224 (*task)->tasktype = TASK_FULLSCRAPE;
225 }
226 break;
227 }
228
229 /* Release lock */
230 pthread_mutex_unlock(&tasklist_mutex);
231 return sock;
232}
233
234void workqueue_wakeall(void) {
235 pthread_mutex_lock(&tasklist_mutex);
236 pthread_cond_broadcast(&tasklist_being_filled);
237 pthread_mutex_unlock(&tasklist_mutex);
238}
239
240void workqueue_init(void) {
241 pthread_mutex_init(&tasklist_mutex, NULL);
242 pthread_cond_init(&tasklist_being_filled, NULL);
243}
244
245void workqueue_deinit(void) {
246 pthread_mutex_destroy(&tasklist_mutex);
247 pthread_cond_destroy(&tasklist_being_filled);
248}