// File: /workspace/github_c_makefile_star2_O2/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, %r15d
pushq $0x1
popq %rdi
movl $0xd8, %esi
callq 0x32c0
movq %rax, %rbx
testq %rax, %rax
je 0x84f4
movl %r15d, 0xc8(%rbx)
pushq $0x18
popq %rdi
callq 0x3450
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movq %rax, 0xb8(%rbx)
pushq $0x50
popq %rdi
callq 0x3450
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movq %rax, %rdi
callq 0x8c30
movq %r14, 0xc0(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3660
testl %eax, %eax
jne 0x849a
leaq 0x28(%rbx), %rdi
xorl %esi, %esi
callq 0x3660
testl %eax, %eax
jne 0x849a
leaq 0x80(%rbx), %rdi
xorl %esi, %esi
callq 0x3530
testl %eax, %eax
jne 0x849a
leaq 0x50(%rbx), %rdi
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x3530
testl %eax, %eax
jne 0x849a
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x3550
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x35b0
testl %r15d, %r15d
movl $0x0, %r14d
movl %r15d, 0x4(%rsp)
cmovgl %r15d, %r14d
leaq 0x19d(%rip), %r15       # 0x85c2
leaq 0x8(%rsp), %r12
cmpl %ebp, %r14d
je 0x8463
andq $0x0, 0x8(%rsp)
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x37a0
testl %eax, %eax
jne 0x8546
movq 0xb8(%rbx), %rdi
movq %r12, %rsi
callq 0x8b56
incl %ebp
jmp 0x842a
movl %r14d, %ebp
leaq 0x10(%rsp), %rdi
callq 0x3700
cmpl 0x4(%rsp), %ebp
jne 0x852a
movq %rbx, %rdi
addq $0xb0, %rdi
leaq 0x247(%rip), %rdx       # 0x86d2
xorl %esi, %esi
movq %rbx, %rcx
callq 0x37a0
jmp 0x8534
movq 0x7b37(%rip), %r15      # 0xffd8
movq (%r15), %rdi
leaq 0x1d00(%rip), %rsi      # 0xa1ab
leaq 0x394a(%rip), %rdx      # 0xbdfc
leaq 0x3955(%rip), %rcx      # 0xbe0e
pushq $0x72
popq %r8
xorl %eax, %eax
callq 0x3610
movq (%r15), %rcx
leaq 0x397e(%rip), %rdi      # 0xbe4c
pushq $0x1a
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x3800
movq (%r15), %rcx
leaq 0x37bd(%rip), %rdi      # 0xbca4
pushq $0x2
popq %rsi
movq %r14, %rdx
callq 0x3800
jmp 0x852a
movq 0x7add(%rip), %rax      # 0xffd8
movq (%rax), %rdi
leaq 0x1ca6(%rip), %rsi      # 0xa1ab
leaq 0x38f0(%rip), %rdx      # 0xbdfc
leaq 0x38fb(%rip), %rcx      # 0xbe0e
pushq $0x55
popq %r8
xorl %eax, %eax
callq 0x3610
leaq 0x38f8(%rip), %rdi      # 0xbe1d
callq 0x3380
movq %rbx, %rdi
callq 0x88ec
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r13d
movq 0x7a88(%rip), %r15      # 0xffd8
movq (%r15), %rdi
leaq 0x1c51(%rip), %rsi      # 0xa1ab
leaq 0x389b(%rip), %rdx      # 0xbdfc
leaq 0x38a6(%rip), %rcx      # 0xbe0e
pushq $0x7e
popq %r8
xorl %eax, %eax
callq 0x3610
movq (%r15), %r14
movl %r13d, %edi
callq 0x3170
leaq 0x366e(%rip), %rsi      # 0xbbf3
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3610
movq (%r15), %rcx
leaq 0x3708(%rip), %rdi      # 0xbca4
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x3800
jmp 0x8466
movq %rax, %rbx
pushq $0x50
popq %rsi
movq %r14, %rdi
callq 0x3470
movq %rbx, %rdi
callq 0x3770