// File: /workspace/github_c_makefile_star2_O1/sszllzss[P]webSocket_Libevent_ThreadPool/evbase_threadpool.cpp
// Function: evbase_threadpool_add_event(evbase_threadpool_t*)
event_base * evbase_threadpool_add_event(evbase_threadpool_t * evb_thpool)
{
    event_base *base = NULL;
    evbase_thread_vector::iterator i;
    pthread_mutex_lock(&evb_thpool->evbase_thread_list.lock);
    for(i = evb_thpool->evbase_thread_list.list->begin();
        i != evb_thpool->evbase_thread_list.list->end(); i++)
    {
        pthread_mutex_lock(&(*i)->monitor_num_locl);
        if((*i)->monitor_num < BASE_MAX_MONITOR_NUM)
        {
            base = (*i)->base;
            (*i)->monitor_num++;
            pthread_mutex_unlock(&(*i)->monitor_num_locl);
            break;
        }
        pthread_mutex_unlock(&(*i)->monitor_num_locl);
    }
    
    if(i == evb_thpool->evbase_thread_list.list->end())
    {
        pthread_mutex_unlock(&evb_thpool->evbase_thread_list.lock);
        do{
            struct evbase_thread_arg *evb_th_arg = (struct evbase_thread_arg *)malloc(sizeof(struct evbase_thread_arg));
            if(evb_th_arg == NULL)
                break;
            memset(evb_th_arg, 0, sizeof(struct evbase_thread_arg));
            evb_th_arg->base = event_base_new();
            if(evb_th_arg->base == NULL)
            {
                free(evb_th_arg);
                break;

            }
            evb_th_arg->evb_thpool = evb_thpool;

            evbase_thread_t *evb_th = (evbase_thread_t *)malloc(sizeof(evbase_thread_t));
            if(evb_th == NULL)
            {
                event_base_free(evb_th_arg->base);
                free(evb_th_arg);
                break;
            }
            memset(evb_th, 0, sizeof(evbase_thread_t));
            evb_th->base = base;
            evb_th->monitor_num++;
            if(pthread_mutex_init(&evb_th->monitor_num_locl, NULL) != 0)
            {
                free(evb_th);
                event_base_free(evb_th_arg->base);
                free(evb_th_arg);
                break;
            }
            pthread_mutex_lock(&evb_thpool->evbase_thread_list.lock);
            evb_thpool->evbase_thread_list.list->push_back(evb_th);
            pthread_mutex_unlock(&evb_thpool->evbase_thread_list.lock);

            //创建一个base监听
            threadpool_add(evb_thpool->pool, evbase_thread, evb_th_arg);
            base = evb_th_arg->base;
        }while(0);   
    }else
        pthread_mutex_unlock(&evb_thpool->evbase_thread_list.lock);


    return base;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %r14
leaq 0x8(%rdi), %r15
movq %r15, %rdi
callq 0x3620
movq 0x30(%r14), %rax
movq (%rax), %r12
cmpq 0x8(%rax), %r12
je 0x896a
leaq 0x8aeb(%rip), %r13      # 0x11428
xorl %ebx, %ebx
movq (%r12), %rdi
callq 0x3620
movq (%r12), %rdi
movl 0x30(%rdi), %eax
cmpl (%r13), %eax
jb 0x896e
callq 0x3310
addq $0x8, %r12
movq 0x30(%r14), %rax
cmpq 0x8(%rax), %r12
jne 0x893f
jmp 0x897c
xorl %ebx, %ebx
jmp 0x897c
movq 0x28(%rdi), %rbx
incl %eax
movl %eax, 0x30(%rdi)
callq 0x3310
movq 0x30(%r14), %rax
movq 0x8(%rax), %r13
movq %r15, %rdi
callq 0x3310
cmpq %r13, %r12
jne 0x8a2a
movl $0x10, %edi
callq 0x35a0
testq %rax, %rax
je 0x8a2a
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
callq 0x36f0
movq %rax, (%r12)
testq %rax, %rax
je 0x8a22
movq %rax, %r13
movq %r14, 0x8(%r12)
movl $0x38, %edi
callq 0x35a0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x8a1a
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq $0x0, 0x30(%rax)
movq %rbx, 0x28(%rax)
movl $0x1, 0x30(%rax)
movq %rax, %rdi
xorl %esi, %esi
callq 0x3630
testl %eax, %eax
je 0x8a3b
movq 0x8(%rsp), %rdi
callq 0x3500
movq (%r12), %rdi
jmp 0x8a1d
movq %r13, %rdi
callq 0x35b0
movq %r12, %rdi
callq 0x3500
movq %rbx, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %r15, %rdi
callq 0x3620
movq 0x30(%r14), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x8a60
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x8a6a
leaq 0x8(%rsp), %rdx
callq 0x8af2
movq %r15, %rdi
callq 0x3310
movq (%r14), %rdi
leaq -0x277(%rip), %rsi      # 0x8805
movq %r12, %rdx
callq 0x920d
movq (%r12), %rbx
jmp 0x8a2a