// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x38(%rbp)
addq $0x90, %rdi
callq 0x5b00
movq -0x38(%rbp), %rdi
movb $0x0, 0x150(%rdi)
addq $0x90, %rdi
callq 0x3d90
movq -0x38(%rbp), %rdi
addq $0xf0, %rdi
callq 0x5b00
movq -0x38(%rbp), %rax
testb $0x1, 0x151(%rax)
je 0x539d
movq -0x38(%rbp), %rdi
addq $0xf0, %rdi
callq 0x3d90
movb $0x1, -0x1(%rbp)
jmp 0x555c
movb $0x1, -0x11(%rbp)
movq 0x8c30(%rip), %rdi      # 0xdfd8
leaq 0x4062(%rip), %rsi      # 0x9411
callq 0x2180
movq %rax, %rdi
movq 0x8c0a(%rip), %rsi      # 0xdfc8
callq 0x21c0
movq -0x38(%rbp), %rdi
addq $0xc0, %rdi
callq 0x5de0
movq 0x8bfe(%rip), %rdi      # 0xdfd8
leaq 0x3ecb(%rip), %rsi      # 0x92ac
callq 0x2180
movq -0x38(%rbp), %rdi
movq %rax, -0x40(%rbp)
addq $0x10, %rdi
callq 0x5c00
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
movq %rax, %rdi
leaq 0x402d(%rip), %rsi      # 0x943a
callq 0x2180
movq %rax, %rdi
movq 0x8bac(%rip), %rsi      # 0xdfc8
callq 0x21c0
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
callq 0x5e00
movq %rax, -0x20(%rbp)
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
callq 0x5e60
movq %rax, -0x28(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x5e30
testb $0x1, %al
jne 0x5459
jmp 0x5520
leaq -0x20(%rbp), %rdi
callq 0x5e90
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2150
cmpl $0x0, %eax
je 0x54b2
movq 0x8b5e(%rip), %rdi      # 0xdfd8
leaq 0x3fcf(%rip), %rsi      # 0x9450
callq 0x2180
movq %rax, -0x48(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x5e90
movq -0x48(%rbp), %rdi
movq (%rax), %rsi
callq 0x2050
movq %rax, %rdi
movq 0x8b1f(%rip), %rsi      # 0xdfc8
callq 0x21c0
movb $0x0, -0x11(%rbp)
movq -0x38(%rbp), %rdi
addq $0xc0, %rdi
callq 0x5de0
movq 0x8b0f(%rip), %rdi      # 0xdfd8
leaq 0x3f9c(%rip), %rsi      # 0x946c
callq 0x2180
movq %rax, -0x50(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x5e90
movq -0x50(%rbp), %rdi
movq (%rax), %rsi
callq 0x2050
movq %rax, %rdi
leaq 0x3b86(%rip), %rsi      # 0x907e
callq 0x2180
movq %rax, %rdi
movq 0x8ac1(%rip), %rsi      # 0xdfc8
callq 0x21c0
leaq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x5eb0
movq %rax, -0x30(%rbp)
jmp 0x5432
movq -0x38(%rbp), %rdi
addq $0x10, %rdi
callq 0x5ef0
movq -0x38(%rbp), %rdi
addq $0x120, %rdi            # imm = 0x120
callq 0x5be0
movq -0x38(%rbp), %rdi
movb $0x1, 0x151(%rdi)
addq $0xf0, %rdi
callq 0x3d90
movb -0x11(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)