// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x30(%rbp)
addq $0xf0, %rdi
callq 0x5b00
movq -0x30(%rbp), %rax
testb $0x1, 0x151(%rax)
jne 0x58c2
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
addq $0x60, %rdi
callq 0x5bc0
movq %rax, %rcx
movq -0x38(%rbp), %rax
cmpq %rcx, %rax
jne 0x590e
movq -0x30(%rbp), %rax
movb 0x150(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x58d4
jmp 0x58d6
jmp 0x58f5
leaq 0x3bbf(%rip), %rdi      # 0x949c
leaq 0x3a83(%rip), %rsi      # 0x9367
movl $0xdd, %edx
leaq 0x3c27(%rip), %rcx      # 0x9517
callq 0x20e0
movq -0x30(%rbp), %rdi
addq $0xf0, %rdi
callq 0x3d90
movb $0x1, -0x1(%rbp)
jmp 0x5af2
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
addq $0x60, %rdi
callq 0x5bc0
movq %rax, %rcx
movq -0x40(%rbp), %rax
cmpq %rcx, %rax
jbe 0x5a34
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, 0x8(%rdi)
addq $0x158, %rdi            # imm = 0x158
callq 0x5b00
movl $0x0, -0x1c(%rbp)
movq -0x30(%rbp), %rdi
movl -0x1c(%rbp), %eax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
addq $0x60, %rdi
callq 0x5bc0
movq -0x50(%rbp), %rcx
movq %rax, %rdx
movq -0x48(%rbp), %rax
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x59f9
movq -0x30(%rbp), %rcx
leaq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0xe14(%rip), %rdx      # 0x4b80
callq 0x22f0
cmpl $0x0, %eax
je 0x59e9
movq 0x8633(%rip), %rdi      # 0xdfd8
leaq 0x38a1(%rip), %rsi      # 0x924d
callq 0x2180
movq %rax, %rdi
movq 0x860d(%rip), %rsi      # 0xdfc8
callq 0x21c0
movq -0x30(%rbp), %rdi
addq $0xf0, %rdi
callq 0x3d90
movq -0x30(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x3d90
movb $0x0, -0x1(%rbp)
jmp 0x5af2
jmp 0x59eb
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x5952
movq -0x30(%rbp), %rdi
movq %rdi, %rax
addq $0x188, %rax            # imm = 0x188
movq %rax, -0x58(%rbp)
addq $0x158, %rdi            # imm = 0x158
callq 0x5b60
movq -0x58(%rbp), %rdi
movq %rax, %rsi
callq 0x5b30
movq -0x30(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x3d90
jmp 0x5a9c
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, 0x8(%rdi)
movb $0x1, 0x152(%rdi)
addq $0xc0, %rdi
callq 0x5be0
movq -0x30(%rbp), %rdi
addq $0x1b8, %rdi            # imm = 0x1B8
callq 0x5b00
movq -0x30(%rbp), %rdi
movq %rdi, %rax
addq $0x1e8, %rax            # imm = 0x1E8
movq %rax, -0x60(%rbp)
addq $0x1b8, %rdi            # imm = 0x1B8
callq 0x5b60
movq -0x60(%rbp), %rdi
movq %rax, %rsi
callq 0x5b30
movq -0x30(%rbp), %rdi
addq $0x1b8, %rdi            # imm = 0x1B8
callq 0x3d90
movq -0x30(%rbp), %rdi
movq 0x8(%rdi), %rax
movq %rax, -0x68(%rbp)
addq $0x60, %rdi
callq 0x5bc0
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
jne 0x5abf
jmp 0x5ade
leaq 0x3a7d(%rip), %rdi      # 0x9543
leaq 0x389a(%rip), %rsi      # 0x9367
movl $0xfb, %edx
leaq 0x3a3e(%rip), %rcx      # 0x9517
callq 0x20e0
movq -0x30(%rbp), %rdi
addq $0xf0, %rdi
callq 0x3d90
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)