long ProcessTimer() { Position it, itHead, itFirst, itPrev; struct time_node* tvTimer; unsigned long ulNow; long ulRet = -1; List events = lstMakeEmpty(NULL); ulNow = get_tick_count(); if(g_timer == NULL) return -1; lstLock(g_timer); itFirst = it = lstFirst(g_timer); itPrev = itHead = lstHeader(g_timer); for(; it != NULL; itPrev = it, it = lstAdvance(it)) { tvTimer = (struct time_node*)lstRetrieve(it); if(ulNow >= tvTimer->ulNextTime) { if(tvTimer->nRemainCount > 0) tvTimer->nRemainCount--; if(tvTimer->nRemainCount != 0) tvTimer->ulNextTime += tvTimer->nInterval; } else { ulRet = tvTimer->ulNextTime - ulNow; break; } } //Now move the events from the timer list and insert into a temp list. if(itPrev != itHead){ itHead->Next = it; events->Next = itFirst; itPrev->Next = NULL; } lstUnLock(g_timer); itPrev = lstHeader(events); for(it = lstFirst(events); it != NULL; itPrev = it, it = lstAdvance(it)) { tvTimer = (struct time_node*)lstRetrieve(it); if(!tvTimer) continue; tvTimer->OnTimer(tvTimer->pPtr, tvTimer->ulID); if(tvTimer->nRemainCount != 0) { Position itTemp = lstRemovePos(g_timer, itPrev); it = itPrev; if(itTemp) ReSchedule(itTemp); }else { free(tvTimer); } } lstDeleteAll(events); return ulRet; }
void SchdOpti() { if (num_of_booking > 0) Genetic(); ReSchedule(); }