bool RateListenPlan::execute() { // split each second up into 10 pieces, allocate 1/10 total write to each tenth second size_t leftThisSecond = settings.rate; size_t msgsPerTenth = settings.rate / 10; size_t leftThisTenth = msgsPerTenth; expected = 0; absolute_time startOfSecond = GetAbsoluteTime(); absolute_time startOfTenth = startOfSecond; count = 0; totalCount = 0; for (;;) { boost::this_thread::interruption_point(); if (leftThisTenth > 0) { Message* message = getMessage(); if (message != NULL) { leftThisTenth--; leftThisSecond--; delete message->getData(); delete message; } } absolute_time currTime = GetAbsoluteTime(); int64_t tenthTime = GetTimeDurationMillisecs(startOfTenth, currTime); if (tenthTime > 100) { //start new tenth leftThisTenth = msgsPerTenth; startOfTenth = currTime; } else continue; int64_t secondTime = GetTimeDurationMillisecs(startOfSecond, currTime); if (secondTime >= 1000) { //might be 1 message left over, if rate is less than 10 if (leftThisSecond > 0) { Message* message = getMessage(); if (message != NULL) { leftThisSecond--; delete message->getData(); delete message; } } leftThisSecond = settings.rate; startOfSecond = currTime; if (count > 0) std::cout << count << " messages received\n"; count = 0; sendHeartbeat(expected); } } return true; }
char IsTimerExpired(char TimerNum) { if((TimerStarted[TimerNum] == TRUE) && (Timers[TimerNum].AsLong <= GetAbsoluteTime())) { TimerStarted[TimerNum] = FALSE; return TRUE; } return FALSE; }
void StartTimer(char TimerNum, unsigned long ms_duration) { unsigned long ticks; //Calculate ms value in clock ticks. May be truncated. ticks = (unsigned long)(long)((float)ms_duration * TICKS_PER_MS); //Calculate end time and store in timer Timers[TimerNum].AsLong = GetAbsoluteTime() + ticks; TimerStarted[TimerNum] = TRUE; }
void* svlStreamProc::Proc(svlStreamManager* baseref) { svlSample *inputsample, *outputsample; svlFilterBase *filter, *prevfilter; svlFilterSourceBase* source = baseref->StreamSource; svlFilterOutput* output; svlFilterInput* input; svlProcInfo info; svlSyncPoint *sync = baseref->SyncPoint; unsigned int counter = 0; osaTimeServer* timeserver = 0; double timestamp; int status = SVL_OK; // Initializing thread info structure info.count = ThreadCount; info.ID = ThreadID; info.sync = sync; info.cs = baseref->CS; if (ThreadID == 0) { // Execute only on one thread - BEGIN // Initialize time server for accessing absolute time timeserver = new osaTimeServer; timeserver->SetTimeOrigin(); // Execute only on one thread - END } while (baseref->StopThread == false) { source->FrameCounter = counter; outputsample = 0; /////////////////////////////////////// // Handle stream control (pause/play) if (source->PauseAtFrameID == static_cast<int>(counter)) { if (ThreadID == 0) { // Wait until playback resumed or stream stopped while (source->PlayCounter == 0 && baseref->StopThread == false) { osaSleep(0.1); // check 10 times a second } if (baseref->StopThread) { CMN_LOG_INIT_DEBUG << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << source->GetName() << "\"): stream stopped while paused" << std::endl; break; } } if (ThreadCount > 1) { // Execute only if multi-threaded - BEGIN // Synchronization point, wait for other threads if (sync->Sync(ThreadID) != SVL_SYNC_OK) { CMN_LOG_INIT_ERROR << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << source->GetName() << "\"): Sync() returned error (#1)" << std::endl; break; } // Execute only if multi-threaded - END } } if (ThreadID == 0) { if (source->PlayCounter > 0) source->PlayCounter --; if (source->PlayCounter == 0) { // Pause when the next frame arrives source->PauseAtFrameID = static_cast<int>(counter) + 1; } } //////////////////////////////////// // Starting from the stream source status = source->Process(&info, outputsample); if (status == SVL_STOP_REQUEST) { CMN_LOG_INIT_DEBUG << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << source->GetName() << "\"): SVL_STOP_REQUEST received" << std::endl; break; } else if (status < 0) { CMN_LOG_INIT_ERROR << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << source->GetName() << "\"): svlFilterSourceBase::Process() returned error (" << status << ")" << std::endl; break; } if (ThreadID == 0) { // Execute only on one thread - BEGIN if (outputsample && (source->AutoTimestamp || outputsample->GetTimestamp() < 0.0)) { // Get fresh timestamp and assign it to the output sample outputsample->SetTimestamp(GetAbsoluteTime(timeserver)); } // Execute only on one thread - END } if (ThreadCount > 1) { // Execute only if multi-threaded - BEGIN // Synchronization point, wait for other threads if (sync->Sync(ThreadID) != SVL_SYNC_OK) { CMN_LOG_INIT_ERROR << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << source->GetName() << "\"): Sync() returned error (#2)" << std::endl; break; } // Execute only if multi-threaded - END } // Enabled/Disabled flag to be ignored in case of // source filters. Use Pause and Play instead. // Check for errors and stop request if (baseref->StopThread) { CMN_LOG_INIT_DEBUG << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << source->GetName() << "\"): StopThread flag is true (#1)" << std::endl; break; } else if (baseref->StreamStatus != SVL_OK) { CMN_LOG_INIT_ERROR << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << source->GetName() << "\"): StreamStatus signals error (" << baseref->StreamStatus << ") (#1)" << std::endl; break; } prevfilter = source; // Get next filter in the chain output = source->GetOutput(); filter = 0; // Check if trunk output exists if (output) { input = output->Connection; // Check if trunk output is connected if (input) { // If connected input is trunk if (input->Trunk) filter = input->Filter; // If connected input is not trunk else if (ThreadID == 0 && outputsample) input->Buffer->Push(outputsample); // Store timestamps on both the filter input and the filter output if (outputsample) { timestamp = outputsample->GetTimestamp(); output->Timestamp = timestamp; input->Timestamp = timestamp; } } } //////////////////////////////////////////// // Going downstream filter by filter while (filter != 0) { filter->FrameCounter = counter; // Pass samples downstream inputsample = outputsample; outputsample = 0; // Check if the previous output is valid input for the next filter status = filter->IsDataValid(filter->GetInput()->Type, inputsample); if (status != SVL_OK) { CMN_LOG_INIT_ERROR << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << filter->GetName() << "\"): svlFilterBase::IsDataValid() returned error (" << status << ")" << std::endl; break; } status = filter->Process(&info, inputsample, outputsample); if (status < 0) { CMN_LOG_INIT_ERROR << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << filter->GetName() << "\"): svlFilterBase::Process() returned error (" << status << ")" << std::endl; break; } if (ThreadCount > 1) { // Execute only if multi-threaded - BEGIN // Synchronization point, wait for other threads if (sync->Sync(ThreadID) != SVL_SYNC_OK) { CMN_LOG_INIT_ERROR << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << filter->GetName() << "\"): Sync() returned error (#3)" << std::endl; break; } // Execute only if multi-threaded - END } // Thread-safe propagation of Enabled flag to EnabledInternal. // This step introduces at most 1 frame delay to the Enabled/Disabled state. if (ThreadID == 0) { filter->EnabledInternal = filter->Enabled; } // Check for errors and stop request if (baseref->StopThread) { CMN_LOG_INIT_DEBUG << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << filter->GetName() << "\"): StopThread flag is true (#2)" << std::endl; break; } else if (baseref->StreamStatus != SVL_OK) { CMN_LOG_INIT_ERROR << "svlStreamProc::Proc (ThreadID=" << ThreadID << ", Filter=\"" << filter->GetName() << "\"): StreamStatus signals error (" << baseref->StreamStatus << ") (#2)" << std::endl; break; } // Store input time stamp filter->PrevInputTimestamp = inputsample->GetTimestamp(); // Pass input timestamp to output sample if (outputsample) outputsample->SetTimestamp(filter->PrevInputTimestamp); prevfilter = filter; // Get next filter in the chain output = filter->GetOutput(); filter = 0; // Check if trunk output exists if (output) { input = output->Connection; // Check if trunk output is connected if (input) { // If connected input is trunk if (input->Trunk) filter = input->Filter; // If connected input is not trunk else if (ThreadID == 0 && outputsample) input->Buffer->Push(outputsample); // Store timestamps on both the filter input and the filter output if (outputsample) { timestamp = outputsample->GetTimestamp(); output->Timestamp = timestamp; input->Timestamp = timestamp; } } } } if (status < 0) break; // incrementing frame counter counter ++; } if (ThreadID == 0) { // Execute only on one thread - BEGIN // Delete time server if (timeserver) delete timeserver; // Execute only on one thread - END } // Signal the error status if (baseref->StopThread == false) { // Internal shutdown baseref->StreamStatus = status; } if (ThreadCount > 1) { // Execute only if multi-threaded - BEGIN sync->ReleaseAll(); // Execute only if multi-threaded - END } // Run InternalStop() method in case of internal shutdown if (baseref->StopThread == false && ThreadID == 0) { baseref->InternalStop(ThreadID); } return this; }