// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/t_list.c
// Function: handleClientsBlockedOnLists
void handleClientsBlockedOnLists(void) {

    // 遍历整个 ready_keys 链表
    while(listLength(server.ready_keys) != 0) {
        list *l;

        /* Point server.ready_keys to a fresh list and save the current one
         * locally. This way as we run the old list we are free to call
         * signalListAsReady() that may push new elements in server.ready_keys
         * when handling clients blocked into BRPOPLPUSH. */
        // 备份旧的 ready_keys ，再给服务器端赋值一个新的
        l = server.ready_keys;
        server.ready_keys = listCreate();

        while(listLength(l) != 0) {

            // 取出 ready_keys 中的首个链表节点
            listNode *ln = listFirst(l);

            // 指向 readyList 结构
            readyList *rl = ln->value;

            /* First of all remove this key from db->ready_keys so that
             * we can safely call signalListAsReady() against this key. */
            // 从 ready_keys 中移除就绪的 key
            dictDelete(rl->db->ready_keys,rl->key);

            /* If the key exists and it's a list, serve blocked clients
             * with data. */
            // 获取键对象，这个对象应该是非空的，并且是列表
            robj *o = lookupKeyWrite(rl->db,rl->key);
            if (o != NULL && o->type == REDIS_LIST) {
                dictEntry *de;

                /* We serve clients in the same order they blocked for
                 * this key, from the first blocked to the last. */
                // 取出所有被这个 key 阻塞的客户端
                de = dictFind(rl->db->blocking_keys,rl->key);
                if (de) {
                    list *clients = dictGetVal(de);
                    int numclients = listLength(clients);

                    while(numclients--) {
                        // 取出客户端
                        listNode *clientnode = listFirst(clients);
                        redisClient *receiver = clientnode->value;

                        // 设置弹出的目标对象（只在 BRPOPLPUSH 时使用）
                        robj *dstkey = receiver->bpop.target;

                        // 从列表中弹出元素
                        // 弹出的位置取决于是执行 BLPOP 还是 BRPOP 或者 BRPOPLPUSH
                        int where = (receiver->lastcmd &&
                                     receiver->lastcmd->proc == blpopCommand) ?
                                    REDIS_HEAD : REDIS_TAIL;
                        robj *value = listTypePop(o,where);

                        // 还有元素可弹出（非 NULL）
                        if (value) {
                            /* Protect receiver->bpop.target, that will be
                             * freed by the next unblockClient()
                             * call. */
                            if (dstkey) incrRefCount(dstkey);

                            // 取消客户端的阻塞状态
                            unblockClient(receiver);

                            // 将值 value 推入到造成客户度 receiver 阻塞的 key 上
                            if (serveClientBlockedOnList(receiver,
                                rl->key,dstkey,rl->db,value,
                                where) == REDIS_ERR)
                            {
                                /* If we failed serving the client we need
                                 * to also undo the POP operation. */
                                    listTypePush(o,value,where);
                            }

                            if (dstkey) decrRefCount(dstkey);
                            decrRefCount(value);
                        } else {
                            // 如果执行到这里，表示还有至少一个客户端被键所阻塞
                            // 这些客户端要等待对键的下次 PUSH
                            break;
                        }
                    }
                }
                
                // 如果列表元素已经为空，那么从数据库中将它删除
                if (listTypeLength(o) == 0) dbDelete(rl->db,rl->key);
                /* We don't call signalModifiedKey() as it was already called
                 * when an element was pushed on the list. */
            }

            /* Free this item. */
            decrRefCount(rl->key);
            zfree(rl);
            listDelNode(l,ln);
        }
        listRelease(l); /* We have the new list on place at this point. */
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
leaq 0x8edaa(%rip), %rbx     # 0xc9fa8
movq 0x740(%rbx), %r14
cmpq $0x0, 0x28(%r14)
je 0x3b3d6
leaq 0x28(%r14), %r12
callq 0x203fc
movq %rax, 0x740(%rbx)
cmpq $0x0, 0x28(%r14)
je 0x3b3b1
movq %r14, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq (%r14), %rbx
movq 0x10(%rbx), %r15
movq (%r15), %rax
movq 0x8(%r15), %rsi
movq 0x18(%rax), %rdi
callq 0x22796
movq (%r15), %rdi
movq 0x8(%r15), %rsi
callq 0x31724
testq %rax, %rax
je 0x3b38a
movl (%rax), %ecx
andl $0xf, %ecx
cmpl $0x1, %ecx
jne 0x3b38a
movq %rax, (%rsp)
movq %rbx, 0x18(%rsp)
movq (%r15), %rax
movq 0x8(%r15), %rsi
movq 0x10(%rax), %rdi
callq 0x226ac
testq %rax, %rax
je 0x3b361
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x28(%rax), %r12d
testl %r12d, %r12d
je 0x3b361
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %r14
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x3b2cf
xorl %ebp, %ebp
leaq 0x120(%rip), %rcx       # 0x3b3e5
cmpq %rcx, 0x8(%rax)
setne %bpl
jmp 0x3b2d4
movl $0x1, %ebp
movq (%rsp), %rdi
movq 0x130(%r14), %rbx
movl %ebp, %esi
callq 0x3988d
testq %rax, %rax
je 0x3b361
movq %rax, %r13
testq %rbx, %rbx
je 0x3b312
movq %rbx, %rdi
callq 0x306f3
movq %r14, %rdi
callq 0x60977
movq (%r15), %rcx
movq 0x8(%r15), %rsi
movq %r14, %rdi
movq %rbx, %rdx
jmp 0x3b326
movq %r14, %rdi
callq 0x60977
movq (%r15), %rcx
movq 0x8(%r15), %rsi
movq %r14, %rdi
xorl %edx, %edx
movq %r13, %r8
movl %ebp, %r9d
callq 0x3b06d
cmpl $-0x1, %eax
jne 0x3b344
movq (%rsp), %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x397a9
testq %rbx, %rbx
je 0x3b351
movq %rbx, %rdi
callq 0x30b0c
decl %r12d
movq %r13, %rdi
callq 0x30b0c
jmp 0x3b29e
movq (%rsp), %rdi
callq 0x39979
testq %rax, %rax
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r12
movq 0x18(%rsp), %rbx
jne 0x3b38a
movq (%r15), %rdi
movq 0x8(%r15), %rsi
callq 0x31a36
movq 0x8(%r15), %rdi
callq 0x30b0c
movq %r15, %rdi
callq 0x29af7
movq %r14, %rdi
movq %rbx, %rsi
callq 0x205b1
cmpq $0x0, (%r12)
jne 0x3b235
movq %r14, %rdi
callq 0x2041c
leaq 0x8ebe8(%rip), %rbx     # 0xc9fa8
movq 0x740(%rbx), %r14
leaq 0x28(%r14), %r12
cmpq $0x0, 0x28(%r14)
jne 0x3b214
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq