* Wait for all workers to finish computing. * * Even if the parallel operation seems to have completed successfully, it's * important to call this function afterwards. We must not miss any errors * the workers may have thrown during the parallel operation, or any that they * may yet throw while shutting down. * * Also, we want to update our notion of XactLastRecEnd based on worker * feedb
| 808 | * feedback. |
| 809 | */ |
| 810 | void |
| 811 | WaitForParallelWorkersToFinish(ParallelContext *pcxt) |
| 812 | { |
| 813 | for (;;) |
| 814 | { |
| 815 | bool anyone_alive = false; |
| 816 | int nfinished = 0; |
| 817 | int i; |
| 818 | |
| 819 | /* |
| 820 | * This will process any parallel messages that are pending, which may |
| 821 | * change the outcome of the loop that follows. It may also throw an |
| 822 | * error propagated from a worker. |
| 823 | */ |
| 824 | CHECK_FOR_INTERRUPTS(); |
| 825 | |
| 826 | for (i = 0; i < pcxt->nworkers_launched; ++i) |
| 827 | { |
| 828 | /* |
| 829 | * If error_mqh is NULL, then the worker has already exited |
| 830 | * cleanly. If we have received a message through error_mqh from |
| 831 | * the worker, we know it started up cleanly, and therefore we're |
| 832 | * certain to be notified when it exits. |
| 833 | */ |
| 834 | if (pcxt->worker[i].error_mqh == NULL) |
| 835 | ++nfinished; |
| 836 | else if (pcxt->known_attached_workers[i]) |
| 837 | { |
| 838 | anyone_alive = true; |
| 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (!anyone_alive) |
| 844 | { |
| 845 | /* If all workers are known to have finished, we're done. */ |
| 846 | if (nfinished >= pcxt->nworkers_launched) |
| 847 | { |
| 848 | Assert(nfinished == pcxt->nworkers_launched); |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * We didn't detect any living workers, but not all workers are |
| 854 | * known to have exited cleanly. Either not all workers have |
| 855 | * launched yet, or maybe some of them failed to start or |
| 856 | * terminated abnormally. |
| 857 | */ |
| 858 | for (i = 0; i < pcxt->nworkers_launched; ++i) |
| 859 | { |
| 860 | pid_t pid; |
| 861 | shm_mq *mq; |
| 862 | |
| 863 | /* |
| 864 | * If the worker is BGWH_NOT_YET_STARTED or BGWH_STARTED, we |
| 865 | * should just keep waiting. If it is BGWH_STOPPED, then |
| 866 | * further investigation is needed. |
| 867 | */ |
no test coverage detected