// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/bio.c
// Function: bioProcessBackgroundJobs
void *bioProcessBackgroundJobs(void *arg) {
    struct bio_job *job;
    unsigned long type = (unsigned long) arg;
    sigset_t sigset;

    /* Make the thread killable at any time, so that bioKillThreads()
     * can work reliably. */
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    pthread_mutex_lock(&bio_mutex[type]);
    /* Block SIGALRM so we are sure that only the main thread will
     * receive the watchdog signal. */
    sigemptyset(&sigset);
    sigaddset(&sigset, SIGALRM);
    if (pthread_sigmask(SIG_BLOCK, &sigset, NULL))
        redisLog(REDIS_WARNING,
            "Warning: can't mask SIGALRM in bio.c thread: %s", strerror(errno));

    while(1) {
        listNode *ln;

        /* The loop always starts with the lock hold. */
        if (listLength(bio_jobs[type]) == 0) {
            pthread_cond_wait(&bio_condvar[type],&bio_mutex[type]);
            continue;
        }

        /* Pop the job from the queue. 
         *
         * 取出（但不删除）队列中的首个任务
         */
        ln = listFirst(bio_jobs[type]);
        job = ln->value;

        /* It is now possible to unlock the background system as we know have
         * a stand alone job structure to process.*/
        pthread_mutex_unlock(&bio_mutex[type]);

        /* Process the job accordingly to its type. */
        // 执行任务
        if (type == REDIS_BIO_CLOSE_FILE) {
            close((long)job->arg1);

        } else if (type == REDIS_BIO_AOF_FSYNC) {
            aof_fsync((long)job->arg1);

        } else {
            redisPanic("Wrong job type in bioProcessBackgroundJobs().");
        }

        zfree(job);

        /* Lock again before reiterating the loop, if there are no longer
         * jobs to process we'll block again in pthread_cond_wait(). */
        pthread_mutex_lock(&bio_mutex[type]);
        // 将执行完成的任务从队列中删除，并减少任务计数器
        listDelNode(bio_jobs[type],ln);
        bio_pending[type]--;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x18(%rbp)
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f050
movl $0x1, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f5e0
leaq 0xb2799(%rip), %rdi     # 0x128140
imulq $0x28, -0x18(%rbp), %rax
addq %rax, %rdi
callq 0x1fac0
leaq -0x98(%rbp), %rdi
callq 0x1fa50
leaq -0x98(%rbp), %rdi
movl $0xe, %esi
callq 0x1f860
xorl %edi, %edi
leaq -0x98(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1f160
cmpl $0x0, %eax
je 0x75a0a
callq 0x1f820
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
movl $0x3, %edi
leaq 0x725f8(%rip), %rsi     # 0xe7ffb
movb $0x0, %al
callq 0x24fb0
jmp 0x75a0c
movq -0x18(%rbp), %rcx
leaq 0xb27d9(%rip), %rax     # 0x1281f0
movq (%rax,%rcx,8), %rax
cmpq $0x0, 0x28(%rax)
jne 0x75a47
leaq 0xb2767(%rip), %rdi     # 0x128190
imulq $0x30, -0x18(%rbp), %rax
addq %rax, %rdi
leaq 0xb2708(%rip), %rsi     # 0x128140
imulq $0x28, -0x18(%rbp), %rax
addq %rax, %rsi
callq 0x1f930
jmp 0x75a0c
movq -0x18(%rbp), %rcx
leaq 0xb279e(%rip), %rax     # 0x1281f0
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x10(%rbp)
leaq 0xb26ca(%rip), %rdi     # 0x128140
imulq $0x28, -0x18(%rbp), %rax
addq %rax, %rdi
callq 0x1fbd0
cmpq $0x0, -0x18(%rbp)
jne 0x75a9b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edi
callq 0x1f170
jmp 0x75ad7
cmpq $0x1, -0x18(%rbp)
jne 0x75ab3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edi
callq 0x1f540
jmp 0x75ad5
leaq 0x72571(%rip), %rdi     # 0xe802b
leaq 0x7259a(%rip), %rsi     # 0xe805b
movl $0xff, %edx
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
jmp 0x75ad7
movq -0x10(%rbp), %rdi
callq 0x2e3a0
leaq 0xb2659(%rip), %rdi     # 0x128140
imulq $0x28, -0x18(%rbp), %rax
addq %rax, %rdi
callq 0x1fac0
movq -0x18(%rbp), %rcx
leaq 0xb26f1(%rip), %rax     # 0x1281f0
movq (%rax,%rcx,8), %rdi
movq -0xa0(%rbp), %rsi
callq 0x20170
movq -0x18(%rbp), %rcx
leaq 0xb26e6(%rip), %rax     # 0x128200
movq (%rax,%rcx,8), %rdx
addq $-0x1, %rdx
leaq 0xb26d7(%rip), %rax     # 0x128200
movq %rdx, (%rax,%rcx,8)
jmp 0x75a0c
nopw %cs:(%rax,%rax)