// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x20(%rdi), %rsi
leaq 0x18(%rsp), %rdi
callq 0x3eee
leaq 0x70(%rbx), %rsi
leaq 0x8(%rsp), %rdi
callq 0x3eee
movq 0xa8(%rbx), %rax
cmpq %rax, 0xc8(%rbx)
je 0x3d73
movl %ebp, 0x4(%rsp)
leaq 0x98(%rbx), %r14
movq (%rax), %r15
movq 0x8(%rax), %r12
movq %r14, %rdi
callq 0x43f8
leaq 0x8(%rsp), %rdi
callq 0x3f04
leaq 0x18(%rsp), %rdi
callq 0x3f04
leaq 0xe8(%rbx), %r13
leaq 0x8(%rsp), %rbp
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x43a6
movq %rbp, %rdi
callq 0x43ce
movq 0xa8(%rbx), %rax
cmpq %rax, 0xc8(%rbx)
je 0x3d21
movq (%rax), %r15
movq 0x8(%rax), %r12
movq %r14, %rdi
callq 0x43f8
movq %rbp, %rdi
callq 0x3f04
jmp 0x3ce2
leaq 0x8(%rsp), %rdi
callq 0x3f04
leaq 0x18(%rsp), %rdi
callq 0x43ce
addq $0x48, %rbx
movslq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x3fea
notq %rdx
andq %rdx, (%rax)
leaq 0x18(%rsp), %rdi
callq 0x3f04
movq 0x5273(%rip), %rdi      # 0x8fd0
leaq 0x157d(%rip), %rsi      # 0x52e1
callq 0x2210
movq %rax, %rdi
callq 0x2120
jmp 0x3d88
addq $0x48, %rbx
movslq %ebp, %rsi
movq %rbx, %rdi
callq 0x3fea
notq %rdx
andq %rdx, (%rax)
leaq 0x8(%rsp), %rdi
callq 0x3f2a
leaq 0x18(%rsp), %rdi
callq 0x3f2a
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3db6
jmp 0x3db6
movq %rax, %rbx
jmp 0x3dc3
jmp 0x3db6
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3f2a
leaq 0x18(%rsp), %rdi
callq 0x3f2a
movq %rbx, %rdi
callq 0x2330
nop