// File: /workspace/github_cpp_makefile_star2_O2/rachit31[P]CubeSatADCS/threadManager.cpp
// Function: ThreadManager::wait()
const char* ThreadManager::wait()
{
  std::stringstream message;

  // Join with terminated threads
  while (!m_threadMap.empty())
  {
    std::string error;

    // Lock the thread Mutex (TLPI pg 636)
    error = m_mutex.lock();
    if (!error.empty())
    {
      return error.c_str();
    }

    // Set the Conditional Variable to wait for a thread to die
    if (m_numUnjoined == 0)
    {
      error = m_mutex.wait();
      if (!error.empty())
      {
        return error.c_str();
      }
    }

    // Loop through the threads to find the dead ones
    std::vector<unsigned int> eraseVec;
    std::map<unsigned int, Thread*>::iterator it;
    for (it = m_threadMap.begin(); it != m_threadMap.end(); ++it)
    {
      if (!it->second->isAlive())
      {
        void* pthreadRetVal;

        // Join with terminated thread (TLPI pg 625)
        int retVal = pthread_join(it->second->getThread(), &pthreadRetVal);
        if (retVal != 0)
        {
          message << "pthread_join(): " << std::strerror(retVal);
          return message.str().c_str();
        }

        if (pthreadRetVal == PTHREAD_CANCELED)
        {
          message << "thread " << it->first << " was canceled" << std::endl;
        }
        else
        {
          message << "thread " << it->first << " joined successfully" << std::endl;
        }

        // Release the dynamically allocated memory
        delete it->second;

        // Add the map key to the delete vector
        eraseVec.push_back(it->first);

        // Decrement the number of unjoined threads
        m_numUnjoined--;
      }
    }

    std::vector<unsigned int>::iterator iter;
    for (iter = eraseVec.begin(); iter != eraseVec.end(); ++iter)
    {
      if (!m_threadMap.erase(*iter))
      {
        error = "could not remove thread from threadManager threadMap";
        return error.c_str();
      }
    }

    // Lock the thread Mutex (TLPI pg 636)
    error = m_mutex.unlock();
    if (!error.empty())
    {
      return error.c_str();
    }
  }

  return message.str().c_str();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %rbx
leaq -0x230(%rbp), %rdi
callq 0x2170
leaq 0x8(%rbx), %rax
movq %rax, -0x80(%rbp)
leaq 0x38(%rbx), %rax
movq %rax, -0x50(%rbp)
leaq 0x10(%rbx), %r15
leaq -0xa8(%rbp), %r14
cmpq $0x0, 0x30(%rbx)
je 0x4416
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
andq $0x0, -0x40(%rbp)
movb $0x0, -0x38(%rbp)
movq -0x50(%rbp), %rdi
callq 0x3228
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x23b0
cmpq $0x0, -0x40(%rbp)
je 0x421a
movq -0x48(%rbp), %r13
xorl %r14d, %r14d
jmp 0x43fb
cmpl $0x0, (%rbx)
je 0x435d
movq %r13, -0x78(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
movq 0x20(%rbx), %r13
cmpq %r15, %r13
je 0x4321
movq 0x28(%r13), %rdi
callq 0x4ebc
testb %al, %al
jne 0x4311
movq 0x28(%r13), %rdi
callq 0x4ec6
movq %rax, %rdi
leaq -0x88(%rbp), %rsi
callq 0x2200
movl %eax, %r12d
testl %eax, %eax
jne 0x43a8
leaq 0x20(%r13), %r12
cmpq $-0x1, -0x88(%rbp)
je 0x42aa
leaq -0x220(%rbp), %rdi
leaq 0x3050(%rip), %rsi      # 0x72e0
callq 0x2220
movl (%r12), %esi
movq %rax, %rdi
callq 0x2150
leaq 0x307a(%rip), %rsi      # 0x7322
jmp 0x42d0
leaq -0x220(%rbp), %rdi
leaq 0x3028(%rip), %rsi      # 0x72e0
callq 0x2220
movl (%r12), %esi
movq %rax, %rdi
callq 0x2150
leaq 0x3044(%rip), %rsi      # 0x7314
movq %rax, %rdi
callq 0x2220
movq %rax, %rdi
callq 0x2100
movq 0x28(%r13), %r14
testq %r14, %r14
je 0x42f1
movq %r14, %rdi
callq 0x4eb6
movq %r14, %rdi
pushq $0x20
popq %rsi
callq 0x2240
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x467c
leaq -0xa8(%rbp), %r14
decl (%rbx)
movq %r13, %rdi
callq 0x2160
movq %rax, %r13
jmp 0x4237
movq -0x70(%rbp), %r12
movq -0x78(%rbp), %r13
cmpq -0x68(%rbp), %r12
je 0x4382
movq -0x80(%rbp), %rdi
movq %r12, %rsi
callq 0x4c38
addq $0x4, %r12
testq %rax, %rax
jne 0x4329
leaq -0x48(%rbp), %rdi
leaq 0x2fe8(%rip), %rsi      # 0x7337
callq 0x23b0
movq -0x48(%rbp), %r13
jmp 0x43ef
movq -0x50(%rbp), %rdi
callq 0x33a0
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x23b0
cmpq $0x0, -0x40(%rbp)
jne 0x420e
jmp 0x4223
movq -0x50(%rbp), %rdi
callq 0x32e4
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x23b0
cmpq $0x0, -0x40(%rbp)
sete %r14b
je 0x43f2
movq -0x48(%rbp), %r13
jmp 0x43f2
leaq -0x220(%rbp), %rdi
leaq 0x2f4d(%rip), %rsi      # 0x7303
callq 0x2220
movq %rax, %r13
movl %r12d, %edi
callq 0x20b0
movq %r13, %rdi
movq %rax, %rsi
callq 0x2220
movq %r14, %rdi
leaq -0x218(%rbp), %rsi
callq 0x22e0
movq -0xa8(%rbp), %r13
movq %r14, %rdi
callq 0x20f0
xorl %r14d, %r14d
leaq -0x70(%rbp), %rdi
callq 0x4aa0
leaq -0x48(%rbp), %rdi
callq 0x20f0
testb %r14b, %r14b
leaq -0xa8(%rbp), %r14
jne 0x41d6
jmp 0x4432
leaq -0x48(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0x22e0
leaq -0x48(%rbp), %rdi
movq (%rdi), %r13
callq 0x20f0
leaq -0x230(%rbp), %rdi
callq 0x21b0
movq %r13, %rax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x447c
jmp 0x4467
jmp 0x4467
jmp 0x4467
movq %rax, %rbx
jmp 0x4473
jmp 0x4467
jmp 0x4467
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x4aa0
leaq -0x48(%rbp), %rdi
callq 0x20f0
leaq -0x230(%rbp), %rdi
callq 0x21b0
movq %rbx, %rdi
callq 0x2350