void ThreadTest::workerThread()
{
   m_mutex.acquire();
   m_workerRunning = true;
   m_startedCondition.broadcast();

   ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("%t Thread running, waiting for stop condition.\n")));

   if (m_doubleLock)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%t Thread starting double acquire.\n")));
      m_mutex.acquire();
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%t Thread finished double acquire.\n")));
    }


   // Wait for the STOP condition to occur
   m_stopCondition.wait();
   ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("%t Thread received stop condition, exiting.\n")));

   if (m_doubleLock)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%t Thread starting double release.\n")));
      m_mutex.release();
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%t Thread finished double acquire.\n")));
    }
   m_mutex.release();
}
Пример #2
0
// returns 0 on success to take mutex, -3 on failure upon timeout, -2 on failure upon null pointer. m is pointer obtained w/ ace_mutex_new(), tout_usec is in microsec (no timeout is 0xffffffff).
int8_t ace_mutex_take(void* m, uint32_t tout_usec)
{
    ACE_Recursive_Thread_Mutex* acemtx = (ACE_Recursive_Thread_Mutex*)m;
    if(NULL == acemtx)
    {
        return(-2);
    }

    acemtx->acquire();

    return(0);
}
int
ThreadTest::run(bool doubleLock)
{
  ACE_hthread_t m_workerThreadHandle;
  ACE_thread_t m_workerThreadId;
  m_workerRunning = false;
  m_doubleLock = doubleLock;

  m_mutex.acquire();

  // Start worker thread
  int rval = ACE_Thread::spawn((ACE_THR_FUNC) workerThreadWrapper, this,
     THR_JOINABLE | THR_NEW_LWP, &m_workerThreadId, &m_workerThreadHandle,
     ACE_DEFAULT_THREAD_PRIORITY);

  if (rval == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("%t Could not start worker thread!\n")),
                         1);
    }

  if (!m_workerRunning)
    {
      ACE_DEBUG ((LM_DEBUG,
                 ACE_TEXT ("%t Waiting for worker thread to start running...\n")));
      m_startedCondition.wait();
    }
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("%t Worker thread is running...\n")));

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("%t Broadcasting STOP Condition...\n")));

  m_stopCondition.broadcast();

  m_mutex.release();

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("%t Joining worker thread...\n")));

  ACE_Thread::join(m_workerThreadHandle);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("%t Test finished...\n")));

  return 0;
}