| 1937 | } |
| 1938 | |
| 1939 | cbm_proc_poll_t cbm_subprocess_poll(cbm_subprocess_t *process, cbm_proc_result_t *out) { |
| 1940 | if (!process) { |
| 1941 | return CBM_PROC_POLL_ERROR; |
| 1942 | } |
| 1943 | int lifecycle = atomic_load_explicit(&process->lifecycle, memory_order_acquire); |
| 1944 | if (lifecycle == CBM_SUBPROCESS_TERMINAL) { |
| 1945 | if (out) { |
| 1946 | *out = process->result; |
| 1947 | } |
| 1948 | return CBM_PROC_POLL_TERMINAL; |
| 1949 | } |
| 1950 | if (lifecycle == CBM_SUBPROCESS_DRAINING) { |
| 1951 | cbm_tail_result_t tail = cbm_subprocess_poll_log(process, true); |
| 1952 | if (tail.status == CBM_TAIL_MORE) { |
| 1953 | return CBM_PROC_POLL_RUNNING; |
| 1954 | } |
| 1955 | if (tail.status == CBM_TAIL_ERROR) { |
| 1956 | cbm_log_error("subprocess.log_drain_failed", "reason", "io_error"); |
| 1957 | return cbm_subprocess_publish_terminal(process, out, false); |
| 1958 | } |
| 1959 | return cbm_subprocess_publish_terminal(process, out, true); |
| 1960 | } |
| 1961 | /* The one owner-thread tail batch for this public poll. Platform-specific |
| 1962 | * reap paths never tail again, preserving the exact per-poll work cap. */ |
| 1963 | (void)cbm_subprocess_poll_log(process, false); |
| 1964 | #ifdef _WIN32 |
| 1965 | return cbm_subprocess_poll_win(process, out); |
| 1966 | #else |
| 1967 | return cbm_subprocess_poll_posix(process, out); |
| 1968 | #endif |
| 1969 | } |
| 1970 | |
| 1971 | bool cbm_subprocess_request_cancel(cbm_subprocess_t *process) { |
| 1972 | if (!process) { |