// File: /workspace/github_c_makefile_star2_O2/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 0x8e7ab(%rip), %rax     # 0xc6fa8
movq 0x740(%rax), %r14
cmpq $0x0, 0x28(%r14)
je 0x38994
movq %rax, %rbx
callq 0x1fd8c
movq %rax, 0x740(%rbx)
movq %r14, 0x10(%rsp)
cmpq $0x0, 0x28(%r14)
je 0x38987
movq (%r14), %rbx
movq 0x10(%rbx), %r15
movq (%r15), %rax
movq 0x8(%r15), %rsi
movq 0x18(%rax), %rdi
callq 0x21c36
movq (%r15), %rdi
movq 0x8(%r15), %rsi
callq 0x2f988
testq %rax, %rax
je 0x38966
movl (%rax), %ecx
andl $0xf, %ecx
cmpl $0x1, %ecx
jne 0x38966
movq %rax, 0x8(%rsp)
movq %rbx, 0x18(%rsp)
movq (%r15), %rax
movq 0x8(%r15), %rsi
movq 0x10(%rax), %rdi
callq 0x21b64
testq %rax, %rax
je 0x38941
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x28(%rax), %r12d
subl $0x1, %r12d
jb 0x38941
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %r14
movq 0x48(%r14), %rax
pushq $0x1
popq %rbp
testq %rax, %rax
movq 0x8(%rsp), %rdi
je 0x388d0
xorl %ebp, %ebp
leaq 0xdb(%rip), %rcx        # 0x389a3
cmpq %rcx, 0x8(%rax)
setne %bpl
movq 0x130(%r14), %r13
movl %ebp, %esi
callq 0x36f24
testq %rax, %rax
je 0x38941
movq %rax, %rbx
testq %r13, %r13
je 0x388f3
movq %r13, %rdi
callq 0x2eacc
movq %r14, %rdi
callq 0x5ba5f
movq (%r15), %rcx
movq 0x8(%r15), %rsi
movq %r14, %rdi
movq %r13, %rdx
movq %rbx, %r8
movl %ebp, %r9d
callq 0x38680
cmpl $-0x1, %eax
jne 0x38927
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x36e46
testq %r13, %r13
je 0x38934
movq %r13, %rdi
callq 0x2ee1f
movq %rbx, %rdi
callq 0x2ee1f
jmp 0x38898
movq 0x8(%rsp), %rdi
callq 0x3700e
testq %rax, %rax
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rbx
jne 0x38966
movq (%r15), %rdi
movq 0x8(%r15), %rsi
callq 0x2fc3b
movq 0x8(%r15), %rdi
callq 0x2ee1f
movq %r15, %rdi
callq 0x283cd
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1ff28
jmp 0x38823
movq %r14, %rdi
callq 0x1fdaa
jmp 0x387f6
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq