// File: /workspace/github_c_makefile_star2_O3/sszllzss[P]webSocket_Libevent_ThreadPool/Threadpool.cpp
// Function: threadpool_create(int)
threadpool_t *threadpool_create(int min_thr_num)
{
    threadpool_t *pool=NULL;
    int i;
    do{
        /*  开辟threadpool结构内存空间   */
        if((pool = (threadpool_t *) malloc(sizeof(threadpool_t)))==NULL)
        {
            De_perror("malloci thrread fail");
            break;
        }
        /* 清空线程池结构体 */
        memset(pool, 0, sizeof(threadpool_t));

        pool->min_thr_num = min_thr_num;//线程最小值
        pool->shutdown = false;//不关闭线程
        /* 创建线程列表 */
        pool->threads = new pthread_list_t();
        if(pool->threads == NULL)
        {
            De_fprintf(stderr,"new pthread_list_t fail\r\n");
            break;
        }

        /* 创建任务队列 */
        pool->threadpool_task_queue = new thread_pool_task_queue_t();
        if(pool->threadpool_task_queue == NULL)
        {
            De_fprintf(stderr,"new threadpool_task_queue_t fail\r\n");
            break;
        }
        /* 初始化互斥琐、条件变量 */
        if (pthread_mutex_init(&(pool->lock), NULL) != 0
            || pthread_mutex_init(&(pool->thread_counter), NULL) != 0
            || pthread_cond_init(&(pool->queue_not_empty), NULL) != 0
            || pthread_cond_init(&(pool->queue_not_full), NULL) != 0)
        {
            De_fprintf(stderr,"init the lock or cond fail");
            break;
        }
        /* 启动 min_thr_num 个 work thread */
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//设置线程分离
        for (i = 0; i < min_thr_num; i++) {
            pthread_t pid=0;
            int ret = pthread_create(&pid, &attr, threadpool_thread, (void *)pool);/*pool指向当前线程池*/
            if(ret !=0)
            {
                De_fprintf(stderr, "creatd thread fail:%s\r\n", strerror(ret));
                break;
            }
            pool->threads->push_back(pid); 
            /* De_printf("start thread 0x%x...\n", (unsigned int)pid); */
        }
        pthread_attr_destroy(&attr);
        if(i!=min_thr_num)
            break;
        pthread_create(&(pool->adjust_tid), NULL, adjust_thread, (void *)pool);/* 启动管理者线程 */
        return pool;
    }while(0);
    threadpool_destroy(pool);

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edi, %r13d
movl $0x1, %edi
movl $0xd8, %esi
callq 0x32b0
movq %rax, %rbx
testq %rax, %rax
je 0x8d2d
movl %r13d, 0xc8(%rbx)
movl $0x18, %edi
callq 0x3440
movq %rax, 0x8(%rax)
movq %rax, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, 0xb8(%rbx)
movl $0x50, %edi
callq 0x3440
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x40(%rax)
movq %rax, %rdi
xorl %esi, %esi
callq 0x949e
movq %r14, 0xc0(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3630
testl %eax, %eax
jne 0x8d26
leaq 0x28(%rbx), %rdi
xorl %esi, %esi
callq 0x3630
testl %eax, %eax
jne 0x8d26
leaq 0x80(%rbx), %rdi
xorl %esi, %esi
callq 0x3510
testl %eax, %eax
jne 0x8d26
leaq 0x50(%rbx), %rdi
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x3510
testl %eax, %eax
jne 0x8d26
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x3530
movq %r14, %rdi
movl $0x1, %esi
callq 0x3580
testl %r13d, %r13d
jle 0x8d40
xorl %ebp, %ebp
leaq 0x8(%rsp), %r15
leaq 0x10(%rsp), %r12
movq $0x0, 0x8(%rsp)
movq %r15, %rdi
movq %r12, %rsi
leaq 0xc8(%rip), %rdx        # 0x8da0
movq %rbx, %rcx
callq 0x3770
testl %eax, %eax
jne 0x8d34
movl %r13d, %r14d
movq 0xb8(%rbx), %r13
movl $0x18, %edi
callq 0x3440
movq 0x8(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0x3090
incq 0x10(%r13)
movl %r14d, %r13d
incl %ebp
cmpl %ebp, %r14d
jne 0x8cc2
leaq 0x10(%rsp), %rdi
callq 0x36d0
jmp 0x8d4f
callq 0x3fc8
jmp 0x8d6c
callq 0x4094
jmp 0x8d6c
leaq 0x8(%rsp), %rsi
movl %eax, %edi
callq 0x402a
leaq 0x10(%rsp), %rdi
callq 0x36d0
cmpl %r13d, %ebp
jne 0x8d6c
movq %rbx, %rdi
addq $0xb0, %rdi
leaq 0x17d(%rip), %rdx       # 0x8edd
xorl %esi, %esi
movq %rbx, %rcx
callq 0x3770
jmp 0x8d76
movq %rbx, %rdi
callq 0x90f9
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x50, %esi
movq %r14, %rdi
callq 0x3460
movq %rbx, %rdi
callq 0x3740