// File: Barath-Kannan[P]SignalsAndSlots/src/details/WheeledThreadPool.cpp
// Function: BSignals::details::WheeledThreadPool::queueListener(unsigned int)
void WheeledThreadPool::queueListener(uint32_t index) {
    auto &spoke = threadPooledFunctions.getSpoke(index);
    std::function<void()> func;
    std::chrono::duration<double> waitTime = std::chrono::nanoseconds(1);
    const uint32_t wrap = threadPooledFunctions.size();
    auto wrapIncrementer = [wrap](uint32_t i){return (i+1 == wrap ? 0 : i+1);};
    
    while (isStarted){
        if (spoke.dequeue(func)){
            spoke.transferMaxToCache();
            if (func) func();
            waitTime = std::chrono::nanoseconds(1);
        }
        else{
            bool found = false;
            for (uint32_t i=wrapIncrementer(index); i != index; i=wrapIncrementer(i)){
                if (threadPooledFunctions.getSpoke(i).fastDequeue(func)){
                    found = true;
                    break;
                }
            }
            if (found){
                if (func) func();
                waitTime = std::chrono::nanoseconds(1);
            }
            else{
                std::this_thread::sleep_for(waitTime);
                waitTime*=2;
            }
        }
        if (waitTime > maxWait){
            spoke.blockingDequeue(func);
            if (func) func();
            waitTime = std::chrono::nanoseconds(1);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movl %edi, -0x4(%rbp)
movl -0x4(%rbp), %esi
movq 0xc2a0(%rip), %rdi      # 0x18fd8
callq 0xaa40
movq %rax, -0x10(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xacc0
movq 0x733f(%rip), %rax      # 0x14090
movq %rax, -0x38(%rbp)
movq 0xc27c(%rip), %rdi      # 0x18fd8
callq 0xad20
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
movq 0xc25f(%rip), %rax      # 0x18fd0
testb $0x1, (%rax)
je 0xcfa9
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0xa4f0
movb %al, -0xa5(%rbp)
jmp 0xcd8f
movb -0xa5(%rbp), %al
testb $0x1, %al
jne 0xcd9b
jmp 0xce0e
movq -0x10(%rbp), %rdi
callq 0xa080
jmp 0xcda6
leaq -0x30(%rbp), %rdi
callq 0xaf40
testb $0x1, %al
jne 0xcdb5
jmp 0xcddc
leaq -0x30(%rbp), %rdi
callq 0xa7c0
jmp 0xcdc0
jmp 0xcddc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xa2a0
jmp 0xcfbb
movl $0x1, -0x64(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x64(%rbp), %rsi
callq 0xa610
jmp 0xcdf2
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0xac30
jmp 0xce01
movq -0x58(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xcf16
movb $0x0, -0x65(%rbp)
movl -0x4(%rbp), %esi
leaq -0x40(%rbp), %rdi
callq 0xcff0
movl %eax, -0xac(%rbp)
jmp 0xce26
movl -0xac(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x4(%rbp), %eax
je 0xce8d
movl -0x6c(%rbp), %esi
movq 0xc197(%rip), %rdi      # 0x18fd8
callq 0xaa40
movq %rax, %rdi
leaq -0x30(%rbp), %rsi
callq 0xa2c0
movb %al, -0xad(%rbp)
jmp 0xce5a
movb -0xad(%rbp), %al
testb $0x1, %al
jne 0xce66
jmp 0xce6c
movb $0x1, -0x65(%rbp)
jmp 0xce8d
jmp 0xce6e
movl -0x6c(%rbp), %esi
leaq -0x40(%rbp), %rdi
callq 0xcff0
movl %eax, -0xb4(%rbp)
jmp 0xce82
movl -0xb4(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0xce2f
testb $0x1, -0x65(%rbp)
je 0xcee4
leaq -0x30(%rbp), %rdi
callq 0xaf40
testb $0x1, %al
jne 0xcea2
jmp 0xceaf
leaq -0x30(%rbp), %rdi
callq 0xa7c0
jmp 0xcead
jmp 0xceaf
movl $0x1, -0x84(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0xa610
jmp 0xcecb
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xac30
jmp 0xceda
movq -0x78(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xcf14
leaq -0x38(%rbp), %rdi
callq 0xa500
jmp 0xceef
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, -0x90(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xb170
jmp 0xcf12
jmp 0xcf14
jmp 0xcf16
movq 0xc0c3(%rip), %rsi      # 0x18fe0
leaq -0x38(%rbp), %rdi
callq 0xa620
movb %al, -0xb5(%rbp)
jmp 0xcf2e
movb -0xb5(%rbp), %al
testb $0x1, %al
jne 0xcf3a
jmp 0xcfa4
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0xb0f0
jmp 0xcf49
leaq -0x30(%rbp), %rdi
callq 0xaf40
testb $0x1, %al
jne 0xcf58
jmp 0xcf65
leaq -0x30(%rbp), %rdi
callq 0xa7c0
jmp 0xcf63
jmp 0xcf65
movl $0x1, -0xa4(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa4(%rbp), %rsi
callq 0xa610
jmp 0xcf84
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xac30
jmp 0xcf99
movq -0x98(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xcd6a
leaq -0x30(%rbp), %rdi
callq 0xa2a0
addq $0xc0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0xaf80
nopw %cs:(%rax,%rax)