// File: /workspace/github_c_makefile_star2_O3/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
leaq 0x8(%rdi), %r15
movq %r15, %rdi
callq 0x3620
movq 0x30(%r14), %rax
movq (%rax), %r12
cmpq 0x8(%rax), %r12
je 0x88e9
leaq 0x8b6c(%rip), %r13      # 0x11428
xorl %ebx, %ebx
movq (%r12), %rdi
callq 0x3620
movq (%r12), %rdi
movl 0x30(%rdi), %eax
cmpl (%r13), %eax
jb 0x88ed
callq 0x3310
addq $0x8, %r12
movq 0x30(%r14), %rax
cmpq 0x8(%rax), %r12
jne 0x88be
jmp 0x8912
xorl %ebx, %ebx
jmp 0x8912
movq 0x28(%rdi), %rbx
incl %eax
movl %eax, 0x30(%rdi)
callq 0x3310
movq 0x30(%r14), %rax
cmpq 0x8(%rax), %r12
je 0x8912
movq %r15, %rdi
callq 0x3310
jmp 0x8993
movq %r15, %rdi
callq 0x3310
movl $0x1, %edi
movl $0x10, %esi
callq 0x32b0
testq %rax, %rax
je 0x8993
movq %rax, %r12
callq 0x36f0
movq %rax, (%r12)
testq %rax, %rax
je 0x898b
movq %rax, %r13
movq %r14, 0x8(%r12)
movl $0x1, %edi
movl $0x38, %esi
callq 0x32b0
movq %rax, (%rsp)
testq %rax, %rax
je 0x8983
movq %rax, %rbp
movq %rbx, 0x28(%rax)
movl $0x1, 0x30(%rax)
movq %rax, %rdi
xorl %esi, %esi
callq 0x3630
testl %eax, %eax
je 0x89a5
movq %rbp, %rdi
callq 0x3500
movq %r13, %rdi
callq 0x35b0
movq %r12, %rdi
callq 0x3500
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x3620
movq 0x30(%r14), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x89c5
movq %rbp, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x89cd
movq %rsp, %rdx
callq 0x8a6c
movq %r15, %rdi
callq 0x3310
movq (%r14), %rdi
leaq -0x25e(%rip), %rsi      # 0x8781
movq %r12, %rdx
callq 0x9179
movq (%r12), %rbx
jmp 0x8993