// File: /workspace/github_c_makefile_star2_O3/ttoobne[P]bohttpd/src/core/threadpool.c
// Function: threadpool_create
threadpool_t* threadpool_create(int threadpool_size, int task_queue_size) {

    int i;
    threadpool_t* pool = NULL;

    if (threadpool_size <= 0 || task_queue_size <= 0) {
        log_error("arguments invalid.");
        return NULL;
    }

    do {
        if ((pool = (threadpool_t*)malloc(sizeof(threadpool_t))) == NULL) {
            log_error("malloc threadpool failed.");
            break;
        }

        /* 初始化互斥锁和条件变量 */
        if (pthread_mutex_init(&(pool->lock_mutex), NULL) != 0 ||
            pthread_cond_init(&(pool->nfull_cond), NULL) != 0 ||
            pthread_cond_init(&(pool->nempty_cond), NULL) != 0) {
            log_error("mutex and condition initialize failed.");
            break;
        }

        if ((pool->threads = (pthread_t*)malloc
            (sizeof(pthread_t) * threadpool_size)) == NULL) {
            log_error("malloc threads failed.");
            break;
        }

        if ((pool->task_queue = (threadpool_task_t*)malloc
            (sizeof(threadpool_task_t) * task_queue_size)) == NULL) {
            log_error("malloc task queue failed.");
            break;
        }

        /* 初始化各个属性值 */
        pool->threadpool_size = threadpool_size;
        pool->thread_running = 0;
        pool->task_queue_size = task_queue_size;
        pool->task_queue_front = 0;
        pool->task_queue_rear = 0;
        pool->task_num = 0;
        pool->shutdown = 0;

        /* 创建 threadpool_size 个线程 */
        for (i = 0; i < threadpool_size; ++ i) {
            if (pthread_create(&(pool->threads[i]), NULL,
                                threadpool_worker, (void*)pool) != 0) {
                log_error("create threads failed.");
                break;
            }
            pool->thread_running ++ ;
        }
        if (i != threadpool_size) {
            break;
        }

        return pool;

    } while (0);

    threadpool_free(pool);

    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
testl %edi, %edi
setg %al
testl %esi, %esi
setg %cl
testb %cl, %al
jne 0x5c12
leaq 0x22a1(%rip), %rsi      # 0x7e8d
leaq 0x22b0(%rip), %rdx      # 0x7ea3
leaq 0x22bb(%rip), %r8       # 0x7eb5
xorl %ebx, %ebx
movl $0x4, %edi
movl $0x19, %ecx
xorl %eax, %eax
callq 0x252a
jmp 0x5cae
movl %esi, %r14d
movl %edi, %r15d
movl $0xb8, %edi
callq 0x22d0
movq %rax, %rbx
testq %rax, %rax
je 0x5c7e
leaq 0x30(%rbx), %rdi
xorl %esi, %esi
callq 0x2440
testl %eax, %eax
jne 0x5c5d
leaq 0x58(%rbx), %rdi
xorl %esi, %esi
callq 0x22a0
testl %eax, %eax
jne 0x5c5d
movq %rbx, %rdi
addq $0x88, %rdi
xorl %esi, %esi
callq 0x22a0
testl %eax, %eax
je 0x5cba
leaq 0x2229(%rip), %rsi      # 0x7e8d
leaq 0x2238(%rip), %rdx      # 0x7ea3
leaq 0x2270(%rip), %r8       # 0x7ee2
movl $0x4, %edi
movl $0x27, %ecx
jmp 0x5c9d
leaq 0x2208(%rip), %rsi      # 0x7e8d
leaq 0x2217(%rip), %rdx      # 0x7ea3
leaq 0x2235(%rip), %r8       # 0x7ec8
movl $0x4, %edi
movl $0x1f, %ecx
xorl %eax, %eax
callq 0x252a
movq %rbx, %rdi
callq 0x5e4a
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl %r15d, %r12d
leaq (,%r12,8), %rdi
callq 0x22d0
movq %rax, (%rbx)
testq %rax, %rax
je 0x5d2c
movl %r14d, %edi
shlq $0x4, %rdi
callq 0x22d0
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x5d50
movl %r15d, 0x10(%rbx)
movl $0x0, 0x14(%rbx)
movl %r14d, 0x18(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rbx)
xorl %r15d, %r15d
leaq 0xa1(%rip), %r14        # 0x5da8
movq (%rbx), %rdi
addq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2350
testl %eax, %eax
jne 0x5d74
incl 0x14(%rbx)
addq $0x8, %r15
decq %r12
jne 0x5d07
jmp 0x5cae
leaq 0x215a(%rip), %rsi      # 0x7e8d
leaq 0x2169(%rip), %rdx      # 0x7ea3
leaq 0x21c8(%rip), %r8       # 0x7f09
movl $0x4, %edi
movl $0x2d, %ecx
jmp 0x5c9d
leaq 0x2136(%rip), %rsi      # 0x7e8d
leaq 0x2145(%rip), %rdx      # 0x7ea3
leaq 0x21bb(%rip), %r8       # 0x7f20
movl $0x4, %edi
movl $0x33, %ecx
jmp 0x5c9d
leaq 0x2112(%rip), %rsi      # 0x7e8d
leaq 0x2121(%rip), %rdx      # 0x7ea3
leaq 0x21b1(%rip), %r8       # 0x7f3a
movl $0x4, %edi
movl $0x44, %ecx
xorl %eax, %eax
callq 0x252a
testl %r12d, %r12d
jne 0x5ca4
jmp 0x5cae