// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rdi, %r14
leaq 0xf0(%rdi), %rbx
movq %rbx, %rdi
callq 0x2290
movb $0x1, 0x118(%r14)
cmpb $0x0, 0x151(%r14)
jne 0x4124
cmpq %r12, 0x88(%r14)
jne 0x4151
cmpb $0x1, 0x150(%r14)
jne 0x4232
leaq 0x1371(%rip), %rdi      # 0x54aa
leaq 0x1235(%rip), %rsi      # 0x5375
leaq 0x13de(%rip), %rcx      # 0x5525
movl $0xdd, %edx
callq 0x2100
movq %r12, 0x8(%r14)
jae 0x41d6
leaq 0x158(%r14), %rdi
movq %rdi, 0x8(%rsp)
callq 0x2290
movb $0x1, 0x180(%r14)
cmpq %r12, 0x88(%r14)
je 0x41b8
movl $0x1, %r15d
leaq -0xdf5(%rip), %r13      # 0x3391
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2340
testl %eax, %eax
jne 0x4256
movl %r15d, %eax
movq %r12, %rcx
subq 0x88(%r14), %rcx
incl %r15d
cmpq %rax, %rcx
ja 0x418b
leaq 0x188(%r14), %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x22b0
movb $0x0, 0x180(%r14)
jmp 0x4219
movb $0x1, %bpl
movb %bpl, 0x152(%r14)
leaq 0xc0(%r14), %rdi
callq 0x2030
leaq 0x1b8(%r14), %r15
movq %r15, %rdi
callq 0x2290
movb %bpl, 0x1e0(%r14)
leaq 0x1e8(%r14), %rdi
movq %r15, %rsi
callq 0x22b0
movb $0x0, 0x1e0(%r14)
movq %r15, %rdi
callq 0x2150
movq 0x8(%r14), %rax
cmpq 0x88(%r14), %rax
jne 0x42bd
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
movq 0x3d7b(%rip), %r12      # 0x7fd8
leaq 0xff7(%rip), %rsi       # 0x525b
movl $0x1c, %edx
movq %r12, %rdi
callq 0x2220
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r12, %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), %rdi
callq 0x2150
jmp 0x4245
leaq 0x128d(%rip), %rdi      # 0x5551
leaq 0x10aa(%rip), %rsi      # 0x5375
leaq 0x1253(%rip), %rcx      # 0x5525
movl $0xfb, %edx
callq 0x2100