/* clock_task: clock task */ void clock_task(void) { si_time next_time; /* local copies of the current time */ int alarm_hours, alarm_minutes, alarm_seconds, hours, minutes, seconds, enable; si_get_current_time(&next_time); /* infinite loop */ while (1) { /* increment time */ //printf("%d\n",1); increment_time(); /* read and display current time */ get_time(&hours, &minutes, &seconds, &enable); get_alarm_time(&alarm_hours, &alarm_minutes, &alarm_seconds); display_time(hours, minutes, seconds); if (enable && alarm_hours == hours && alarm_minutes == minutes && alarm_seconds == seconds) { enable_alarm(); } si_time_add_n_ms(&next_time,1000); /* wait one second */ si_wait_until_time(&next_time); } }
int stream_handler::on_connect(const int e) { if (e == 0) { state_ = CONNECT; enable_alarm(); return msghandler_->on_connect(this); } else { return e; } }
/* additionally the active alarm menu might be set by this function */ void gui_SignalTimeChange(void) { /* recalculate dependent values */ gui_Recalculate(); /* setup menu */ menu_SetMEList(&gui_menu, melist_display_time, 0); /* enable alarm */ if ( gui_data.is_alarm != 0 ) { menu_SetMEList(&gui_menu, melist_active_alarm_menu, 0); enable_alarm(); } }
/* * Schedule alarm for the next active timeout, if any * * We assume the caller has obtained the current time, or a close-enough * approximation. */ static void schedule_alarm(TimestampTz now) { if (num_active_timeouts > 0) { struct itimerval timeval; long secs; int usecs; MemSet(&timeval, 0, sizeof(struct itimerval)); /* Get the time remaining till the nearest pending timeout */ TimestampDifference(now, active_timeouts[0]->fin_time, &secs, &usecs); /* * It's possible that the difference is less than a microsecond; * ensure we don't cancel, rather than set, the interrupt. */ if (secs == 0 && usecs == 0) usecs = 1; timeval.it_value.tv_sec = secs; timeval.it_value.tv_usec = usecs; /* * We must enable the signal handler before calling setitimer(); if we * did it in the other order, we'd have a race condition wherein the * interrupt could occur before we can set alarm_enabled, so that the * signal handler would fail to do anything. * * Because we didn't bother to reset the timer in disable_alarm(), * it's possible that a previously-set interrupt will fire between * enable_alarm() and setitimer(). This is safe, however. There are * two possible outcomes: * * 1. The signal handler finds nothing to do (because the nearest * timeout event is still in the future). It will re-set the timer * and return. Then we'll overwrite the timer value with a new one. * This will mean that the timer fires a little later than we * intended, but only by the amount of time it takes for the signal * handler to do nothing useful, which shouldn't be much. * * 2. The signal handler executes and removes one or more timeout * events. When it returns, either the queue is now empty or the * frontmost event is later than the one we looked at above. So we'll * overwrite the timer value with one that is too soon (plus or minus * the signal handler's execution time), causing a useless interrupt * to occur. But the handler will then re-set the timer and * everything will still work as expected. * * Since these cases are of very low probability (the window here * being quite narrow), it's not worth adding cycles to the mainline * code to prevent occasional wasted interrupts. */ enable_alarm(); /* Set the alarm timer */ if (setitimer(ITIMER_REAL, &timeval, NULL) != 0) elog(FATAL, "could not enable SIGALRM timer: %m"); } }