// File: /workspace/github_c_makefile_star2_O1/sszllzss[P]webSocket_Libevent_ThreadPool/Threadpool.cpp
// Function: adjust_thread(void*)
void *adjust_thread(void *threadpool)
{
    int i;
    threadpool_t *pool = (threadpool_t *)threadpool;
    while (!pool->shutdown) {

        usleep(DEFAULT_TIME*1000);                                    /*定时 对线程池管理*/

        pthread_mutex_lock(&(pool->lock));
        unsigned int queue_size = pool->threadpool_task_queue->size();                      /* 关注 任务数 */
        unsigned int live_thr_num = pool->threads->size();                  /* 存活 线程数 */
        unsigned int max_size_th = pool->threads->max_size();
        pthread_mutex_unlock(&(pool->lock));

        pthread_mutex_lock(&(pool->thread_counter));
        unsigned int busy_thr_num = pool->busy_thr_num;                  /* 忙着的线程数 */
        pthread_mutex_unlock(&(pool->thread_counter));
        /* 创建新线程 算法： 任务数大于最小线程池个数, 且存活的线程数少于最大线程个数时 如：30>=10 && 40<100*/
        if (queue_size >= MIN_WAIT_TASK_NUM && live_thr_num < max_size_th) {

            pthread_mutex_lock(&(pool->lock));  
            int add = 0;
            pthread_attr_t attr;
            pthread_attr_init(&attr);
            pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//设置线程分离
            /*一次增加 DEFAULT_THREAD 个线程*/
            for(i=0;i<DEFAULT_THREAD_VARY
                &&pool->threads->size() < pool->threads->max_size();i++)
            {
                pthread_t pid = 0;
                int ret = pthread_create( &pid, &attr,threadpool_thread, (void*)pool );     
                if(ret != 0)
                {
                    if(add<=TH_CREATE_RETRY_NUM)
                    {
                        add++;
                        i--;//i可以减为 -1 -1++ = 0
                    }
                    else
                    {
                        add = 0;
                        De_fprintf(stderr,"new pthread_list_t fail\r\n");
                    }
                }
                else
                {
                    add = 0;
                    pool->threads->push_back(pid);
                }
            }

            pthread_mutex_unlock(&(pool->lock));

        }

        /* 销毁多余的空闲线程 算法：忙线程X2 小于 存活的线程数 且 存活的线程数 大于 最小线程数时*/
        if ((busy_thr_num * 2) < live_thr_num  &&  live_thr_num > pool->min_thr_num) {

            /* 一次销毁DEFAULT_THREAD个线程, 隨機10個即可 */
            pthread_mutex_lock(&(pool->lock));
            pool->wait_exit_thr_num = DEFAULT_THREAD_VARY;      /* 要销毁的线程数 设置为10 */
            pthread_mutex_unlock(&(pool->lock));

            for (i = 0; i < DEFAULT_THREAD_VARY; i++) {
                /* 通知处在空闲状态的线程, 他们会自行终止*/
                pthread_cond_signal(&(pool->queue_not_empty));

            }

        }

    }

    return NULL;

}

// Assembly:
cmpl $0x0, 0xd4(%rdi)
jne 0x918a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
leaq 0x28(%rdi), %rax
movq %rax, 0x18(%rsp)
leaq 0x80(%rdi), %r14
leaq 0x847a(%rip), %r13      # 0x11420
movq %r14, 0x10(%rsp)
imull $0x3e8, (%r13), %edi    # imm = 0x3E8
callq 0x35d0
movq %rbx, %rdi
callq 0x3620
movq 0xc0(%rbx), %rax
movq 0x48(%rax), %rcx
movl 0x28(%rax), %edx
movl %ecx, %r14d
subl %edx, %r14d
shrl $0x3, %r14d
cmpq $0x1, %rcx
adcl $-0x1, %r14d
movq 0xb8(%rbx), %rcx
movq 0x30(%rax), %r15
subq 0x38(%rax), %r15
movq 0x20(%rax), %rbp
subq 0x10(%rax), %rbp
movl 0x10(%rcx), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
callq 0x3310
movq %r13, %r12
movq 0x18(%rsp), %r13
movq %r13, %rdi
callq 0x3620
movl 0xcc(%rbx), %eax
movl %eax, 0x4(%rsp)
movq %r13, %rdi
movq %r12, %r13
callq 0x3310
movl (%rsp), %ecx
cmpl $0x55555554, %ecx       # imm = 0x55555554
ja 0x9125
shll $0x5, %r14d
shrq $0x4, %r15
shrq $0x4, %rbp
addl %r15d, %ebp
addl %r14d, %ebp
leaq 0x83ca(%rip), %rax      # 0x1141c
cmpl %ebp, (%rax)
ja 0x9125
movq %rbx, %rdi
callq 0x3620
leaq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x3530
movq %r12, %rdi
movl $0x1, %esi
callq 0x3580
cmpl $0x0, (%r13)
movabsq $0x555555555555554, %rbp # imm = 0x555555555555554
jle 0x911a
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0xb8(%rbx), %rax
cmpq %rbp, 0x10(%rax)
ja 0x911a
movq $0x0, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
leaq -0x284(%rip), %rdx      # 0x8e38
movq %rbx, %rcx
callq 0x3770
testl %eax, %eax
je 0x90dc
leaq 0x8355(%rip), %rax      # 0x11424
cmpl (%rax), %r14d
jg 0x9113
incl %r14d
decl %r15d
jmp 0x9108
movq 0xb8(%rbx), %r14
movl $0x18, %edi
callq 0x3440
movq 0x8(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x3090
incq 0x10(%r14)
xorl %r14d, %r14d
incl %r15d
cmpl (%r13), %r15d
jl 0x9097
jmp 0x911a
callq 0x4003
jmp 0x9105
movq %rbx, %rdi
callq 0x3310
movl (%rsp), %ecx
movl 0x4(%rsp), %eax
addl %eax, %eax
cmpl %ecx, %eax
movq 0x10(%rsp), %r14
jae 0x916f
cmpl %ecx, 0xc8(%rbx)
jae 0x916f
movq %rbx, %rdi
callq 0x3620
movl (%r13), %eax
movl %eax, 0xd0(%rbx)
movq %rbx, %rdi
callq 0x3310
cmpl $0x0, (%r13)
jle 0x916f
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x3040
incl %ebp
cmpl (%r13), %ebp
jl 0x915f
cmpl $0x0, 0xd4(%rbx)
je 0x8fab
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
xorl %eax, %eax
retq