Exemplo n.º 1
0
TEST(BasicThreadPoolTest, StartStopFailTest) {
  volatile uint32_t num = 0;
  ReleaseWaitArg arg_data1;
  ReleaseWaitArg arg_data2;
  IncrementArg arg_data = { &num };
  ContainedThreadPool<2, 20> thread_pool;

  thread_pool.Start();
  ASSERT_TRUE(thread_pool.EnqueueRun(ReleaseWaitSemaphoreRoutine, &arg_data1));
  ASSERT_TRUE(arg_data1.release_semaphore.Wait(50));
  ASSERT_TRUE(thread_pool.EnqueueRun(ReleaseWaitSemaphoreRoutine, &arg_data2));
  ASSERT_TRUE(arg_data2.release_semaphore.Wait(50));

  // Release semaphore 1
  arg_data1.wait_semaphore.Release();

  // Only first thread is stoppable.
  ASSERT_FALSE(thread_pool.Stop(100));

  // Also stop the 2nd thread now
  arg_data2.wait_semaphore.Release();

  YPlatform::Semaphore semaphore1(0, 1);
  YPlatform::Semaphore semaphore2(0, 1);

  // Restart threads and make sure we have 2 active threads here.
  thread_pool.Start();

  // Have both threads be waiting for the semaphore, but only wake one up.
  ASSERT_TRUE(thread_pool.EnqueueRun(WaitSemaphoreRoutine, &semaphore1));
  ASSERT_TRUE(thread_pool.EnqueueRun(WaitSemaphoreRoutine, &semaphore1));

  // Only wake one up.
  semaphore1.Release();

  for (int i = 0; i < 10; ++i) {
    thread_pool.EnqueueRun(IncrementRoutine, &arg_data);
  }
  ASSERT_TRUE(thread_pool.EnqueueRun(ReleaseSemaphoreRoutine, &semaphore2));
  ASSERT_TRUE(semaphore2.Wait(500));

  semaphore1.Release();
  ASSERT_TRUE(thread_pool.Stop(500));
  EXPECT_EQ(10, num);
}
Exemplo n.º 2
0
int
run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Bug_3541_Regression_Test"));

  int ret = 0;

#if defined(ACE_WIN32) && !defined (ACE_USES_WINCE_SEMA_SIMULATION)
  int lastError;

  // ACE_OS::event_init()

  const ACE_TCHAR *eventName = ACE_TEXT ("Bug3541_Event");

  ACE_Event event0(0,             // int manual_reset = 0
                   0,             // int initial_state = 0
                   USYNC_PROCESS, // int type = USYNC_THREAD
                   eventName);    // const ACE_TCHAR *name = 0

  lastError = ACE_OS::last_error();

  ACE_event_t eventHandle = event0.handle();

  if ((eventHandle == ACE_INVALID_HANDLE) ||
      (lastError != 0))
  {
    ret = -1;

    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("ACE_Event(%s) failed - handle %d lastError %d\n"),
                eventName, eventHandle, lastError));
  }
  else
  {
    ACE_Event event1(0,              // int manual_reset = 0
                     0,              // int initial_state = 0
                     USYNC_PROCESS,  // int type = USYNC_THREAD
                     eventName);     // const ACE_TCHAR *name = 0

    lastError = ACE_OS::last_error();

    eventHandle = event1.handle();

    if ((eventHandle == ACE_INVALID_HANDLE) ||
        (lastError != ERROR_ALREADY_EXISTS))
    {
      ret = -1;

      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("ACE_Event(%s) failed - handle %d lastError %d\n"),
                  eventName, eventHandle, lastError));
    }
  }

  // ACE_OS::sema_init

  const ACE_TCHAR *semaphoreName = ACE_TEXT ("Bug3541_Semaphore");

  ACE_Semaphore semaphore0(1,               // int count = 1
                           USYNC_PROCESS,   // int type = USYNC_THREAD
                           semaphoreName);  // const ACE_TCHAR *name = 0

  lastError = ACE_OS::last_error();

  const ACE_sema_t &semaphoreLock = semaphore0.lock();
  if ((semaphoreLock == ACE_INVALID_HANDLE) ||
      (lastError != 0))
  {
    ret = -1;

    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("ACE_Semaphore(%s) failed - lock %d lastError %d\n"),
                semaphoreName, semaphoreLock, lastError));
  }
  else
  {
    ACE_Semaphore semaphore1(1,               // int count = 1
                             USYNC_PROCESS,   // int type = USYNC_THREAD
                             semaphoreName);  // const ACE_TCHAR *name = 0

    lastError = ACE_OS::last_error();

    const ACE_sema_t &semaphoreLock = semaphore1.lock();

    if ((semaphoreLock == ACE_INVALID_HANDLE) ||
        (lastError != ERROR_ALREADY_EXISTS))
    {
      ret = -1;

      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("ACE_Semaphore(%s) failed - lock %d lastError %d\n"),
                  semaphoreName, semaphoreLock, lastError));
    }
  }

  // ACE_OS::mutex_init()

  const ACE_TCHAR *mutexName = ACE_TEXT ("Bug3541_Mutex");

  ACE_Mutex mutex0(USYNC_PROCESS,  // int type = USYNC_THREAD
                   mutexName);     // const ACE_TCHAR *name = 0

  lastError = ACE_OS::last_error();

  const ACE_mutex_t &mutexLock = mutex0.lock();

  if ((mutexLock.proc_mutex_ == ACE_INVALID_HANDLE) ||
      (lastError != 0))
  {
    ret = -1;

    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("ACE_Mutex(%s) failed - lock %d lastError %d\n"),
                mutexName, mutexLock, lastError));
  }
  else
  {
    ACE_Mutex mutex1(USYNC_PROCESS,  // int type = USYNC_THREAD
                     mutexName);     // const ACE_TCHAR *name = 0

    lastError = ACE_OS::last_error();

    const ACE_mutex_t &mutexLock = mutex1.lock();

    if ((mutexLock.proc_mutex_ == ACE_INVALID_HANDLE) ||
        (lastError != ERROR_ALREADY_EXISTS))
    {
      ret = -1;

      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("ACE_Mutex(%s) failed - lock %d lastError %d\n"),
                  mutexName, mutexLock, lastError));
    }
  }

 #endif

  ACE_END_TEST;

  return ret;
}