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