Commit b8279c3
Change the approach to recruiting threads from a
Since calling the `pthreadpool_executor`'s `schedule` function can be expensive, the calling thread currently packed an array of data for the threads needing to be scheduled and, in turn, scheduled a single call to the `wake_up_threads` function that would schedule them.
This apporach, although more efficient than letting the calling thread do the scheduling, has high latency due to the additional level of indirection.
This change removes the `wake_up_threads` function and modifies `ensure_num_threads` to check if any threads with an ID higher than the current thread need to be scheduled, and schedules only the first two it finds.
A call to `ensure_num_threads` is added at the top of the `thread_main` function for each scheduled thread, thus ensuring that threads are scheduled in a cascading fashion.
If no threads are scheduled,
* The first call to `pthreadpool_parallelize` will call `ensure_num_threads(threadpool, 0)`, and the `pthreadpool_executor`'s `schedule` function once to schedule the first and second threads, then go on to do work.
* The first thread, at the top of the `thread_main` function, will then call `ensure_num_threads(threadpool, 1)` and schedule the third and fourth threads, then go on to do work.
* The second thread, etc...
One advantage of this approach is that `ensure_num_threads` will stop scheduling threads if the work has already been completed, thus avoiding overheads where we have a large number of threads, but very little work.
Since each thread schedules two more threads, the latency until the last thread is scheduled is at most `O(log(n))`.
PiperOrigin-RevId: 814693873pthreadpool_executor.1 parent d5952a0 commit b8279c3
1 file changed
Lines changed: 48 additions & 65 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
432 | 432 | | |
433 | 433 | | |
434 | 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 | + | |
435 | 479 | | |
436 | 480 | | |
437 | 481 | | |
| |||
443 | 487 | | |
444 | 488 | | |
445 | 489 | | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
446 | 493 | | |
447 | 494 | | |
448 | 495 | | |
| |||
603 | 650 | | |
604 | 651 | | |
605 | 652 | | |
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 | 653 | | |
671 | 654 | | |
672 | 655 | | |
| |||
737 | 720 | | |
738 | 721 | | |
739 | 722 | | |
740 | | - | |
| 723 | + | |
741 | 724 | | |
742 | 725 | | |
743 | 726 | | |
| |||
0 commit comments