// File: /workspace/github_cpp_makefile_star2_O1/waket[P]ThreadPool/ThreadPoolTask.cpp
// Function: ThreadPoolTask::setWorkerCount(unsigned long)
bool ThreadPoolTask::setWorkerCount(size_t dynWorkerCount) {
    //Lock by mutex to avoid scheduling successive requestStop
    stopMutex.lock();
    if(isShutdown || dynWorkerCount == workersSet.size()) { //Nothing to do
        assert(!isStarted);
        stopMutex.unlock();
        return true;
    }

    if (dynWorkerCount > workersSet.size()) {
        workerCount = dynWorkerCount;
        startWorkersMutex.lock();
        //Start new worker by creating new thread
        for (unsigned int i = 0; i < dynWorkerCount - workersSet.size(); i++) {
            pthread_t pth;
            if (pthread_create(&pth, NULL, start_worker, (void*) this) != 0) {
                cout << "\tFailed to create new worker" << endl;
                stopMutex.unlock();
                startWorkersMutex.unlock();
                return false;
            }
        }
        startWorkersCond.wait(startWorkersMutex.mutexPtr());
        startWorkersMutex.unlock();
    } else {
        workerCount = dynWorkerCount;
        isStopWorker = true;
        // wakeup a worker to exit
        tasksCond.signal();
        stopWorkersMutex.lock();
        // wait till all unnecessary workers exit
        stopWorkersCond.wait(stopWorkersMutex.mutexPtr());
        stopWorkersMutex.unlock();
    }
    assert(workerCount == workersSet.size());
    stopMutex.unlock();
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r13
movq %rdi, %r14
leaq 0xf0(%rdi), %rbx
movq %rbx, %rdi
callq 0x2290
movb $0x1, 0x118(%r14)
cmpb $0x0, 0x151(%r14)
jne 0x40e0
cmpq %r13, 0x88(%r14)
jne 0x410d
cmpb $0x1, 0x150(%r14)
jne 0x4284
leaq 0x13b5(%rip), %rdi      # 0x54aa
leaq 0x1279(%rip), %rsi      # 0x5375
leaq 0x1422(%rip), %rcx      # 0x5525
movl $0xdd, %edx
callq 0x2100
movq %r13, 0x8(%r14)
jae 0x4219
leaq 0x158(%r14), %rdi
movq %rdi, 0x8(%rsp)
callq 0x2290
leaq 0x180(%r14), %rax
movq %rax, (%rsp)
movb $0x1, 0x180(%r14)
movl $0x188, %r12d           # imm = 0x188
cmpq %r13, 0x88(%r14)
setne %r15b
je 0x4254
leaq -0xdc8(%rip), %rdx      # 0x3391
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
movq %r14, %rcx
callq 0x2340
testl %eax, %eax
jne 0x41a6
movl $0x1, %ebp
movq %r13, %rax
subq 0x88(%r14), %rax
movl %ebp, %ecx
cmpq %rcx, %rax
seta %r15b
jbe 0x4254
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
leaq -0xe07(%rip), %rdx      # 0x3391
movq %r14, %rcx
callq 0x2340
incl %ebp
testl %eax, %eax
je 0x4171
movq 0x3e2b(%rip), %r13      # 0x7fd8
leaq 0x10a7(%rip), %rsi      # 0x525b
movl $0x1c, %edx
movq %r13, %rdi
callq 0x2220
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
xorl %ebp, %ebp
movb %bpl, 0x118(%r14)
movq %rbx, %rdi
callq 0x2150
movb %bpl, 0x180(%r14)
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x2150
testb %r15b, %r15b
movq (%rsp), %r15
je 0x425d
jmp 0x4297
movb $0x1, %bpl
movb %bpl, 0x152(%r14)
leaq 0xc0(%r14), %rdi
callq 0x2030
leaq 0x1b8(%r14), %r13
movq %r13, %rdi
callq 0x2290
leaq 0x1e0(%r14), %r15
movb %bpl, 0x1e0(%r14)
movl $0x1e8, %r12d           # imm = 0x1E8
jmp 0x425d
movq 0x8(%rsp), %r13
movq (%rsp), %r15
addq %r14, %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x22b0
movb $0x0, (%r15)
movq %r13, %rdi
callq 0x2150
movq 0x8(%r14), %rax
cmpq 0x88(%r14), %rax
jne 0x42a8
movb $0x0, 0x118(%r14)
movq %rbx, %rdi
callq 0x2150
movb $0x1, %bpl
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12a2(%rip), %rdi      # 0x5551
leaq 0x10bf(%rip), %rsi      # 0x5375
leaq 0x1268(%rip), %rcx      # 0x5525
movl $0xfb, %edx
callq 0x2100
nop