void run_alarms(void) { int done=0; struct snmp_alarm *sa_ptr; /* loop through everything we have repeatedly looking for the next thing to call until all events are finally in the future again */ DEBUGMSGTL(("snmp_alarm_run_alarms","looking for alarms to run...\n")); while(done == 0) { sa_ptr = sa_find_next(); if (sa_ptr == NULL) return; if (sa_ptr->nextcall <= time(NULL)) { DEBUGMSGTL(("snmp_alarm_run_alarms"," running alarm %d\n", sa_ptr->clientreg)); (*(sa_ptr->thecallback))(sa_ptr->clientreg, sa_ptr->clientarg); DEBUGMSGTL(("snmp_alarm_run_alarms"," ... done\n")); sa_ptr->lastcall = time(NULL); sa_ptr->nextcall = 0; sa_update_entry(sa_ptr); } else { done = 1; } } DEBUGMSGTL(("snmp_alarm_run_alarms","Done.\n")); }
int get_next_alarm_delay_time(void) { struct snmp_alarm *sa_ptr; int nexttime = 0; sa_ptr = sa_find_next(); if (sa_ptr) { nexttime = sa_ptr->nextcall - time(NULL); if (nexttime <= 0) nexttime = 1; /* occurred already, return 1 second */ } return nexttime; }
int get_next_alarm_delay_time(struct timeval *delta) { struct zevent_alarm *sa_ptr; struct timeval t_diff, t_now; sa_ptr = sa_find_next(); if(sa_ptr) { gettimeofday(&t_now, 0); if((t_now.tv_sec > sa_ptr->t_next.tv_sec) || ((t_now.tv_sec == sa_ptr->t_next.tv_sec) && (t_now.tv_usec > sa_ptr->t_next.tv_usec))) { /* * * Time has already passed. Return the smallest possible amount of * * time. * */ delta->tv_sec = 0; delta->tv_usec = 1; return sa_ptr->clientreg; } else { /* * * Time is still in the future. * */ t_diff.tv_sec = sa_ptr->t_next.tv_sec - t_now.tv_sec; t_diff.tv_usec = sa_ptr->t_next.tv_usec - t_now.tv_usec; while(t_diff.tv_usec < 0) { t_diff.tv_sec -= 1; t_diff.tv_usec += 1000000; } delta->tv_sec = t_diff.tv_sec; delta->tv_usec = t_diff.tv_usec; return sa_ptr->clientreg; } } /* * * Nothing Left. * */ return 0; }
void run_alarms(void) { int done = 0; struct zevent_alarm *a = NULL; unsigned int clientreg; struct timeval t_now; /* * * Loop through everything we have repeatedly looking for the next thing to * * call until all events are finally in the future again. * */ while(!done) { if((a = sa_find_next()) == NULL) { return; } gettimeofday(&t_now, NULL); if((a->t_next.tv_sec < t_now.tv_sec) || ((a->t_next.tv_sec == t_now.tv_sec) && (a->t_next.tv_usec < t_now.tv_usec))) { clientreg = a->clientreg; printf("run alarm %d\n", clientreg); (*(a->thecallback)) (clientreg, a->clientarg); printf("alarm %d completed\n", clientreg); if((a = sa_find_specific(clientreg)) != NULL) { a->t_last.tv_sec = t_now.tv_sec; a->t_last.tv_usec = t_now.tv_usec; a->t_next.tv_sec = 0; a->t_next.tv_usec = 0; sa_update_entry(a); } else { printf("alarm %d deleted itself\n", clientreg); } } else { done = 1; } } }