// File: /workspace/github_c_makefile_star2_O0/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
subq $0x20, %rsp
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
cmpl $0x0, -0xc(%rbp)
jle 0x79c2
cmpl $0x0, -0x10(%rbp)
jg 0x79f5
movl $0x4, %edi
leaq 0x24c8(%rip), %rsi      # 0x9e96
leaq 0x24d7(%rip), %rdx      # 0x9eac
movl $0x19, %ecx
leaq 0x24dd(%rip), %r8       # 0x9ebe
movb $0x0, %al
callq 0x6450
movq $0x0, -0x8(%rbp)
jmp 0x7c1b
jmp 0x79f7
movl $0xb8, %edi
callq 0x22f0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x7a36
movl $0x4, %edi
leaq 0x247f(%rip), %rsi      # 0x9e96
leaq 0x248e(%rip), %rdx      # 0x9eac
movl $0x1f, %ecx
leaq 0x24a7(%rip), %r8       # 0x9ed1
movb $0x0, %al
callq 0x6450
jmp 0x7c0a
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2460
cmpl $0x0, %eax
jne 0x7a7b
movq -0x20(%rbp), %rdi
addq $0x58, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x22c0
cmpl $0x0, %eax
jne 0x7a7b
movq -0x20(%rbp), %rdi
addq $0x88, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x22c0
cmpl $0x0, %eax
je 0x7aa6
movl $0x4, %edi
leaq 0x240f(%rip), %rsi      # 0x9e96
leaq 0x241e(%rip), %rdx      # 0x9eac
movl $0x27, %ecx
leaq 0x2451(%rip), %r8       # 0x9eeb
movb $0x0, %al
callq 0x6450
jmp 0x7c0a
movslq -0xc(%rbp), %rdi
shlq $0x3, %rdi
callq 0x22f0
movq -0x20(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x7aeb
movl $0x4, %edi
leaq 0x23ca(%rip), %rsi      # 0x9e96
leaq 0x23d9(%rip), %rdx      # 0x9eac
movl $0x2d, %ecx
leaq 0x2433(%rip), %r8       # 0x9f12
movb $0x0, %al
callq 0x6450
jmp 0x7c0a
movslq -0x10(%rbp), %rdi
shlq $0x4, %rdi
callq 0x22f0
movq -0x20(%rbp), %rcx
movq %rax, 0x8(%rcx)
cmpq $0x0, %rax
jne 0x7b31
movl $0x4, %edi
leaq 0x2384(%rip), %rsi      # 0x9e96
leaq 0x2393(%rip), %rdx      # 0x9eac
movl $0x33, %ecx
leaq 0x2404(%rip), %r8       # 0x9f29
movb $0x0, %al
callq 0x6450
jmp 0x7c0a
movl -0xc(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x14(%rax)
movl -0x10(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x28(%rax)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x7bf6
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movslq -0x14(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x20(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq 0x84(%rip), %rdx        # 0x7c30
callq 0x2380
cmpl $0x0, %eax
je 0x7bde
movl $0x4, %edi
leaq 0x22d4(%rip), %rsi      # 0x9e96
leaq 0x22e3(%rip), %rdx      # 0x9eac
movl $0x44, %ecx
leaq 0x236e(%rip), %r8       # 0x9f43
movb $0x0, %al
callq 0x6450
jmp 0x7bf6
movq -0x20(%rbp), %rax
movl 0x14(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x14(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x7b83
movl -0x14(%rbp), %eax
cmpl -0xc(%rbp), %eax
je 0x7c00
jmp 0x7c0a
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x7c1b
movq -0x20(%rbp), %rdi
callq 0x7d60
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)