Debian Codegraph @

easel/esl_workqueue.c

raw symbols SCIP
1 /* Threaded work queue.
2 *
3 * Contents:
4 * 1. Work queue routines
5 * 2. Examples.
6 */
7 #include <esl_config.h>
8
9 #ifdef HAVE_PTHREAD
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <pthread.h>
15
16 #include "easel.h"
17 #include "esl_workqueue.h"
18
19 /*****************************************************************
20 *# 1. Work queue routines
21 *****************************************************************/
22
23 /* Function: esl_workqueue_Create()
24 * Synopsis: Create a work queue object.
25 * Incept: MSF, Thu Jun 18 11:51:39 2009
26 *
27 * Purpose: Creates an <ESL_WORK_QUEUE> object of <size>. The
28 * queues are used to handle objects <void *> that
29 * are ready to be processed and that have been
30 * processed by worker threads.
31 *
32 * Returns: ptr to the new <ESL_WORK_QUEUE> object.
33 *
34 * Throws: <eslESYS> on allocation or initialization failure.
35 */
36 ESL_WORK_QUEUE *
37 esl_workqueue_Create(int size)
38 {
39 int i;
40 int status;
41 ESL_WORK_QUEUE *queue = NULL;
42
43 ESL_ALLOC(queue, sizeof(ESL_WORK_QUEUE));
44
45 queue->readerQueue = NULL;
46 queue->readerQueueCnt = 0;
47 queue->readerQueueHead = 0;
48
49 queue->workerQueue = NULL;
50 queue->workerQueueCnt = 0;
51 queue->workerQueueHead = 0;
52
53 queue->queueSize = size;
54 queue->pendingWorkers = 0;
55
56 if (pthread_mutex_init(&queue->queueMutex, NULL) != 0) ESL_XEXCEPTION(eslESYS, "mutex init failed");
57
58 if (pthread_cond_init(&queue->readerQueueCond, NULL) != 0) ESL_XEXCEPTION(eslESYS, "cond reader init failed");
59 if (pthread_cond_init(&queue->workerQueueCond, NULL) != 0) ESL_XEXCEPTION(eslESYS, "cond worker init failed");
60
61 ESL_ALLOC(queue->readerQueue, sizeof(void *) * size);
62 ESL_ALLOC(queue->workerQueue, sizeof(void *) * size);
63
64 for (i = 0; i < queue->queueSize; ++i)
65 {
66 queue->readerQueue[i] = NULL;
67 queue->workerQueue[i] = NULL;
68 }
69
70 return queue;
71
72 ERROR:
73 esl_workqueue_Destroy(queue);
74 return NULL;
75 }
76
77 /* Function: esl_workqueue_queuelock_Create()
78 * Synopsis: Create a work queue queulock object.
79 * Incept: NPC 1/20/2023
80 *
81 * Purpose: Creates an <ESL_WORK_QUEUE_QUEUELOCK> object of <size>. The
82 * queues are used to handle objects <void *> that
83 * are ready to be processed and that have been
84 * processed by worker threads.
85 *
86 * Returns: ptr to the new <ESL_WORK_QUEUE_QUEUELOCK> object.
87 *
88 * Throws: <eslESYS> on allocation or initialization failure.
89 */
90 ESL_WORK_QUEUE_QUEUELOCK *esl_workqueue_queuelock_Create(int size)
91 {
92 int i;
93 int status;
94 ESL_WORK_QUEUE_QUEUELOCK *queue = NULL;
95
96 ESL_ALLOC(queue, sizeof(ESL_WORK_QUEUE_QUEUELOCK));
97
98 queue->readerQueue = NULL;
99 queue->readerQueueCnt = 0;
100 queue->readerQueueHead = 0;
101
102 queue->workerQueue = NULL;
103 queue->workerQueueCnt = 0;
104 queue->workerQueueHead = 0;
105
106 queue->readerWaitQueue = NULL;
107 queue->readerWaitQueueCnt = 0;
108 queue->readerWaitQueueHead = 0;
109
110 queue->workerWaitQueue = NULL;
111 queue->workerWaitQueueCnt = 0;
112 queue->workerWaitQueueHead = 0;
113
114 queue->queueSize = size;
115 queue->waitingWorkers = 0;
116 queue->waitingReaders = 0;
117
118 if (pthread_mutex_init(&queue->queueMutex, NULL) != 0) ESL_XEXCEPTION(eslESYS, "mutex init failed");
119
120 ESL_ALLOC(queue->readerQueue, sizeof(void *) * size);
121 ESL_ALLOC(queue->workerQueue, sizeof(void *) * size);
122 ESL_ALLOC(queue->readerWaitQueue, sizeof(void *) * size);
123 ESL_ALLOC(queue->workerWaitQueue, sizeof(void *) * size);
124
125 for (i = 0; i < queue->queueSize; ++i)
126 {
127 queue->readerQueue[i] = NULL;
128 queue->workerQueue[i] = NULL;
129 queue->readerWaitQueue[i] = NULL;
130 queue->workerWaitQueue[i] = NULL;
131 }
132
133 return queue;
134
135 ERROR:
136 esl_workqueue_queuelock_Destroy(queue);
137 return NULL;
138 }
139
140 /* Function: esl_workqueue_Destroy()
141 * Synopsis: Destroys an <ESL_WORK_QUEUE> object.
142 * Incept: MSF, Thu Jun 18 11:51:39 2009
143 *
144 * Purpose: Frees an <ESL_WORK_QUEUE> object.
145 *
146 * The calling routine is responsible for freeing the
147 * memory of the actual queued objects.
148 *
149 * Returns: void
150 */
151 void
152 esl_workqueue_Destroy(ESL_WORK_QUEUE *queue)
153 {
154 if (queue == NULL) return;
155
156 pthread_mutex_destroy (&queue->queueMutex);
157 pthread_cond_destroy (&queue->readerQueueCond);
158 pthread_cond_destroy (&queue->workerQueueCond);
159
160 if (queue->readerQueue != NULL) free(queue->readerQueue);
161 if (queue->workerQueue != NULL) free(queue->workerQueue);
162
163 free(queue);
164 }
165
166 /* Function: esl_workqueue_queuelock_Destroy()
167 * Synopsis: Destroys an <ESL_WORK_QUEUE_QUEUELOCK> object.
168 * Incept: NPC, 1/20/2023
169 *
170 * Purpose: Frees an <ESL_WORK_QUEUE_QUEUELOCK> object.
171 *
172 * The calling routine is responsible for freeing the
173 * memory of the actual queued objects.
174 *
175 * Returns: void
176 */
177 void
178 esl_workqueue_queuelock_Destroy(ESL_WORK_QUEUE_QUEUELOCK *queue)
179 {
180 if (queue == NULL) return;
181
182 pthread_mutex_destroy (&queue->queueMutex);
183
184 if (queue->readerQueue != NULL) free(queue->readerQueue);
185 if (queue->workerQueue != NULL) free(queue->workerQueue);
186 if (queue->readerWaitQueue != NULL) free(queue->readerWaitQueue);
187 if (queue->workerWaitQueue != NULL) free(queue->workerWaitQueue);
188
189 free(queue);
190 }
191
192 /* Function: esl_workqueue_Init()
193 * Synopsis: Adds a queued object to the producers list.
194 * Incept: MSF, Thu Jun 18 11:51:39 2009
195 *
196 * Purpose: Added a work object <void> to the producers list checking for
197 * any errors.
198 *
199 * Returns: <eslOK> on success.
200 *
201 * Throws: <eslESYS> if thread synchronization fails somewhere.
202 * <eslEINVAL> if something's wrong with <queue> or <ptr>.
203 */
204 int esl_workqueue_Init(ESL_WORK_QUEUE *queue, void *ptr)
205 {
206 int cnt;
207 int inx;
208
209 int queueSize;
210
211 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
212 if (ptr == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid reader object");
213
214 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
215
216 queueSize = queue->queueSize;
217
218 /* check to make sure we won't overflow */
219 cnt = queue->readerQueueCnt;
220 if (cnt >= queueSize) ESL_EXCEPTION(eslEINVAL, "Reader queue overflow");
221
222 inx = (queue->readerQueueHead + cnt) % queueSize;
223 queue->readerQueue[inx] = ptr;
224
225 ++queue->readerQueueCnt;
226 if (cnt == 0)
227 {
228 if (pthread_cond_signal (&queue->readerQueueCond) != 0) ESL_EXCEPTION(eslESYS, "cond signal failed");
229 }
230
231 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
232
233 return eslOK;
234 }
235
236 /* Function: esl_workqueue_queuelock_Init()
237 * Synopsis: Adds a queued object to the producers list.
238 * Incept: NPC 1/20/2023
239 *
240 * Purpose: Added a work object <void> to the producers list checking for
241 * any errors.
242 *
243 * Returns: <eslOK> on success.
244 *
245 * Throws: <eslESYS> if thread synchronization fails somewhere.
246 * <eslEINVAL> if something's wrong with <queue> or <ptr>.
247 */
248 int esl_workqueue_queuelock_Init(ESL_WORK_QUEUE_QUEUELOCK *queue, void *ptr)
249 {
250 int cnt;
251 int inx;
252
253 int queueSize;
254
255 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
256 if (ptr == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid reader object");
257
258 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
259
260 queueSize = queue->queueSize;
261
262 /* check to make sure we won't overflow */
263 cnt = queue->readerQueueCnt;
264 if (cnt >= queueSize) ESL_EXCEPTION(eslEINVAL, "Reader queue overflow");
265
266 if(queue->workerWaitQueueCnt > 0){ // There's at least one worker waiting for this block, so just pass it to them
267 *(queue->workerWaitQueue[queue->workerWaitQueueHead]) = ptr;
268 queue->workerWaitQueue[queue->workerWaitQueueHead] = NULL;
269 queue->workerWaitQueueHead = (queue->workerWaitQueueHead +1) % queue->queueSize;
270 queue->workerWaitQueueCnt -= 1;
271 }
272 else{ // put it in the queue
273 inx = (queue->readerQueueHead + cnt) % queueSize;
274 queue->readerQueue[inx] = ptr;
275
276 ++queue->readerQueueCnt;
277 }
278
279 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
280
281 return eslOK;
282 }
283
284 /* Function: esl_workqueue_Remove()
285 * Synopsis: Removes a queued object from the producers list.
286 * Incept: MSF, Thu Jun 18 11:51:39 2009
287 *
288 * Purpose: Removes a queued object from the producers list.
289 *
290 * A object <void> that has already been consumed by a worker
291 * is removed the the producers list. If there are no empty
292 * objects, a <obj> is set to NULL.
293 *
294 * The pointer to the object is returned in the obj arguement.
295 *
296 * Returns: <eslOK> on success.
297 * <eslEOD> if no objects are in the queue.
298 *
299 * Throws: <eslESYS> if thread synchronization fails somewhere.
300 * <eslEINVAL> if something's wrong with <queue>.
301 */
302 int
303 esl_workqueue_Remove(ESL_WORK_QUEUE *queue, void **obj)
304 {
305 int inx;
306 int status = eslEOD;
307
308 if (obj == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid object pointer");
309 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
310
311 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
312
313 /* check if there are any items on the readers list */
314 *obj = NULL;
315 if (queue->readerQueueCnt > 0)
316 {
317 inx = (queue->readerQueueHead + queue->readerQueueCnt) % queue->queueSize;
318 *obj = queue->readerQueue[inx];
319 queue->readerQueue[inx] = NULL;
320 --queue->readerQueueCnt;
321 status = eslOK;
322 }
323
324 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
325
326 return status;
327 }
328 /* Function: esl_workqueue_queuelock_Remove()
329 * Synopsis: Removes a queued object from the producers list.
330 * Incept: NPC 1/20-2023
331 *
332 * Purpose: Removes a queued object from the producers list.
333 *
334 * A object <void> that has already been consumed by a worker
335 * is removed the the producers list. If there are no empty
336 * objects, a <obj> is set to NULL.
337 *
338 * The pointer to the object is returned in the obj arguement.
339 *
340 * Returns: <eslOK> on success.
341 * <eslEOD> if no objects are in the queue.
342 *
343 * Throws: <eslESYS> if thread synchronization fails somewhere.
344 * <eslEINVAL> if something's wrong with <queue>.
345 */
346 int
347 esl_workqueue__queuelock_Remove(ESL_WORK_QUEUE_QUEUELOCK *queue, void **obj)
348 {
349 int inx;
350 int status = eslEOD;
351
352 if (obj == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid object pointer");
353 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
354
355 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
356
357 /* check if there are any items on the readers list */
358 *obj = NULL;
359 if (queue->readerQueueCnt > 0)
360 {
361 inx = (queue->readerQueueHead + queue->readerQueueCnt) % queue->queueSize;
362 *obj = queue->readerQueue[inx];
363 queue->readerQueue[inx] = NULL;
364 --queue->readerQueueCnt;
365 status = eslOK;
366 }
367
368 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
369
370 return status;
371 }
372
373
374 /* Function: esl_workqueue_Complete()
375 * Synopsis: Signals the end of the queue.
376 * Incept: MSF, Thu Jun 18 11:51:39 2009
377 *
378 * Purpose: Signal the end of the queue. If there are any threads
379 * waiting on an object, signal them to wake up and complete
380 * their processing.
381 *
382 * Returns: <eslOK> on success.
383 *
384 * Throws: <eslESYS> if thread synchronization fails somewhere.
385 * <eslEINVAL> if something's wrong with <queue>.
386 */
387 int
388 esl_workqueue_Complete(ESL_WORK_QUEUE *queue)
389 {
390 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
391 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
392
393 if (queue->pendingWorkers != 0)
394 {
395 if (pthread_cond_broadcast (&queue->workerQueueCond) != 0) ESL_EXCEPTION(eslESYS, "broadcast failed");
396 }
397
398 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
399
400 return eslOK;
401 }
402
403 /* Function: esl_workqueue_queuelock-Complete()
404 * Synopsis: Signals the end of the queue.
405 * Incept: NPC 1/20/2023
406 *
407 * Purpose: Signal the end of the queue. If there are any threads
408 * waiting on an object, signal them to wake up and complete
409 * their processing.
410 *
411 * Returns: <eslOK> on success.
412 *
413 * Throws: <eslESYS> if thread synchronization fails somewhere.
414 * <eslEINVAL> if something's wrong with <queue>.
415 */
416 int
417 esl_workqueue_queuelock_Complete(ESL_WORK_QUEUE_QUEUELOCK *queue)
418 {
419 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
420 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
421
422 while(queue->workerWaitQueueCnt > 0){
423
424 }
425
426 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
427
428 return eslOK;
429 }
430
431 /* Function: esl_workqueue_Reset()
432 * Synopsis: Reset the queue for another run.
433 * Incept: MSF, Thu Jun 18 11:51:39 2009
434 *
435 * Purpose: Reset the queue for another run. This is done by moving
436 * all the queued object to the reader's list (i.e. producer).
437 *
438 * Returns: <eslOK> on success.
439 *
440 * Throws: <eslESYS> if thread synchronization fails somewhere.
441 * <eslEINVAL> if something's wrong with <queue>.
442 */
443 int
444 esl_workqueue_Reset(ESL_WORK_QUEUE *queue)
445 {
446 int inx;
447 int queueSize;
448
449 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
450 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
451
452 queueSize = queue->queueSize;
453
454 /* move all buffers back to the reader queue */
455 while (queue->workerQueueCnt > 0)
456 {
457 inx = (queue->readerQueueHead + queue->readerQueueCnt) % queueSize;
458 queue->readerQueue[inx] = queue->workerQueue[queue->workerQueueHead];
459 ++queue->readerQueueCnt;
460
461 queue->workerQueue[queue->workerQueueHead] = NULL;
462 queue->workerQueueHead = (queue->workerQueueHead + 1) % queueSize;
463 --queue->workerQueueCnt;
464 }
465
466 queue->pendingWorkers = 0;
467
468 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
469
470 return eslOK;
471 }
472
473 /* Function: esl_workqueue_ReaderUpdate()
474 * Synopsis: Producer routine.
475 * Incept: MSF, Thu Jun 18 11:51:39 2009
476 *
477 * Purpose: The producer (i.e. Reader) places an object, that is
478 * ready to be processed by a worker on the consumers
479 * (i.e. Workers) work queue.
480 *
481 * If the <in> object is not null, it is placed on the
482 * workers queue. If there are any workers waiting for
483 * an object, they are signaled to wake up.
484 *
485 * If the reader routine has supplied an <out> pointer,
486 * an object that has already been processed by a worker,
487 * is placed in <out> so the object can be made ready
488 * for another worker thread.
489 *
490 * Returns: <eslOK> on success.
491 *
492 * Throws: <eslESYS> if thread synchronization fails somewhere.
493 * <eslEINVAL> if something's wrong with <queue>.
494 */
495 int esl_workqueue_ReaderUpdate(ESL_WORK_QUEUE *queue, void *in, void **out)
496 {
497 int inx;
498 int queueSize;
499
500 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
501 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
502
503 queueSize = queue->queueSize;
504
505 /* check if the caller is queuing up an item */
506 if (in != NULL)
507 {
508
509 /* check to make sure we don't overflow */
510 if (queue->workerQueueCnt >= queueSize) ESL_EXCEPTION(eslEINVAL, "Work queue overflow");
511
512 inx = (queue->workerQueueHead + queue->workerQueueCnt) % queueSize;
513 queue->workerQueue[inx] = in;
514 ++queue->workerQueueCnt;
515
516 if (queue->pendingWorkers != 0)
517 {
518 if (pthread_cond_broadcast (&queue->workerQueueCond) != 0) ESL_EXCEPTION(eslESYS, "broadcast failed");
519 }
520 }
521
522 /* check if the caller is waiting for a queued item */
523 if (out != NULL)
524 {
525
526 /* wait for a processed buffers to be returned */
527 while (queue->readerQueueCnt == 0)
528 {
529 if (pthread_cond_wait (&queue->readerQueueCond, &queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "cond wait failed");
530 }
531
532 inx = queue->readerQueueHead;
533 *out = queue->readerQueue[inx];
534 queue->readerQueue[inx] = NULL;
535 queue->readerQueueHead = (queue->readerQueueHead + 1) % queueSize;
536 --queue->readerQueueCnt;
537 }
538
539 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
540
541 return eslOK;
542 }
543
544 /* Function: esl_workqueue_WorkerUpdate()
545 * Synopsis: Consumer routine.
546 * Incept: MSF, Thu Jun 18 11:51:39 2009
547 *
548 * Purpose: The consumer (i.e. Worker) places an object that has
549 * been processed on the producers (i.e. Readers) queue.
550 *
551 * If the <in> object is not null, it is placed on the
552 * readers queue. If the reader is waiting for an object,
553 * it is signaled it to wake up.
554 *
555 * If the worker routine has supplied an <out> pointer,
556 * an object that is ready for processing by a worker,
557 * is placed in <out> so the worker thread can continue.
558 *
559 * Returns: <eslOK> on success.
560 *
561 * Throws: <eslESYS> if thread synchronization fails somewhere.
562 * <eslEINVAL> if something's wrong with <queue>.
563 */
564 int esl_workqueue_WorkerUpdate(ESL_WORK_QUEUE *queue, void *in, void **out)
565 {
566 int cnt;
567 int inx;
568 int queueSize;
569 int status;
570
571 if (queue == NULL) ESL_XEXCEPTION(eslEINVAL, "Invalid queue object");
572 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_XEXCEPTION(eslESYS, "mutex lock failed");
573
574 queueSize = queue->queueSize;
575
576 /* check if the caller is queuing up an item */
577 if (in != NULL)
578 {
579
580 /* check to make sure we don't overflow */
581 if (queue->readerQueueCnt >= queueSize) ESL_XEXCEPTION(eslEINVAL, "Reader queue overflow");
582
583 inx = (queue->readerQueueHead + queue->readerQueueCnt) % queueSize;
584 queue->readerQueue[inx] = in;
585 cnt = queue->readerQueueCnt++;
586 if (cnt == 0)
587 {
588 if (pthread_cond_signal (&queue->readerQueueCond) != 0) ESL_XEXCEPTION(eslESYS, "cond signal failed");
589 }
590 }
591
592 /* check if the caller is waiting for a queued item */
593 if (out != NULL)
594 {
595
596 if (queue->workerQueueCnt == 0)
597 {
598 /* wait for a processed buffers to be returned */
599 ++queue->pendingWorkers;
600 while (queue->workerQueueCnt == 0)
601 {
602 if (pthread_cond_wait (&queue->workerQueueCond, &queue->queueMutex) != 0) ESL_XEXCEPTION(eslESYS, "cond wait failed");
603 }
604 --queue->pendingWorkers;
605 }
606
607 inx = queue->workerQueueHead;
608 *out = queue->workerQueue[inx];
609 queue->workerQueue[inx] = NULL;
610 queue->workerQueueHead = (queue->workerQueueHead + 1) % queueSize;
611 --queue->workerQueueCnt;
612 }
613
614 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_XEXCEPTION(eslESYS, "mutex unlock failed");
615 return eslOK;
616
617 ERROR:
618 if (out) *out = NULL;
619 return status;
620 }
621
622 /* Function: esl_workqueue_Dump()
623 * Synopsis: Print the contents of the queues.
624 * Incept: MSF, Thu Jun 18 11:51:39 2009
625 *
626 * Purpose: Print the contents of the queues and their pointers.
627 *
628 * Returns: <eslOK> on success.
629 */
630 int esl_workqueue_Dump(ESL_WORK_QUEUE *queue)
631 {
632 int i;
633
634 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
635 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
636
637 printf ("Reader head: %2d count: %2d\n", queue->readerQueueHead, queue->readerQueueCnt);
638 printf ("Worker head: %2d count: %2d\n", queue->workerQueueHead, queue->workerQueueCnt);
639 for (i = 0; i < queue->queueSize; ++i)
640 {
641 printf (" %2d: %p %p\n", i, queue->readerQueue[i], queue->workerQueue[i]);
642 }
643 printf ("Pending: %2d\n\n", queue->pendingWorkers);
644
645 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
646
647 return eslOK;
648 }
649
650 /* Function: esl_workqueue_queuelock_Dump()
651 * Synopsis: Print the contents of the queues.
652 * Incept: NPC, 1/20/23
653 *
654 * Purpose: Print the contents of the queues and their pointers.
655 *
656 * Returns: <eslOK> on success.
657 */
658 int esl_workqueue__queuelock_Dump(ESL_WORK_QUEUE_QUEUELOCK *queue)
659 {
660 int i;
661
662 if (queue == NULL) ESL_EXCEPTION(eslEINVAL, "Invalid queue object");
663 if (pthread_mutex_lock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
664
665 printf ("Reader head: %2d count: %2d\n", queue->readerQueueHead, queue->readerQueueCnt);
666 printf ("Worker head: %2d count: %2d\n", queue->workerQueueHead, queue->workerQueueCnt);
667 for (i = 0; i < queue->queueSize; ++i)
668 {
669 printf (" %2d: %p %p, %p, %p\n", i, queue->readerQueue[i], queue->workerQueue[i], queue->readerWaitQueue[i], queue->workerWaitQueue[i]);
670 }
671 printf("Waiting workers: %d\n", queue->waitingWorkers);
672 printf("Waiting readers: %d\n", queue->waitingReaders);
673
674 if (pthread_mutex_unlock (&queue->queueMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
675
676 return eslOK;
677 }
678 /*****************************************************************
679 * 2. Example
680 *****************************************************************/
681
682 #ifdef eslWORKQUEUE_EXAMPLE
683 #include "easel.h"
684 #include "esl_threads.h"
685 #include "esl_workqueue.h"
686
687 typedef struct {
688 char id;
689 ESL_WORK_QUEUE *queue;
690 } WORK_INFO;
691
692 /* gcc --std=gnu99 -g -Wall -pthread -o esl_workqueue_example -I. -DeslWORKQUEUE_EXAMPLE esl_workqueue.c easel.c */
693 static void
694 worker_thread(void *data)
695 {
696 ESL_THREADS *thr = (ESL_THREADS *) data;
697 WORK_INFO *info = NULL;
698 int idx;
699
700 int *obj;
701
702 esl_threads_Started(thr, &idx);
703
704 info = (WORK_INFO *) esl_threads_GetData(thr, idx);
705 printf("THREAD %c: ready\n", info->id);
706
707 esl_workqueue_WorkerUpdate(info->queue, NULL, (void *) &obj);
708 while (*obj > 0)
709 {
710 printf("THREAD %c: processing %d\n", info->id, *obj);
711 esl_workqueue_WorkerUpdate(info->queue, obj, (void *) &obj);
712 }
713
714 printf("THREAD %c: done\n", info->id);
715
716 esl_threads_Finished(thr, idx);
717 return;
718 }
719
720 int
721 main(void)
722 {
723 int i;
724 int ncpu = 4;
725 int iter = 25;
726 WORK_INFO *worker = NULL;
727
728 ESL_THREADS *thr = NULL;
729 ESL_WORK_QUEUE *queue = NULL;
730
731 int *objs = NULL;
732 int *obj;
733
734 objs = malloc(sizeof(int) * ncpu * 2);
735 worker = malloc(sizeof(WORK_INFO) * ncpu);
736
737 thr = esl_threads_Create(&worker_thread);
738
739 /* Create a work queue that is able to hold two items per thread.
740 * The idea is that while one object is being processed by a
741 * worker thread, another item is being readied. So, when the
742 * worker thread has completed processing its current object,
743 * its next object to processes is hopefully waiting.
744 */
745 queue = esl_workqueue_Create(ncpu * 2);
746 for (i = 0; i < ncpu * 2; i++)
747 {
748 objs[i] = 0;
749 esl_workqueue_Init(queue, &objs[i]);
750 }
751
752 for (i = 0; i < ncpu; i++)
753 {
754 worker[i].id = 'A' + i;
755 worker[i].queue = queue;
756 esl_threads_AddThread(thr, (void *) &worker[i]);
757 }
758
759 esl_threads_WaitForStart (thr);
760
761 /* For N number of iterations, get an object that has been
762 * processed, i.e. on the readers input queue and place it
763 * on the ready queue.
764 */
765 esl_workqueue_ReaderUpdate(queue, NULL, (void **) &obj);
766 for (i = 1; i <= iter; ++i)
767 {
768 *obj = i;
769 printf("Item %d is ready to be processed\n", *obj);
770 esl_workqueue_ReaderUpdate(queue, obj, (void **) &obj);
771 }
772
773 /* put zeros on the queues to signal the worker that we are done */
774 for (i = 0; i < ncpu; ++i)
775 {
776 *obj = 0;
777 esl_workqueue_ReaderUpdate(queue, obj, (void **) &obj);
778 }
779
780 /* The worker threads now run their work. */
781 esl_threads_WaitForFinish(thr);
782 esl_threads_Destroy(thr);
783
784 esl_workqueue_Destroy(queue);
785
786 free(worker);
787 free(objs);
788
789 return eslOK;
790 }
791 #endif /*eslWORKQUEUE_EXAMPLE*/
792 #endif /* HAVE_PTHREAD */
793
794
795