// File: /workspace/github_c_makefile_star2_O2/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 0x3650
movq 0x30(%r14), %rax
movq (%rax), %r12
xorl %ebx, %ebx
leaq 0x84c2(%rip), %r13      # 0x10440
cmpq 0x8(%rax), %r12
je 0x7fa9
movq (%r12), %rdi
callq 0x3650
movq (%r12), %rdi
movl 0x30(%rdi), %eax
cmpl (%r13), %eax
jb 0x7fae
callq 0x3320
addq $0x8, %r12
movq 0x30(%r14), %rax
jmp 0x7f7e
movq %r12, %r13
jmp 0x7fc4
movq 0x28(%rdi), %rbx
incl %eax
movl %eax, 0x30(%rdi)
callq 0x3320
movq 0x30(%r14), %rax
movq 0x8(%rax), %r13
movq %r15, %rdi
callq 0x3320
cmpq %r13, %r12
jne 0x8042
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x32c0
testq %rax, %rax
je 0x8042
movq %rax, %r12
callq 0x3720
movq %rax, (%r12)
testq %rax, %rax
je 0x803a
movq %rax, %r13
movq %r14, 0x8(%r12)
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x32c0
movq %rax, (%rsp)
testq %rax, %rax
je 0x8032
movq %rax, %rbp
movq %rbx, 0x28(%rax)
movl $0x1, 0x30(%rax)
movq %rax, %rdi
xorl %esi, %esi
callq 0x3660
testl %eax, %eax
je 0x8054
movq %rbp, %rdi
callq 0x3520
movq %r13, %rdi
callq 0x35e0
movq %r12, %rdi
callq 0x3520
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x3650
movq 0x30(%r14), %rdi
movq %rsp, %rsi
callq 0x8144
movq %r15, %rdi
callq 0x3320
movq (%r14), %rdi
leaq -0x21a(%rip), %rsi      # 0x7e60
movq %r12, %rdx
callq 0x8979
movq (%r12), %rbx
jmp 0x8042