// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0xd4(%rax)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xf890
jmp 0xfb12
leaq 0xdc19(%rip), %rax      # 0x1d4b0
imull $0x3e8, (%rax), %edi    # imm = 0x3E8
callq 0x46d0
movq -0x18(%rbp), %rdi
callq 0x4720
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rdi
callq 0xffd0
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rdi
callq 0x10060
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rdi
callq 0x10230
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4390
movq -0x18(%rbp), %rdi
addq $0x28, %rdi
callq 0x4720
movq -0x18(%rbp), %rax
movl 0xcc(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
addq $0x28, %rdi
callq 0x4390
movl -0x1c(%rbp), %eax
leaq 0xdb8e(%rip), %rcx      # 0x1d4ac
cmpl (%rcx), %eax
jb 0xfa9d
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0xfa9d
movq -0x18(%rbp), %rdi
callq 0x4720
movl $0x0, -0x2c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x4620
leaq -0x68(%rbp), %rdi
movl $0x1, %esi
callq 0x4680
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %ecx
xorl %eax, %eax
leaq 0xdb44(%rip), %rdx      # 0x1d4b0
cmpl (%rdx), %ecx
movb %al, -0x75(%rbp)
jge 0xf9a7
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rdi
callq 0x10060
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rdi
callq 0x10230
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
setb %al
movb %al, -0x75(%rbp)
movb -0x75(%rbp), %al
testb $0x1, %al
jne 0xf9b3
jmp 0xfa94
movq $0x0, -0x70(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x70(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x37e(%rip), %rdx      # 0xf650
callq 0x4880
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0xfa69
movl -0x2c(%rbp), %eax
leaq 0xdaca(%rip), %rcx      # 0x1d4b4
cmpl (%rcx), %eax
jg 0xfa02
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0xc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xfa67
movl $0x0, -0x2c(%rbp)
movq 0xd5c8(%rip), %rax      # 0x1cfd8
movq (%rax), %rdi
leaq 0x4781(%rip), %rsi      # 0x1419b
leaq 0x649c(%rip), %rdx      # 0x15ebd
leaq 0x6419(%rip), %rcx      # 0x15e41
movl $0x132, %r8d            # imm = 0x132
movb $0x0, %al
callq 0x46e0
movq 0xd59c(%rip), %rax      # 0x1cfd8
movq (%rax), %rdi
leaq 0x641f(%rip), %rsi      # 0x15e65
movb $0x0, %al
callq 0x46e0
movq 0xd584(%rip), %rax      # 0x1cfd8
movq (%rax), %rdi
leaq 0x628b(%rip), %rsi      # 0x15ce9
movb $0x0, %al
callq 0x46e0
jmp 0xfa67
jmp 0xfa84
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rdi
leaq -0x70(%rbp), %rsi
callq 0xff60
jmp 0xfa86
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xf960
movq -0x18(%rbp), %rdi
callq 0x4390
movl -0x28(%rbp), %eax
shll %eax
cmpl -0x20(%rbp), %eax
jae 0xfb0d
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0xc8(%rcx), %eax
jbe 0xfb0d
movq -0x18(%rbp), %rdi
callq 0x4720
leaq 0xd9ea(%rip), %rax      # 0x1d4b0
movl (%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0xd0(%rax)
movq -0x18(%rbp), %rdi
callq 0x4390
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
leaq 0xd9c4(%rip), %rcx      # 0x1d4b0
cmpl (%rcx), %eax
jge 0xfb0b
movq -0x18(%rbp), %rdi
addq $0x80, %rdi
callq 0x4040
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xfae2
jmp 0xfb0d
jmp 0xf877
xorl %eax, %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)