Пример #1
0
//---------------------------------------------------------------------------
//	@function:
//		CSyncHashtableTest::Unittest_WaitTasks
//
//	@doc:
//		Waits until all others tasks have started
//
//---------------------------------------------------------------------------
void
CSyncHashtableTest::Unittest_WaitTasks
	(
	SElemTest *pelemtest
	)
{
	CEvent *pevent = pelemtest->Pevent();

	CAutoMutex am(*(pevent->Pmutex()));
	am.Lock();

	// increase number of started tasks
	pelemtest->IncStarted();

	// wait if some other task has not started yet
	if (pelemtest->UlStarted() < GPOS_SHT_THREADS)
	{
		pevent->Wait();
	}
	else
	{
		// wake up all waiting tasks
		pevent->Broadcast();
	}
}
Пример #2
0
	virtual bool OnStart()
	{
		if(m_pTcp)
			m_pTcp->Start();
		if(m_oDomainInfo.GetSize())
		{
			m_oEvent.Reset();
			CRbTreeNode* pIt = m_oDomainInfo.First();
			CRbTreeNode* pEnd = m_oDomainInfo.End();
			for(; pIt!=pEnd; pIt=m_oDomainInfo.GetNext(pIt))
			{
				CAcmDomain& oDomain = m_oDomainInfo.GetItem(pIt);
				if(oDomain.pSerialModule)
					oDomain.pSerialModule->Start();
				oDomain.pUdp->Start();
			}
			if(m_pTokenMoudle)
			{
				if(m_pVipModule && !m_pVipModule->Start())
					return false;
				m_pTokenMoudle->Start();
			}
			m_oEvent.Wait(3000);
			if(m_bNodeRepeat)
				return false;
		}
		return true;
	}
CStatus CThreadForMsgLoop::Run(void * pContext)
{
	if(m_pThread == 0)
	{
		return CStatus(-1,0,"In CThreadForMsgLoop::Run : m_pThread is null");
	}

	CEvent event;
	CThreadInitialFinishedNotifier notifier(&event);

	SInitialParameter para;
	para.pContext = pContext;
	para.pNotifier = &notifier;

	CStatus s_r = m_pThread->Run(&para);
	if(!s_r.IsSuccess())
	{
		m_bWaitForDeath = false;
		m_pThread = 0;
		return CStatus(-1,0,"In CThreadForMsgLoop::Run invoke run failed!");
	}
	
	//主线程等待子线程初始化完毕
	CStatus s_w = event.Wait();
	if(!s_w.IsSuccess())
		return s_w;
	
	//判断子线程初始化是否成功
	if(!notifier.IsInitialSuccess())
		return CStatus(-1,0,"In CThreadForMsgLoop::Run : Thread initial failed");
	else
		return CStatus(0,0);
}
Пример #4
0
bool PowerOnTV(uint64_t iTimeout = 60000)
{
  cec_power_status currentTvPower(CEC_POWER_STATUS_UNKNOWN);
  uint64_t iNow = GetTimeMs();
  uint64_t iTarget = iNow + iTimeout;

  if (currentTvPower != CEC_POWER_STATUS_ON)
  {
    currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV);
    if (currentTvPower != CEC_POWER_STATUS_ON)
    {
      PrintToStdOut("Sending 'power on' command to the TV\n=== Please wait ===");
      g_parser->PowerOnDevices(CECDEVICE_TV);
      while (iTarget > iNow)
      {
        g_responseEvent.Wait((uint32_t)(iTarget - iNow));
        if (g_lastCommand == CEC_OPCODE_REQUEST_ACTIVE_SOURCE)
          break;
        iNow = GetTimeMs();
      }
    }
  }

  currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV);

  if (currentTvPower != CEC_POWER_STATUS_ON)
    PrintToStdOut("Failed to power on the TV, or the TV does not respond properly");

  return currentTvPower == CEC_POWER_STATUS_ON;
}
Пример #5
0
TEST(TestThreadLocal, Simple)
{
  GlobalThreadLocal runnable;
  thread t(runnable);

  gate.Wait();
  EXPECT_TRUE(runnable.waiting);
  EXPECT_TRUE(staticThinggy != NULL);
  EXPECT_TRUE(staticThreadLocal.get() == NULL);
  waiter.Set();
  gate.Wait();
  EXPECT_TRUE(runnable.threadLocalHadValue);
  EXPECT_TRUE(!destructorCalled);
  delete staticThinggy;
  EXPECT_TRUE(destructorCalled);
  cleanup();
}
Пример #6
0
bool PreloadClip::WaitForPreload(PlayListItem* pItem)
{
    if (!pItem) return false;
    CEvent syncEvent;
    return m_pPreloadCmdProcessor->SetSyncComamnd(pItem, &syncEvent)
           ? syncEvent.Wait(TIME_INFINITY)
           : false;
}
Пример #7
0
 void Run()
 {
     AtomicGuard g(mutex);
     L lock(sec);
     haslock = true;
     obtainedlock = true;
     if (wait)
         wait->Wait();
     haslock = false;
 }
Пример #8
0
  inline void operator()()
  {
    staticThinggy = new Thinggy;
    staticThreadLocal.set(staticThinggy);
    waiting = true;
    gate.Set();
    waiter.Wait();
    waiting = false;

    threadLocalHadValue = staticThreadLocal.get() != NULL;
    gate.Set();
  }
Пример #9
0
bool CThreadsPool::WaitAllComplete( CEvent& CancelEv )
{
    LogStates();
    m_mtTasks.Lock();
    if( !m_vecTasks.empty() )
    {
        m_TasksEmpty.Reset();
        m_mtTasks.Unlock();
        Log::instance().Trace( 0, "CThreadsPool::WaitAllComplete: Wait for empty" );

        for(;;)
        {
            if( m_TasksEmpty.Wait(0) )
                break;
            if( CancelEv.Wait(100) )
            {
                Log::instance().Trace( 0, "CThreadsPool::WaitAllComplete: canceled" );
                return false;
            }
        }
    } else
        m_mtTasks.Unlock();
    Log::instance().Trace( 0, "CThreadsPool::WaitAllComplete: Wait for complete" );
    for( std::vector< SmartPtr<CEvent> >::iterator It = m_vecThreadsStates.begin(); It != m_vecThreadsStates.end(); It++ )
    {
        for(;;)
        {
            if( (*It)->Wait(0) )
                break;
            if( CancelEv.Wait(100) )
            {
                Log::instance().Trace( 0, "CThreadsPool::WaitAllComplete: canceled2" );
                return false;
            }
        }
    }
    LogStates();
    return true;
}
Пример #10
0
TEST(TestThreadLocal, HeapDestroyed)
{
  {
    HeapThreadLocal runnable;
    thread t(runnable);

    gate.Wait();
    EXPECT_TRUE(runnable.waiting);
    EXPECT_TRUE(staticThinggy != NULL);
    EXPECT_TRUE(runnable.threadLocal.get() == NULL);
    waiter.Set();
    gate.Wait();
    EXPECT_TRUE(runnable.threadLocalHadValue);
    EXPECT_TRUE(!destructorCalled);
  } // runnable goes out of scope

  // even though the threadlocal is gone ...
  EXPECT_TRUE(!destructorCalled);
  delete staticThinggy;
  EXPECT_TRUE(destructorCalled);
  cleanup();
}
Пример #11
0
unsigned __stdcall ThreadFunction( void *pParam )
{
	IClient *pClient = ( IClient * )pParam;

	ASSERT( pClient );

	while ( !m_theQuitThreadEvent.Wait( 0 ) )
	{
		size_t dataLength = 0;

		const void *pData = pClient->GetPackFromServer( dataLength );

		if ( !pData || 0 == dataLength )
		{
			Sleep( 1 );
			continue;
		}

		EXTEND_HEADER* pHeader = (EXTEND_HEADER*)pData;
		if (pHeader->ProtocolFamily == pf_relay)
		{
			if (pHeader->ProtocolID == relay_c2c_data)
			{
				RELAY_DATA* pRelayCmd = (RELAY_DATA*)pData;

				in_addr ia;
				ia.s_addr = pRelayCmd->nFromIP;
				cout << "relaydata: " << inet_ntoa(ia) << '(' << pRelayCmd->nFromRelayID << ')' << " -- recv size: " << dataLength << endl;
			}
			if (pHeader->ProtocolID == relay_s2c_loseway)
			{
				RELAY_DATA* pRelayCmd = (RELAY_DATA*)pData;

				in_addr ia;
				ia.s_addr = pRelayCmd->nFromIP;
				cout << "loseway: " << inet_ntoa(ia) << '(' << pRelayCmd->nFromRelayID << ')' << " -- recv size: " << dataLength << endl;
			}
		}
	}

	cout << "The read thread was killed safely!" << endl;
	m_theAfirmThreadQuitEvent.Set();

	return 0;
}