// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
pushq %rax
testl %edi, %edi
setg %al
testl %esi, %esi
setg %cl
testb %cl, %al
jne 0x5a95
leaq 0x241e(%rip), %rsi      # 0x7e8d
leaq 0x242d(%rip), %rdx      # 0x7ea3
leaq 0x2438(%rip), %r8       # 0x7eb5
xorl %ebx, %ebx
movl $0x4, %edi
movl $0x19, %ecx
xorl %eax, %eax
callq 0x252a
jmp 0x5b31
movl %esi, %r15d
movl %edi, %r14d
movl $0xb8, %edi
callq 0x22d0
movq %rax, %rbx
testq %rax, %rax
je 0x5b01
leaq 0x30(%rbx), %rdi
xorl %esi, %esi
callq 0x2440
testl %eax, %eax
jne 0x5ae0
leaq 0x58(%rbx), %rdi
xorl %esi, %esi
callq 0x22a0
testl %eax, %eax
jne 0x5ae0
movq %rbx, %rdi
addq $0x88, %rdi
xorl %esi, %esi
callq 0x22a0
testl %eax, %eax
je 0x5b43
leaq 0x23a6(%rip), %rsi      # 0x7e8d
leaq 0x23b5(%rip), %rdx      # 0x7ea3
leaq 0x23ed(%rip), %r8       # 0x7ee2
movl $0x4, %edi
movl $0x27, %ecx
jmp 0x5b20
leaq 0x2385(%rip), %rsi      # 0x7e8d
leaq 0x2394(%rip), %rdx      # 0x7ea3
leaq 0x23b2(%rip), %r8       # 0x7ec8
movl $0x4, %edi
movl $0x1f, %ecx
xorl %eax, %eax
callq 0x252a
movq %rbx, %rdi
callq 0x5ce4
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r14d, %r12d
leaq (,%r12,8), %rdi
callq 0x22d0
movq %rax, (%rbx)
testq %rax, %rax
je 0x5bc1
movl %r15d, %edi
shlq $0x4, %rdi
callq 0x22d0
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x5be5
movl %r14d, 0x10(%rbx)
movl $0x0, 0x14(%rbx)
movl %r15d, 0x18(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rbx)
testl %r14d, %r14d
jle 0x5c09
shlq $0x3, %r12
xorl %r15d, %r15d
xorl %r13d, %r13d
movq (%rbx), %rdi
addq %r15, %rdi
xorl %esi, %esi
leaq 0x9e(%rip), %rdx        # 0x5c42
movq %rbx, %rcx
callq 0x2350
testl %eax, %eax
jne 0x5c0e
incl 0x14(%rbx)
incl %r13d
addq $0x8, %r15
cmpq %r15, %r12
jne 0x5b95
jmp 0x5c34
leaq 0x22c5(%rip), %rsi      # 0x7e8d
leaq 0x22d4(%rip), %rdx      # 0x7ea3
leaq 0x2333(%rip), %r8       # 0x7f09
movl $0x4, %edi
movl $0x2d, %ecx
jmp 0x5b20
leaq 0x22a1(%rip), %rsi      # 0x7e8d
leaq 0x22b0(%rip), %rdx      # 0x7ea3
leaq 0x2326(%rip), %r8       # 0x7f20
movl $0x4, %edi
movl $0x33, %ecx
jmp 0x5b20
xorl %r13d, %r13d
jmp 0x5c34
leaq 0x2278(%rip), %rsi      # 0x7e8d
leaq 0x2287(%rip), %rdx      # 0x7ea3
leaq 0x2317(%rip), %r8       # 0x7f3a
movl $0x4, %edi
movl $0x44, %ecx
xorl %eax, %eax
callq 0x252a
cmpl %r14d, %r13d
jne 0x5b27
jmp 0x5b31