/** * Register a tickable to be ticked in a certain interval. * Tickables with the same interval are grouped to one timer to save timers. * A tickable may be registered multiple times with different intervals. * * First a timer with the same interval is looked up. If none found, a free one is * used. Then a free tickable slot (of max CFG_MAX_TICKABLES) is looked up. If all went * well, the timer is configured and (re)started. */ void TickHandler::add(Tickable* tickable, uint32_t interval) { int timerNumber = findTimer(interval); if (timerNumber == -1) { timerNumber = findTimer(0); // no timer with given tick interval exsist -> look for unused (interval == 0) if(timerNumber == -1) { Logger::error("No free timer available for interval=%d", interval); return; } timerEntry[timerNumber].interval = interval; } int tickableNumber = findTickable(timerNumber, 0); if (tickableNumber == -1) { Logger::error("No free tickable slot for timer %d with interval %d", timerNumber, timerEntry[timerNumber].interval); return; } timerEntry[timerNumber].tickable[tickableNumber] = tickable; Logger::debug("register tickable %d as number %d to timer %d, %dus interval", tickable, tickableNumber, timerNumber, interval); switch (timerNumber) { // restarting a timer which would already be running is no problem (see DueTimer.cpp) case 0: Timer0.setPeriod(interval).attachInterrupt(timer0Interrupt).start(); break; case 1: Timer1.setPeriod(interval).attachInterrupt(timer1Interrupt).start(); break; case 2: Timer2.setPeriod(interval).attachInterrupt(timer2Interrupt).start(); break; case 3: Timer3.setPeriod(interval).attachInterrupt(timer3Interrupt).start(); break; case 4: Timer4.setPeriod(interval).attachInterrupt(timer4Interrupt).start(); break; case 5: Timer5.setPeriod(interval).attachInterrupt(timer5Interrupt).start(); break; case 6: Timer6.setPeriod(interval).attachInterrupt(timer6Interrupt).start(); break; case 7: Timer7.setPeriod(interval).attachInterrupt(timer7Interrupt).start(); break; case 8: Timer8.setPeriod(interval).attachInterrupt(timer8Interrupt).start(); break; } }
QString Stats::timerStatistics( const QString& name ) { int timer_index = findTimer(name, 0); if (timer_index < 0) { timer_index = findTimer(name + " (wait GL)", 0); if (timer_index < 0) return QString("Timer not found (%1).").arg(name); } const Record& timer = _records[TIMER][timer_index]; float average = 0; if (timer.touches_since_last_reset > 0) average = timer.value / (float)timer.touches_since_last_reset; QString output = QString("%1: %2 total time, %3 calls, %4 average"). arg(name).arg(timer.value).arg(timer.touches_since_last_reset).arg(average); return output; }
bool TimeManager::checkTime(std::string key, float time) { TimeManager::CheckTimer* timer = findTimer(key); if (timer == NULL) { timer = addTimer(); } return timer->checkTime(time); }
//删除定时 bool ScriptTimerMgr::KillTimer(OBJID idTimer) { MAP_TIMER_HANDLER_ITER iter = findTimer(idTimer); if (iter == m_mapHandler.end()) { return false; } m_mapHandler.erase(iter); m_pkTimerMgr->KillTimer(this, idTimer); return true; }
/** * Return the current count of a timer. */ int Timer(int num) { TIMER *pt; pt = findTimer(num); if (pt == NULL) return -1; if (pt->frame) return pt->ticks; else return pt->secs; }
/** * Find an empty timer slot. */ static TIMER *allocateTimer(int num) { assert(num); // zero is not allowed as a timer number assert(!findTimer(num)); // Allocating already existant timer for (int i = 0; i < MAX_TIMERS; i++) { if (!timers[i].tno) { timers[i].tno = num; return &timers[i]; } } error("Too many timers"); }
void Stats::startTimer( const QString& name ) { int index = findTimer(name, _timer_stack.isEmpty() ? 0 : _timer_stack.last()); if (index == -1) { index = _records[TIMER].size(); Record newtimer; newtimer.name = name; newtimer.category = TIMER; newtimer.stamp.start(); newtimer.value = 0; _records[TIMER].append(newtimer); Record* pointer = &(_records[TIMER].last()); if (_timer_stack.size() > 0) { assert( findTimer(_timer_stack.last()) < findTimer(pointer) ); pointer->parent = _timer_stack.last(); _timer_stack.last()->children.append(pointer); } else { pointer->parent = &_headers[TIMER]; _headers[TIMER].children.append(pointer); } _layout_changed = true; } else { _records[TIMER][index].stamp.start(); } _records[TIMER][index].touches_since_last_reset++; _timer_stack.append(&(_records[TIMER][index])); }
/** * Start a timer up. */ void StartTimer(int num, int sval, bool up, bool frame) { TIMER *pt; assert(num); // zero is not allowed as a timer number pt = findTimer(num); if (pt == NULL) pt = allocateTimer(num); pt->delta = up ? 1 : -1; // Increment/decrement value pt->frame = frame; if (frame) { pt->secs = 0; pt->ticks = sval; } else { pt->secs = sval; pt->ticks = 0; } }
TimeManager::CheckTimer* TimeManager::addTimer(std::string key) { static int i = 0; std::string k = key; TimeManager::CheckTimer* timer = NULL; if (k.empty()) { k = "my standard timer " + to_string(i++); } else { timer = findTimer(k); } if (timer == NULL) { timer = new TimeManager::CheckTimer; _mCT.insert(std::make_pair(k, timer)); } return timer; }