// File: /workspace/github_cpp_makefile_star2_O1/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 $0x218, %rsp            # imm = 0x218
movq %rdi, %rbx
leaq -0x238(%rbp), %rdi
callq 0x2180
leaq 0x8(%rbx), %rax
leaq 0x38(%rbx), %rcx
movq %rcx, -0x78(%rbp)
leaq 0x10(%rbx), %rcx
movq %rcx, -0x80(%rbp)
movq %rax, -0x88(%rbp)
cmpq $0x0, 0x30(%rbx)
je 0x509c
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movb $0x0, -0x38(%rbp)
movq -0x78(%rbp), %rdi
callq 0x3722
movq %rax, %r14
movq -0x40(%rbp), %r15
movq %rax, %rdi
callq 0x2100
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2320
cmpq $0x0, -0x40(%rbp)
je 0x4d4a
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
xorl %r14d, %r14d
jmp 0x5078
cmpl $0x0, (%rbx)
je 0x5000
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movq 0x20(%rbx), %r15
cmpq -0x80(%rbp), %r15
je 0x4f84
movq 0x28(%r15), %rdi
callq 0x5a9e
testb %al, %al
jne 0x4f6f
movq 0x28(%r15), %rdi
callq 0x5aa8
movq %rax, %rdi
leaq -0x90(%rbp), %rsi
callq 0x2220
movl %eax, %r12d
testl %eax, %eax
je 0x4deb
movl $0x10, %edx
leaq -0x228(%rbp), %rdi
leaq 0x3550(%rip), %rsi      # 0x8303
callq 0x2290
movl %r12d, %edi
callq 0x20d0
testq %rax, %rax
je 0x4e55
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
leaq -0x228(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x4e7a
leaq 0x20(%r15), %r14
cmpq $-0x1, -0x90(%rbp)
je 0x4ebb
movl $0x7, %edx
leaq -0x228(%rbp), %rdi
leaq 0x34d0(%rip), %rsi      # 0x82e0
callq 0x2290
movl (%r14), %esi
leaq -0x228(%rbp), %rdi
callq 0x2150
movq %rax, %r13
movl $0x14, %edx
movq %rax, %rdi
leaq 0x34ec(%rip), %rsi      # 0x8322
callq 0x2290
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2240
jmp 0x4f0e
movq -0x228(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x228(%rbp), %rcx
leaq (%rcx,%rax), %rdi
movl -0x208(%rbp,%rax), %esi
orl $0x1, %esi
callq 0x2370
leaq -0xb0(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x2330
movq -0xb0(%rbp), %r14
leaq -0xa0(%rbp), %rax
cmpq %rax, %r14
je 0x4eb2
movq -0xa0(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2270
movq %r14, -0x50(%rbp)
jmp 0x4f66
movl $0x7, %edx
leaq -0x228(%rbp), %rdi
leaq 0x3412(%rip), %rsi      # 0x82e0
callq 0x2290
movl (%r14), %esi
leaq -0x228(%rbp), %rdi
callq 0x2150
movq %rax, %r13
movl $0xd, %edx
movq %rax, %rdi
leaq 0x3420(%rip), %rsi      # 0x8314
callq 0x2290
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2240
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2160
movq 0x28(%r15), %r13
testq %r13, %r13
je 0x4f3f
movq %r13, %rdi
callq 0x5a98
movl $0x20, %esi
movq %r13, %rdi
callq 0x2270
movq -0x68(%rbp), %rsi
cmpq -0x60(%rbp), %rsi
je 0x4f58
movl (%r14), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x68(%rbp)
jmp 0x4f64
leaq -0x70(%rbp), %rdi
movq %r14, %rdx
callq 0x5710
decl (%rbx)
testl %r12d, %r12d
jne 0x5060
movq %r15, %rdi
callq 0x2170
movq %rax, %r15
cmpq -0x80(%rbp), %rax
jne 0x4d70
movq -0x70(%rbp), %r14
cmpq -0x68(%rbp), %r14
je 0x4fbf
movq -0x88(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x5850
movq 0x30(%rbx), %r15
movq %r12, %rdi
movq %rax, %rsi
callq 0x58cc
cmpq 0x30(%rbx), %r15
je 0x503c
addq $0x4, %r14
jmp 0x4f88
movq -0x78(%rbp), %rdi
callq 0x3850
movq %rax, %r14
movq -0x40(%rbp), %r15
movq %rax, %rdi
callq 0x2100
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2320
cmpq $0x0, -0x40(%rbp)
sete %r14b
je 0x5063
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x5063
movq -0x78(%rbp), %rdi
callq 0x397e
movq %rax, %r14
movq -0x40(%rbp), %r15
movq %rax, %rdi
callq 0x2100
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2320
cmpq $0x0, -0x40(%rbp)
jne 0x4d3a
jmp 0x4d53
movq -0x40(%rbp), %rdx
movl $0x34, %r8d
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
leaq 0x32e4(%rip), %rcx      # 0x8337
callq 0x2320
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
xorl %r14d, %r14d
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x5078
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x2270
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x5091
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2270
testb %r14b, %r14b
jne 0x4ce8
jmp 0x50cc
leaq -0x220(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
movq -0x48(%rbp), %rbx
leaq -0x38(%rbp), %rax
cmpq %rax, %rbx
je 0x50c8
movq -0x38(%rbp), %rsi
incq %rsi
movq %rbx, %rdi
callq 0x2270
movq %rbx, -0x50(%rbp)
movq 0x5ee5(%rip), %rsi      # 0xafb8
leaq -0x238(%rbp), %rdi
callq 0x21c0
leaq -0x1b8(%rbp), %rdi
callq 0x20c0
movq -0x50(%rbp), %rax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5144
jmp 0x5113
jmp 0x5113
movq %rax, %rbx
jmp 0x512b
jmp 0x5113
jmp 0x5113
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x512b
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x2270
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x5144
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2270
movq 0x5e6d(%rip), %rsi      # 0xafb8
leaq -0x238(%rbp), %rdi
callq 0x21c0
leaq -0x1b8(%rbp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x23b0
nop