// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movl $0xd8, %edi
callq 0x35a0
movq %rax, %rbx
testq %rax, %rax
je 0x8dff
movl $0xd8, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3250
movl %ebp, 0xc8(%rbx)
movl $0x0, 0xd4(%rbx)
movl $0x18, %edi
callq 0x3440
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, 0x8(%rax)
movq %rax, (%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 0x9540
movq %r14, 0xc0(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3630
testl %eax, %eax
jne 0x8df8
leaq 0x28(%rbx), %rdi
xorl %esi, %esi
callq 0x3630
testl %eax, %eax
jne 0x8df8
leaq 0x80(%rbx), %rdi
xorl %esi, %esi
callq 0x3510
testl %eax, %eax
jne 0x8df8
leaq 0x50(%rbx), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x3510
testl %eax, %eax
jne 0x8df8
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x3530
movq %r15, %rdi
movl $0x1, %esi
callq 0x3580
testl %ebp, %ebp
jle 0x8dcc
xorl %r14d, %r14d
leaq 0x10(%rsp), %r12
movq $0x0, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
leaq 0xc6(%rip), %rdx        # 0x8e38
movq %rbx, %rcx
callq 0x3770
movl %eax, %r13d
testl %eax, %eax
jne 0x8dbf
movl %ebp, %r15d
movq 0xb8(%rbx), %rbp
movl $0x18, %edi
callq 0x3440
movq 0x8(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3090
incq 0x10(%rbp)
movl %r15d, %ebp
testl %r13d, %r13d
jne 0x8dcc
incl %r14d
cmpl %r14d, %ebp
jne 0x8d5a
jmp 0x8dc9
movl %r13d, %edi
callq 0x3f61
jmp 0x8db0
movl %ebp, %r14d
leaq 0x10(%rsp), %rdi
callq 0x36d0
cmpl %ebp, %r14d
jne 0x8e04
movq %rbx, %rdi
addq $0xb0, %rdi
leaq 0x185(%rip), %rdx       # 0x8f71
xorl %esi, %esi
movq %rbx, %rcx
callq 0x3770
jmp 0x8e0e
callq 0x3eff
jmp 0x8e04
callq 0x3fcb
movq %rbx, %rdi
callq 0x918d
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