// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movl %esi, %r15d
movq %rdi, %rbx
leaq 0x20(%rdi), %rax
leaq 0x18(%rsp), %r14
movq %rax, (%r14)
xorl %ebp, %ebp
movb %bpl, 0x8(%r14)
movq %r14, %rdi
callq 0x4620
movb $0x1, 0x8(%r14)
leaq 0x70(%rbx), %rax
leaq 0x8(%rsp), %rdi
movq %rax, (%rdi)
movb %bpl, 0x8(%rdi)
callq 0x4620
movb $0x1, 0x10(%rsp)
movq 0xa8(%rbx), %rax
cmpq %rax, 0xc8(%rbx)
je 0x41e2
movl %r15d, 0x4(%rsp)
leaq 0x98(%rbx), %r14
movq (%rax), %rbp
movq 0x8(%rax), %r12
movq %r14, %rdi
callq 0x465c
leaq 0x8(%rsp), %rdi
callq 0x4306
leaq 0x18(%rsp), %rdi
callq 0x4306
leaq 0xe8(%rbx), %r15
leaq 0x8(%rsp), %r13
movq %rbp, 0x28(%rsp)
movq %r12, 0x30(%rsp)
cmpq $0x0, 0xf8(%rbx)
je 0x421a
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
callq *0x100(%rbx)
movq %r13, %rdi
callq 0x4620
movq 0xa8(%rbx), %rax
cmpq %rax, 0xc8(%rbx)
je 0x4165
movq (%rax), %rbp
movq 0x8(%rax), %r12
movq %r14, %rdi
callq 0x465c
movq %r13, %rdi
callq 0x4306
jmp 0x410e
leaq 0x8(%rsp), %rdi
callq 0x4306
leaq 0x18(%rsp), %rdi
callq 0x4620
addq $0x48, %rbx
movslq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x4344
notq %rdx
andq %rdx, (%rax)
leaq 0x18(%rsp), %rdi
callq 0x4306
movq 0x3e2f(%rip), %rbx      # 0x7fd0
leaq 0x1101(%rip), %rsi      # 0x52a9
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 0x3dfd(%rip), %rdi      # 0x7fd0
callq 0x2040
movq %rax, %rdi
callq 0x2160
jmp 0x41f7
addq $0x48, %rbx
movslq %r15d, %rsi
movq %rbx, %rdi
callq 0x4344
notq %rdx
andq %rdx, (%rax)
leaq 0x8(%rsp), %rdi
callq 0x432e
leaq 0x18(%rsp), %rdi
callq 0x432e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x20c0
jmp 0x422a
jmp 0x422a
movq %rax, %rbx
jmp 0x4237
jmp 0x422a
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x432e
leaq 0x18(%rsp), %rdi
callq 0x432e
movq %rbx, %rdi
callq 0x2350
nop