// File: /workspace/github_c_makefile_star2_O2/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 0x5765
leaq 0x174a(%rip), %rsi      # 0x6e8d
leaq 0x1759(%rip), %rdx      # 0x6ea3
leaq 0x1764(%rip), %r8       # 0x6eb5
xorl %ebx, %ebx
pushq $0x4
popq %rdi
pushq $0x19
popq %rcx
xorl %eax, %eax
callq 0x4c60
jmp 0x57f8
movl %esi, %r14d
movl %edi, %r15d
movl $0xb8, %edi
callq 0x22d0
movq %rax, %rbx
testq %rax, %rax
je 0x57cc
leaq 0x30(%rbx), %rdi
xorl %esi, %esi
callq 0x2440
testl %eax, %eax
jne 0x57b0
leaq 0x58(%rbx), %rdi
xorl %esi, %esi
callq 0x22a0
testl %eax, %eax
jne 0x57b0
movq %rbx, %rdi
addq $0x88, %rdi
xorl %esi, %esi
callq 0x22a0
testl %eax, %eax
je 0x580a
leaq 0x16d6(%rip), %rsi      # 0x6e8d
leaq 0x16e5(%rip), %rdx      # 0x6ea3
leaq 0x171d(%rip), %r8       # 0x6ee2
pushq $0x4
popq %rdi
pushq $0x27
jmp 0x57e6
leaq 0x16ba(%rip), %rsi      # 0x6e8d
leaq 0x16c9(%rip), %rdx      # 0x6ea3
leaq 0x16e7(%rip), %r8       # 0x6ec8
pushq $0x4
popq %rdi
pushq $0x1f
popq %rcx
xorl %eax, %eax
callq 0x4c60
movq %rbx, %rdi
callq 0x598e
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r15d, %r12d
leaq (,%r12,8), %rdi
callq 0x22d0
movq %rax, (%rbx)
testq %rax, %rax
je 0x5880
movl %r14d, %edi
shlq $0x4, %rdi
callq 0x22d0
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x589f
movl %r15d, 0x10(%rbx)
andl $0x0, 0x14(%rbx)
movl %r14d, 0x18(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rbx)
xorl %r15d, %r15d
leaq 0x9a(%rip), %r14        # 0x58ee
movq %r12, %r13
subq $0x1, %r13
jb 0x57f8
movq (%rbx), %rdi
addq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2350
testl %eax, %eax
jne 0x58be
incl 0x14(%rbx)
addq $0x8, %r15
movq %r13, %r12
jmp 0x5857
leaq 0x1606(%rip), %rsi      # 0x6e8d
leaq 0x1615(%rip), %rdx      # 0x6ea3
leaq 0x1674(%rip), %r8       # 0x6f09
pushq $0x4
popq %rdi
pushq $0x2d
jmp 0x57e6
leaq 0x15e7(%rip), %rsi      # 0x6e8d
leaq 0x15f6(%rip), %rdx      # 0x6ea3
leaq 0x166c(%rip), %r8       # 0x6f20
pushq $0x4
popq %rdi
pushq $0x33
jmp 0x57e6
leaq 0x15c8(%rip), %rsi      # 0x6e8d
leaq 0x15d7(%rip), %rdx      # 0x6ea3
leaq 0x1667(%rip), %r8       # 0x6f3a
pushq $0x4
popq %rdi
pushq $0x44
popq %rcx
xorl %eax, %eax
callq 0x4c60
testl %r12d, %r12d
jne 0x57ee
jmp 0x57f8