// File: /workspace/github_c_makefile_star2_O1/sszllzss[P]webSocket_Libevent_ThreadPool/Threadpool.cpp
// Function: threadpool_thread(void*)
void *threadpool_thread(void *threadpool)
{
    threadpool_t *pool = (threadpool_t *)threadpool;
    threadpool_task_t task;

    while (true) {
        /*刚创建出线程，等待任务队列里有任务，否则阻塞等待任务队列里有任务后再唤醒接收任务*/
        pthread_mutex_lock(&(pool->lock));

        /*queue_size == 0 说明没有任务，调 wait 阻塞在条件变量上, 若有任务，跳过该while*/
        while ((pool->threadpool_task_queue->size() == 0) && (!pool->shutdown)) {  
            /* De_printf("thread 0x%x is waiting\n", (unsigned int)pthread_self()); */
            pthread_cond_wait(&(pool->queue_not_empty), &(pool->lock));

            /*清除指定数目的空闲线程，如果要结束的线程个数大于0，结束线程*/
            if (pool->wait_exit_thr_num > 0) {
                pool->wait_exit_thr_num--;

                /*如果线程池里线程个数大于最小值时可以结束当前线程*/
                if ((unsigned)pool->threads->size() > pool->min_thr_num) {
                    /* De_printf("thread 0x%x is exiting\n", (unsigned int)pthread_self()); */
                    pool->threads->remove(pthread_self());
                    pthread_mutex_unlock(&(pool->lock));
                    pthread_exit(NULL);

                }

            }
        }

        /*如果指定了true，要关闭线程池里的每个线程，自行退出处理*/
        if (pool->shutdown) {
            pool->threads->remove(pthread_self());
            pthread_mutex_unlock(&(pool->lock));
            /* De_printf("thread 0x%x is exiting\n", (unsigned int)pthread_self()); */
            pthread_exit(NULL);     /* 线程自行结束 */

        }

        /*从任务队列里获取任务, 是一个出队操作*/
        thread_pool_task_queue_t *task_queue; 
        task_queue = pool->threadpool_task_queue;
        task.function = (*task_queue->begin()).function;
        task.arg = (*task_queue->begin()).arg;
        task_queue->pop_front();

        /*通知可以有新的任务添加进来*/
        pthread_cond_broadcast(&(pool->queue_not_full));

        /*任务取出后，立即将 线程池琐 释放*/
        pthread_mutex_unlock(&(pool->lock));

        /*执行任务*/ 
        /* De_printf("thread 0x%x start working\n", (unsigned int)pthread_self()); */
        pthread_mutex_lock(&(pool->thread_counter));                            /*忙状态线程数变量琐*/
        pool->busy_thr_num++;                                                   /*忙状态线程数+1*/
        pthread_mutex_unlock(&(pool->thread_counter));
        (*(task.function))(task.arg);                                           /*执行回调函数任务*/
        //task.function(task.arg);                                              /*执行回调函数任务*/
        //
        //        /*任务结束处理*/ 
        /* De_printf("thread 0x%x end working\n", (unsigned int)pthread_self()); */
        pthread_mutex_lock(&(pool->thread_counter));
        pool->busy_thr_num--;                                       /*处理掉一个任务，忙状态数线程数-1*/
        pthread_mutex_unlock(&(pool->thread_counter));

    }

    pthread_exit(NULL);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x80(%rdi), %r14
leaq 0x50(%rdi), %r15
leaq 0x28(%rdi), %r12
movq %rbx, %rdi
callq 0x3620
movq 0xc0(%rbx), %rdi
movq 0x48(%rdi), %rax
movq %rax, %rcx
subq 0x28(%rdi), %rcx
shrq $0x3, %rcx
cmpq $0x1, %rax
adcq $-0x1, %rcx
movq 0x30(%rdi), %rdx
subq 0x38(%rdi), %rdx
shlq $0x5, %rcx
sarq $0x4, %rdx
addq %rcx, %rdx
movq 0x10(%rdi), %rax
movq 0x20(%rdi), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
addq %rdx, %rcx
jne 0x8edd
cmpl $0x0, 0xd4(%rbx)
jne 0x8edd
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3660
movl 0xd0(%rbx), %eax
testl %eax, %eax
je 0x8e5d
decl %eax
movl %eax, 0xd0(%rbx)
movq 0xb8(%rbx), %r13
movl 0xc8(%rbx), %eax
cmpl 0x10(%r13), %eax
jae 0x8e5d
jmp 0x8f38
cmpl $0x0, 0xd4(%rbx)
jne 0x8f5a
movq (%rax), %rbp
movq 0x8(%rax), %r13
callq 0x94f2
movq %r15, %rdi
callq 0x3180
movq %rbx, %rdi
callq 0x3310
movq %r12, %rdi
callq 0x3620
incl 0xcc(%rbx)
movq %r12, %rdi
callq 0x3310
movq %r13, %rdi
callq *%rbp
movq %r12, %rdi
callq 0x3620
decl 0xcc(%rbx)
movq %r12, %rdi
callq 0x3310
jmp 0x8e55
callq 0x33a0
movq %rsp, %rsi
movq %rax, (%rsi)
movq %r13, %rdi
callq 0x9460
movq %rbx, %rdi
callq 0x3310
xorl %edi, %edi
callq 0x3750
movq 0xb8(%rbx), %r14
callq 0x33a0
movq %rsp, %rsi
movq %rax, (%rsi)
movq %r14, %rdi
jmp 0x8f46