inline void CRPCClient<TRequest, TReply>::Ask(const TRequest& request, TReply& reply) { CMutexGuard LOCK(m_Mutex); unsigned int tries = 0; for (;;) { try { SetAffinity(GetAffinity(request)); Connect(); // No-op if already connected *m_Out << request; *m_In >> reply; break; } catch (CException& e) { // Some exceptions tend to correspond to transient glitches; // the remainder, however, may as well get propagated immediately. if ( !dynamic_cast<CSerialException*>(&e) && !dynamic_cast<CIOException*>(&e) ) { throw; } else if (++tries == m_RetryLimit || !x_ShouldRetry(tries)) { throw; } else if ( !(tries & 1) ) { // reset on every other attempt in case we're out of sync try { Reset(); } STD_CATCH_ALL_XX(Serial_RPCClient,1,"CRPCClient<>::Reset()"); } SleepSec(m_RetryDelay.GetCompleteSeconds()); SleepMicroSec(m_RetryDelay.GetNanoSecondsAfterSecond() / 1000); } } }
CTestTask::EStatus CTestTask::Execute(void) { int duration = s_WaitPeriods[m_Serial]; CStopWatch timer(CStopWatch::eStart); if (s_CancelTypes[m_Serial] == eCheckCancel) { MSG_POST("Task " << m_Serial << ": " << duration << " with checking"); for (int i = 0; i < 10; ++i) { SleepMicroSec(duration * 100); if (IsCancelRequested()) { MSG_POST("Task " << m_Serial << " was cancelled"); return eCanceled; } } } else { MSG_POST("Task " << m_Serial << ": " << duration); SleepMilliSec(duration); } if (IsCancelRequested()) { MSG_POST("Task " << m_Serial << " was cancelled without check (time spent - " << timer.Elapsed() << ")"); return eCanceled; } else { MSG_POST("Task " << m_Serial << " complete (time spent - " << timer.Elapsed() << ")"); return eCompleted; } }
bool CThreadPoolTester::Thread_Run(int idx) { bool status = true; try { for (int i = 0; i < kTasksPerThread; ++i) { int serial_num = s_SerialNum.Add(1); double need_time = double(s_PostTimes[serial_num]) / 1000; double now_time = s_Timer.Elapsed(); if (now_time < need_time) { SleepMicroSec(int((need_time - now_time) * 1000000)); } int req_num = s_ActionTasks[serial_num]; switch (s_Actions[serial_num]) { case eAddTask: MSG_POST("Task " << req_num << " to be queued"); s_Pool->AddTask(new CTestTask(req_num)); MSG_POST("Task " << req_num << " queued"); break; case eAddExclusiveTask: MSG_POST("Task " << req_num << " to be queued"); s_Pool->RequestExclusiveExecution(new CExclusiveTask(req_num), CThreadPool::fFlushThreads + CThreadPool::fCancelExecutingTasks + CThreadPool::fCancelQueuedTasks); MSG_POST("Task " << req_num << " queued"); break; case eCancelTask: while (!s_Tasks[req_num]) { SleepMilliSec(10); } MSG_POST("Task " << req_num << " to be cancelled"); s_Pool->CancelTask(s_Tasks[req_num]); MSG_POST("Cancelation of task " << req_num << " requested"); break; case eFlushWaiting: case eFlushNoWait: MSG_POST("Flushing threads with " << (s_Actions[serial_num] == eFlushWaiting? "waiting": "immediate restart")); s_Pool->FlushThreads( s_Actions[serial_num] == eFlushWaiting? CThreadPool::eWaitToFinish: CThreadPool::eStartImmediately); MSG_POST("Flushing process began"); break; case eCancelAll: MSG_POST("Cancelling all tasks"); s_Pool->CancelTasks(CThreadPool::fCancelExecutingTasks + CThreadPool::fCancelQueuedTasks); MSG_POST("Cancellation of all tasks requested"); break; } } } STD_CATCH_ALL("CThreadPoolTester: status " << (status = false)) return status; }