// File: /workspace/github_c_makefile_star2_O2/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:
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), %r15
leaq 0x7d3e(%rip), %r13      # 0x10438
cmpl $0x0, 0xd4(%rbx)
jne 0x88db
imull $0x3e8, (%r13), %edi    # imm = 0x3E8
callq 0x3600
movq %rbx, %rdi
callq 0x3650
movq 0xc0(%rbx), %rdi
callq 0x8b62
movq %rax, %r14
movq 0xb8(%rbx), %rax
movl 0x10(%rax), %r12d
movq %rbx, %rdi
callq 0x3320
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
callq 0x3650
movl 0xcc(%rbx), %eax
movl %eax, 0xc(%rsp)
movq %rbp, %rdi
callq 0x3320
cmpl $0x55555554, %r12d      # imm = 0x55555554
ja 0x888d
leaq 0x7cc3(%rip), %rax      # 0x10434
cmpl %r14d, (%rax)
ja 0x888d
movl %r12d, 0x8(%rsp)
movq %rbx, %rdi
callq 0x3650
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x3550
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0x35b0
xorl %ebp, %ebp
xorl %r14d, %r14d
leaq 0x10(%rsp), %r12
cmpl (%r13), %r14d
jge 0x8880
movq 0xb8(%rbx), %rax
movabsq $0x555555555555554, %rcx # imm = 0x555555555555554
cmpq %rcx, 0x10(%rax)
ja 0x8880
andq $0x0, 0x10(%rsp)
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
leaq -0x221(%rip), %rdx      # 0x85c2
movq %rbx, %rcx
callq 0x37a0
testl %eax, %eax
je 0x8801
leaq 0x7c46(%rip), %rax      # 0x1043c
cmpl (%rax), %ebp
jg 0x8817
incl %ebp
decl %r14d
jmp 0x8812
movq 0xb8(%rbx), %rdi
movq %r12, %rsi
callq 0x8b56
xorl %ebp, %ebp
incl %r14d
jmp 0x87a4
movq 0x77ba(%rip), %rax      # 0xffd8
movq (%rax), %rdi
movq %rax, %r12
xorl %ebp, %ebp
leaq 0x197e(%rip), %rsi      # 0xa1ab
leaq 0x3633(%rip), %rdx      # 0xbe67
leaq 0x35d3(%rip), %rcx      # 0xbe0e
movl $0x132, %r8d            # imm = 0x132
xorl %eax, %eax
callq 0x3610
movq (%r12), %rcx
leaq 0x35df(%rip), %rdi      # 0xbe32
pushq $0x19
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
callq 0x3800
movq 0x776f(%rip), %rax      # 0xffd8
movq (%rax), %rcx
leaq 0x3431(%rip), %rdi      # 0xbca4
pushq $0x2
popq %rsi
movq %r12, %rdx
callq 0x3800
jmp 0x8812
movq %rbx, %rdi
callq 0x3320
movl 0x8(%rsp), %r12d
movl 0xc(%rsp), %eax
addl %eax, %eax
cmpl %r12d, %eax
jae 0x86fa
cmpl %r12d, 0xc8(%rbx)
jae 0x86fa
movq %rbx, %rdi
callq 0x3650
movl (%r13), %eax
movl %eax, 0xd0(%rbx)
movq %rbx, %rdi
callq 0x3320
xorl %ebp, %ebp
cmpl (%r13), %ebp
jge 0x86fa
movq %r15, %rdi
callq 0x3040
incl %ebp
jmp 0x88c5
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq