1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
12 #include <anna/core/functions.hpp>
13 #include <anna/core/tracing/Logger.hpp>
15 #include <anna/app/functions.hpp>
16 #include <anna/app/Application.hpp>
18 #include <anna/comm/Communicator.hpp>
20 #include <anna/timex/internal/TickProducer.hpp>
21 #include <anna/timex/Engine.hpp>
27 void* anna::timex::TickProducer::exec(void* arg)
31 pthread_sigmask(SIG_SETMASK, &mask, NULL);
32 timex::TickProducer* _this = reinterpret_cast <timex::TickProducer*>(arg);
35 comm::Communicator* communicator = app::functions::component <comm::Communicator> (ANNA_FILE_LOCATION);
37 while(communicator->isServing() == false)
38 functions::sleep(Millisecond(100));
40 while(_this->a_requestedStop == false) {
41 functions::sleep(_this->calculeSlice(functions::millisecond()));
44 } catch(RuntimeException& ex) {
45 Logger::write(Logger::Critical, ex.getText(), ANNA_FILE_LOCATION);
52 anna::timex::TickProducer::TickProducer(timex::Engine* timeController, const int fdWrite) :
53 a_timeController(*timeController),
57 a_requestedStop(false)
60 void anna::timex::TickProducer::tick()
62 static char onebyte = 0xff;
63 static int errorCount = 0;
65 if(a_isInPause == true)
68 if(write(a_fdWrite, &onebyte, sizeof(onebyte)) < 0) {
69 Logger::write(Logger::Critical, "Cannot generate tick", strerror(errno), ANNA_FILE_LOCATION);
71 if(++ errorCount == 10) {
72 Logger::write(Logger::Critical, "Terminating application due to error at tick generation", ANNA_FILE_LOCATION);
73 app::functions::getApp().eventAbnormalTermination("timex::TickProducer");
79 //--------------------------------------------------------------------------------------------
80 // Calcula la duracion del proximo tick de reloj. Tiene en cuenta la deriva del anterior
81 // tick y la corrige en este.
82 // (2) Si es mayor que cero => El reloj del sistema se adelanta
83 // Si es menor que cero => El reloj del sistema se atrasa.
84 // (3) Si la correcin aplicada es mayor del 5% de la resolucin volvemos a calcularla ya
85 // que podemos considerar que normalmente no desviar�tanto tiempo.
86 //--------------------------------------------------------------------------------------------
87 Millisecond anna::timex::TickProducer::calculeSlice(const Millisecond & msnow)
89 Millisecond result(a_timeController.getResolution());
90 int correction = (a_expectedTime == 0) ? 0 : (a_expectedTime - msnow); // (2)
93 if(((abs(correction) * 100) / result) > 5) // (3)
96 result += Millisecond(correction);
99 a_expectedTime = msnow + result;