// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x20(%rdi), %rax
leaq 0x10(%rsp), %r14
movq %rax, (%r14)
xorl %r15d, %r15d
movb %r15b, 0x8(%r14)
movq %r14, %rdi
callq 0x450c
movb $0x1, 0x8(%r14)
leaq 0x70(%rbx), %rax
movq %rsp, %rdi
movq %rax, (%rdi)
movb %r15b, 0x8(%rdi)
callq 0x450c
movb $0x1, 0x8(%rsp)
movq 0xa8(%rbx), %rax
cmpq %rax, 0xc8(%rbx)
je 0x40eb
leaq 0x98(%rbx), %r14
movups (%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
movq %r14, %rdi
callq 0x4548
movq %rsp, %rdi
callq 0x4202
leaq 0x10(%rsp), %rdi
callq 0x4202
leaq 0xe8(%rbx), %r15
leaq 0x30(%rsp), %r12
movq %rsp, %r13
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
cmpq $0x0, 0xf8(%rbx)
je 0x4121
movq %r15, %rdi
movq %r12, %rsi
callq *0x100(%rbx)
movq %r13, %rdi
callq 0x450c
movq 0xa8(%rbx), %rax
cmpq %rax, 0xc8(%rbx)
je 0x4072
movups (%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
movq %r14, %rdi
callq 0x4548
movq %r13, %rdi
callq 0x4202
movaps 0x20(%rsp), %xmm0
jmp 0x401c
movq %rsp, %rdi
callq 0x4202
leaq 0x10(%rsp), %rdi
callq 0x450c
addq $0x48, %rbx
movslq %ebp, %rsi
movq %rbx, %rdi
callq 0x4240
notq %rdx
andq %rdx, (%rax)
leaq 0x10(%rsp), %rdi
callq 0x4202
movq 0x3f26(%rip), %rbx      # 0x7fd0
leaq 0x1230(%rip), %rsi      # 0x52e1
movl $0x11, %edx
movq %rbx, %rdi
callq 0x2250
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x3ef4(%rip), %rdi      # 0x7fd0
callq 0x2040
movq %rax, %rdi
callq 0x2160
jmp 0x4100
addq $0x48, %rbx
movslq %ebp, %rsi
movq %rbx, %rdi
callq 0x4240
notq %rdx
andq %rdx, (%rax)
movq %rsp, %rdi
callq 0x422a
leaq 0x10(%rsp), %rdi
callq 0x422a
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x20c0
jmp 0x4131
jmp 0x4131
movq %rax, %rbx
jmp 0x413c
jmp 0x4131
movq %rax, %rbx
movq %rsp, %rdi
callq 0x422a
leaq 0x10(%rsp), %rdi
callq 0x422a
movq %rbx, %rdi
callq 0x2350