// File: /workspace/github_c_makefile_star2_O1/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 0x8ee06(%rip), %rbx     # 0xc9fa8
movq 0x740(%rbx), %r14
cmpq $0x0, 0x28(%r14)
je 0x3b368
leaq 0x28(%r14), %r12
callq 0x2038c
movq %rax, 0x740(%rbx)
cmpq $0x0, 0x28(%r14)
je 0x3b343
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 0x225d3
movq (%r15), %rdi
movq 0x8(%r15), %rsi
callq 0x31454
testq %rax, %rax
je 0x3b31c
movl (%rax), %ecx
andl $0xf, %ecx
cmpl $0x1, %ecx
jne 0x3b31c
movq %rax, (%rsp)
movq %rbx, 0x18(%rsp)
movq (%r15), %rax
movq 0x8(%r15), %rsi
movq 0x10(%rax), %rdi
callq 0x224dc
testq %rax, %rax
je 0x3b2f3
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x28(%rax), %r12d
incl %r12d
decl %r12d
je 0x3b2f3
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %r14
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x3b276
xorl %ebp, %ebp
leaq 0x10b(%rip), %rcx       # 0x3b377
cmpq %rcx, 0x8(%rax)
setne %bpl
jmp 0x3b27b
movl $0x1, %ebp
movq (%rsp), %rdi
movq 0x130(%r14), %rbx
movl %ebp, %esi
callq 0x397fd
testq %rax, %rax
je 0x3b2f3
movq %rax, %r13
testq %rbx, %rbx
je 0x3b2a2
movq %rbx, %rdi
callq 0x3041b
movq %r14, %rdi
callq 0x60e97
movq (%r15), %rcx
movq 0x8(%r15), %rsi
movq %r14, %rdi
movq %rbx, %rdx
movq %r13, %r8
movl %ebp, %r9d
callq 0x3b011
cmpl $-0x1, %eax
jne 0x3b2d5
movq (%rsp), %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x39719
testq %rbx, %rbx
je 0x3b2e2
movq %rbx, %rdi
callq 0x30834
movq %r13, %rdi
callq 0x30834
testq %r13, %r13
jne 0x3b245
movq (%rsp), %rdi
callq 0x398e9
testq %rax, %rax
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r12
movq 0x18(%rsp), %rbx
jne 0x3b31c
movq (%r15), %rdi
movq 0x8(%r15), %rsi
callq 0x3174b
movq 0x8(%r15), %rdi
callq 0x30834
movq %r15, %rdi
callq 0x2984b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x20544
cmpq $0x0, (%r12)
jne 0x3b1d9
movq %r14, %rdi
callq 0x203b0
leaq 0x8ec56(%rip), %rbx     # 0xc9fa8
movq 0x740(%rbx), %r14
leaq 0x28(%r14), %r12
cmpq $0x0, 0x28(%r14)
jne 0x3b1b8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq