// File: /workspace/github_cpp_makefile_star2_O1/waket[P]ThreadPool/ThreadPoolTask.cpp
// Function: ThreadPoolTask::stop()
bool ThreadPoolTask::stop() {
    //Lock by mutex before changing the value of isStarted
    tasksMutex.lock();
    isStarted = false;
    tasksMutex.unlock();

    //Lock by mutex for thread safe stop(), wait() and requestStop()
    stopMutex.lock();
    if (isShutdown) {
        stopMutex.unlock();
        return true;
    }
    bool ret = true;
    cout << "\tWakeup all the workers waiting for task" << endl;
    tasksCond.broadcast(); // Sometime calling broadcast() is not enough to wakeup all the worker
    cout << "\t" << tasksDeque.size() << " task will be ignored" << endl;

    for (std::set<pthread_t>::const_iterator it = workersSet.begin() ; it != workersSet.end(); it++) {
        if (pthread_join(*(it), NULL) != 0) {
            cout << "\tFailed to stop the worker " << *(it) << endl;
            ret = false;
        }
        tasksCond.broadcast(); // Keep waking up the workers, because of the lock "tasksMutex"
        cout << "\tStop worker " <<*(it) << " DONE" << endl;
    }
    //Remove all the task from the queue
    tasksDeque.clear();
    //Unlock wait()
    stopCond.signal();
    isShutdown = true;
    stopMutex.unlock();
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x90(%rdi), %r14
movq %r14, %rdi
callq 0x2290
movb $0x1, %bpl
movb %bpl, 0xb8(%rbx)
xorl %r15d, %r15d
movb %r15b, 0x150(%rbx)
movb %r15b, 0xb8(%rbx)
movq %r14, %rdi
callq 0x2150
leaq 0xf0(%rbx), %r12
movq %r12, %rdi
callq 0x2290
movb %bpl, 0x118(%rbx)
cmpb %r15b, 0x151(%rbx)
jne 0x3dc9
movq 0x4413(%rip), %r14      # 0x7fd8
leaq 0x1853(%rip), %rsi      # 0x541f
movl $0x28, %edx
movq %r14, %rdi
callq 0x2220
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
leaq 0xc0(%rbx), %rdi
movq %rdi, 0x10(%rsp)
callq 0x2080
leaq 0x16a2(%rip), %rsi      # 0x52ba
movl $0x1, %edx
movq %r14, %rdi
callq 0x2220
movq 0x58(%rbx), %rax
movq %rax, %rcx
subq 0x38(%rbx), %rcx
shrq $0x3, %rcx
cmpq $0x1, %rax
adcq $-0x1, %rcx
leaq 0x10(%rbx), %rax
movq %rax, 0x8(%rsp)
shlq $0x4, %rcx
movq 0x30(%rbx), %rsi
movq 0x40(%rbx), %rax
subq 0x48(%rbx), %rax
subq 0x20(%rbx), %rsi
sarq $0x5, %rax
sarq $0x5, %rsi
addq %rax, %rsi
addq %rcx, %rsi
movq %r14, %rdi
callq 0x2110
movq %rax, %r14
leaq 0x17cf(%rip), %rsi      # 0x5448
movl $0x15, %edx
movq %rax, %rdi
callq 0x2220
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x78(%rbx), %r13
leaq 0x68(%rbx), %r15
cmpq %r15, %r13
je 0x3da9
movq %r12, (%rsp)
movb $0x1, %bpl
movq 0x430c(%rip), %r12      # 0x7fd8
movq 0x20(%r13), %rdi
xorl %esi, %esi
callq 0x2190
testl %eax, %eax
je 0x3d27
movl $0x1b, %edx
movq %r12, %rdi
leaq 0x1774(%rip), %rsi      # 0x545e
callq 0x2220
movq 0x20(%r13), %rsi
movq %r12, %rdi
callq 0x2110
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
callq 0x2080
movl $0xd, %edx
movq %r12, %rdi
leaq 0x173a(%rip), %rsi      # 0x547a
callq 0x2220
movq 0x20(%r13), %rsi
movq %r12, %rdi
callq 0x2110
movq %rax, %r14
movl $0x5, %edx
movq %rax, %rdi
leaq 0x132f(%rip), %rsi      # 0x5092
callq 0x2220
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq %r13, %rdi
callq 0x2230
movq %rax, %r13
cmpq %r15, %rax
jne 0x3ccc
movq (%rsp), %r12
jmp 0x3dac
movb $0x1, %bpl
movq 0x8(%rsp), %rdi
callq 0x43ba
leaq 0x120(%rbx), %rdi
callq 0x2030
movb $0x1, 0x151(%rbx)
movb $0x0, 0x118(%rbx)
movq %r12, %rdi
callq 0x2150
andb $0x1, %bpl
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop