// File: miklhh[P]mandelbrot/src/thread_pool.cpp
// Function: mandelbrot_thread_pool_thread_callback(mandelbrot_threadpool*, int)
static void mandelbrot_thread_pool_thread_callback(
    mandelbrot_threadpool* threadpool,
    int thread_index
)
{
    /* Test if there is any job to do. Someone might have snatched our job. */
    std::unique_lock<std::mutex> worker_busy_lock(threadpool->worker_busy_mutex);
    std::unique_lock<std::mutex> jobs_lock(threadpool->jobs_mutex);
    if (threadpool->jobs.empty())
    {
        /* Another thread has snated out work. Return. */
        threadpool->worker_busy.at(thread_index) = false;
        return;
    }
    else
    {
        /* Get the job that needs to be done and start working the jobs.*/
        segment_t current_segment = threadpool->jobs.front();
        threadpool->jobs.pop();
        jobs_lock.unlock();
        worker_busy_lock.unlock();
        while (1) 
        {
            /* Render the current segment and try to get a new job.*/
            threadpool->render_segment(current_segment);
            jobs_lock.lock();
            if (threadpool->jobs.empty())
            {
                /* No more jobs. Return this thread. */
                jobs_lock.unlock();
                worker_busy_lock.lock();
                threadpool->worker_busy.at(thread_index) = false;
                worker_busy_lock.unlock();
                std::cout << "Thread Returning." << std::endl;     // DEBUGING.
                return;
            }
            else
            {
                /* More jobs to be done! */
                current_segment = threadpool->jobs.front();
                threadpool->jobs.pop();
                jobs_lock.unlock();
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rsi
addq $0x20, %rsi
leaq -0x20(%rbp), %rdi
callq 0x8140
movq -0x8(%rbp), %rsi
addq $0x70, %rsi
leaq -0x30(%rbp), %rdi
callq 0x8140
jmp 0x7c06
movq -0x8(%rbp), %rdi
addq $0x98, %rdi
callq 0x96a0
movb %al, -0x91(%rbp)
jmp 0x7c1e
movb -0x91(%rbp), %al
testb $0x1, %al
jne 0x7c2a
jmp 0x7ca3
movq -0x8(%rbp), %rdi
addq $0x48, %rdi
movslq -0xc(%rbp), %rsi
callq 0x83d0
movq %rdx, -0xa8(%rbp)
movq %rax, -0xa0(%rbp)
jmp 0x7c4b
movq -0xa8(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq %rax, -0x48(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x9540
movl $0x1, -0x54(%rbp)
jmp 0x7e3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x7e56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x8200
jmp 0x7e56
movq -0x8(%rbp), %rdi
addq $0x98, %rdi
callq 0x96c0
movq %rax, -0xb0(%rbp)
jmp 0x7cbc
movq -0xb0(%rbp), %rax
movups (%rax), %xmm0
movaps %xmm0, -0x70(%rbp)
movq -0x8(%rbp), %rdi
addq $0x98, %rdi
callq 0x96e0
jmp 0x7cdc
leaq -0x30(%rbp), %rdi
callq 0x81b0
jmp 0x7ce7
leaq -0x20(%rbp), %rdi
callq 0x81b0
jmp 0x7cf2
jmp 0x7cf4
jmp 0x7cf6
movq -0x8(%rbp), %rdi
addq $0xe8, %rdi
movaps -0x70(%rbp), %xmm0
movaps %xmm0, -0x80(%rbp)
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
callq 0x9700
jmp 0x7d18
leaq -0x30(%rbp), %rdi
callq 0x9740
jmp 0x7d23
movq -0x8(%rbp), %rdi
addq $0x98, %rdi
callq 0x96a0
movb %al, -0xb1(%rbp)
jmp 0x7d3b
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x7d4a
jmp 0x7df0
leaq -0x30(%rbp), %rdi
callq 0x81b0
jmp 0x7d55
leaq -0x20(%rbp), %rdi
callq 0x9740
jmp 0x7d60
movq -0x8(%rbp), %rdi
addq $0x48, %rdi
movslq -0xc(%rbp), %rsi
callq 0x83d0
movq %rdx, -0xc8(%rbp)
movq %rax, -0xc0(%rbp)
jmp 0x7d81
movq -0xc8(%rbp), %rax
movq -0xc0(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movq %rax, -0x88(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x9540
leaq -0x20(%rbp), %rdi
callq 0x81b0
jmp 0x7db6
movq 0xd213(%rip), %rdi      # 0x14fd0
leaq 0x64cd(%rip), %rsi      # 0xe291
callq 0x3260
movq %rax, -0xd0(%rbp)
jmp 0x7dd2
movq -0xd0(%rbp), %rdi
movq 0xd1d8(%rip), %rsi      # 0x14fb8
callq 0x3290
jmp 0x7de7
movl $0x1, -0x54(%rbp)
jmp 0x7e3b
movq -0x8(%rbp), %rdi
addq $0x98, %rdi
callq 0x96c0
movq %rax, -0xd8(%rbp)
jmp 0x7e09
movq -0xd8(%rbp), %rax
movups (%rax), %xmm0
movaps %xmm0, -0x70(%rbp)
movq -0x8(%rbp), %rdi
addq $0x98, %rdi
callq 0x96e0
jmp 0x7e29
leaq -0x30(%rbp), %rdi
callq 0x81b0
jmp 0x7e34
jmp 0x7e36
jmp 0x7cf4
leaq -0x30(%rbp), %rdi
callq 0x8200
leaq -0x20(%rbp), %rdi
callq 0x8200
addq $0xe0, %rsp
popq %rbp
retq
leaq -0x20(%rbp), %rdi
callq 0x8200
movq -0x38(%rbp), %rdi
callq 0x33e0
nopl (%rax,%rax)