// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/sentinel.c
// Function: sentinelCollectTerminatedScripts
void sentinelCollectTerminatedScripts(void) {
    int statloc;
    pid_t pid;

    // 获取子进程信号
    while ((pid = wait3(&statloc,WNOHANG,NULL)) > 0) {
        int exitcode = WEXITSTATUS(statloc);
        int bysignal = 0;
        listNode *ln;
        sentinelScriptJob *sj;

        // 发送脚本终结信号
        if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);
        sentinelEvent(REDIS_DEBUG,"-script-child",NULL,"%ld %d %d",
            (long)pid, exitcode, bysignal);
        
        // 在队列中安 pid 查找脚本
        ln = sentinelGetScriptListNodeByPid(pid);
        if (ln == NULL) {
            redisLog(REDIS_WARNING,"wait3() returned a pid (%ld) we can't find in our scripts execution queue!", (long)pid);
            continue;
        }
        sj = ln->value;

        /* If the script was terminated by a signal or returns an
         * exit code of "1" (that means: please retry), we reschedule it
         * if the max number of retries is not already reached. */
        if ((bysignal || exitcode == 1) &&
            sj->retry_num != SENTINEL_SCRIPT_MAX_RETRY)
        {
            // 重试脚本

            sj->flags &= ~SENTINEL_SCRIPT_RUNNING;
            sj->pid = 0;
            sj->start_time = mstime() +
                             sentinelScriptRetryDelay(sj->retry_num);
        } else {
            /* Otherwise let's remove the script, but log the event if the
             * execution did not terminated in the best of the ways. */

            // 发送脚本执行错误事件
            if (bysignal || exitcode != 0) {
                sentinelEvent(REDIS_WARNING,"-script-error",NULL,
                              "%s %d %d", sj->argv[0], bysignal, exitcode);
            }

            // 将脚本从队列中删除
            listDelNode(sentinel.scripts_queue,ln);
            sentinelReleaseScriptJob(sj);
            sentinel.running_scripts--;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0x14(%rsp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1f800
testl %eax, %eax
jle 0x5be47
movl %eax, %r13d
leaq 0x14(%rsp), %r12
movl 0x14(%rsp), %ebx
movzbl %bh, %ebp
andl $0x7f, %ebx
movl %ebx, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
movl $0x0, %eax
cmovll %eax, %ebx
movl %r13d, %r15d
movl %ebx, (%rsp)
xorl %edi, %edi
leaq 0x4317a(%rip), %rsi     # 0x9eed4
xorl %edx, %edx
leaq 0x4317f(%rip), %rcx     # 0x9eee2
movq %r15, %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5b66f
movl %r13d, %edi
callq 0x5bb53
testq %rax, %rax
je 0x5bde2
movq %rax, %r14
movq 0x10(%rax), %r13
cmpl $0x1, %ebp
je 0x5bd8d
testl %ebx, %ebx
je 0x5bd94
cmpl $0xa, 0x4(%r13)
jne 0x5bdfa
movl %ebx, %eax
orl %ebp, %eax
je 0x5bdc3
movq 0x8(%r13), %rax
movq (%rax), %r8
movl %ebp, (%rsp)
movl $0x3, %edi
leaq 0x43108(%rip), %rsi     # 0x9eeb8
xorl %edx, %edx
movl %ebx, %r9d
leaq 0x434a0(%rip), %rcx     # 0x9f25c
xorl %eax, %eax
callq 0x5b66f
movq 0x82b46(%rip), %rdi     # 0xde910
movq %r14, %rsi
callq 0x20544
movq %r13, %rdi
callq 0x5bb0d
decl 0x82b1c(%rip)           # 0xde8fc
jmp 0x5be2d
movl $0x3, %edi
leaq 0x430fe(%rip), %rsi     # 0x9eeec
movq %r15, %rdx
xorl %eax, %eax
callq 0x23064
jmp 0x5be2d
andb $-0x2, (%r13)
movl $0x0, 0x18(%r13)
callq 0x232a3
movl 0x4(%r13), %ecx
movl $0x7530, %edx           # imm = 0x7530
cmpl $0x2, %ecx
jl 0x5be26
incl %ecx
addq %rdx, %rdx
decl %ecx
cmpl $0x2, %ecx
jg 0x5be1c
addq %rax, %rdx
movq %rdx, 0x10(%r13)
movq %r12, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1f800
movl %eax, %r13d
testl %eax, %eax
jg 0x5bd2a
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq