int main(int argc, char *argv[]) { struct sigevent ev; timer_t tid; struct itimerspec its; ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGTOTEST; if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { perror("timer_create() did not return success\n"); return PTS_UNRESOLVED; } if (timer_delete(tid) != 0) { perror("timer_delete() did not return success\n"); return PTS_UNRESOLVED; } its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; its.it_value.tv_sec = TIMERSEC; its.it_value.tv_nsec = 0; if (timer_settime(tid, 0, &its, NULL) == -1) { if (errno==EINVAL) { printf("Test PASSED\n"); return PTS_PASS; } else { printf("errno!=EINVAL after a timer_delete()\n"); return PTS_FAIL; } } else { printf("timer_settime() did not fail after timer_delete()\n"); return PTS_FAIL; } printf("This code should not be executed\n"); return PTS_UNRESOLVED; }
int main() { struct itimerspec ts; struct sigaction sa; struct sigevent sev; timer_t tid; int j; /* Establish handler for notification signal */ sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = handler; sigemptyset(&sa.sa_mask); if (sigaction(TIMER_SIG, &sa, NULL) == -1){ perror("sigaction"); } /* Create and start timer */ ts.it_interval.tv_sec = 2; ts.it_interval.tv_nsec = 10; ts.it_value.tv_sec = 1; ts.it_value.tv_nsec = 20; /* pass timer id to handler */ sev.sigev_value.sival_ptr = &tid; sev.sigev_notify = SIGEV_SIGNAL; /* Notify via signal */ sev.sigev_signo = SIGRTMAX; /* Notify using this signal */ if (timer_create(CLOCK_REALTIME, &sev, &tid) == -1){ perror("timer_create"); exit(1); } if (timer_settime(tid, 0, &ts, NULL) == -1) perror("timer_settime"); for (;;) /* Wait for incoming timer signals */ pause(); }
int main(int argc, char **argv) { struct sigevent ev; struct itimerspec ts; sigset_t set; timer_t tid; char *cmd = argv[0]; ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGUSR1; if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1) { (void) fprintf(stderr, "%s: cannot create CLOCK_HIGHRES " "timer: %s\n", cmd, strerror(errno)); exit(EXIT_FAILURE); } (void) sigemptyset(&set); (void) sigaddset(&set, SIGUSR1); (void) sigprocmask(SIG_BLOCK, &set, NULL); ts.it_value.tv_sec = 1; ts.it_value.tv_nsec = 0; ts.it_interval.tv_sec = 0; ts.it_interval.tv_nsec = NANOSEC / 2; if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1) { (void) fprintf(stderr, "%s: timer_settime() failed: %s\n", cmd, strerror(errno)); exit(EXIT_FAILURE); } for (;;) { (void) sigwait(&set); } /*NOTREACHED*/ return (0); }
static int dynticks_start_timer(struct qemu_alarm_timer *t) { struct sigevent ev; timer_t host_timer; struct sigaction act; sigfillset(&act.sa_mask); act.sa_flags = 0; act.sa_handler = host_alarm_handler; sigaction(SIGALRM, &act, NULL); /* * Initialize ev struct to 0 to avoid valgrind complaining * about uninitialized data in timer_create call */ memset(&ev, 0, sizeof(ev)); ev.sigev_value.sival_int = 0; ev.sigev_notify = SIGEV_SIGNAL; #ifdef SIGEV_THREAD_ID if (qemu_signalfd_available()) { ev.sigev_notify = SIGEV_THREAD_ID; ev._sigev_un._tid = qemu_get_thread_id(); } #endif /* SIGEV_THREAD_ID */ ev.sigev_signo = SIGALRM; if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { perror("timer_create"); /* disable dynticks */ fprintf(stderr, "Dynamic Ticks disabled\n"); return -1; } t->timer = host_timer; return 0; }
asyncOp *selectNewAsyncOp(asyncBase *base) { asyncOp *op = new asyncOp; if (op) { struct sigevent sEvent; op->internalBuffer = 0; op->internalBufferSize = 0; op->list = 0; op->next = 0; op->prev = 0; sEvent.sigev_notify = SIGEV_SIGNAL; sEvent.sigev_signo = SIGRTMIN; sEvent.sigev_value.sival_ptr = op; if (timer_create(CLOCK_REALTIME, &sEvent, &op->timerId) == -1) { fprintf(stderr, " * newSelectOp: timer_create error %s\n", strerror(errno)); } } return op; }
DLLEXPORT int jl_profile_start_timer(void) { struct sigevent sigprof; struct sigaction sa; sigset_t ss; // Make sure SIGUSR2 is unblocked sigemptyset(&ss); sigaddset(&ss, SIGUSR2); if (sigprocmask(SIG_UNBLOCK, &ss, NULL) == -1) return -4; // Establish the signal handler memset(&sa, 0, sizeof(struct sigaction)); sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = profile_bt; sigemptyset(&sa.sa_mask); if (sigaction(SIGUSR2, &sa, NULL) == -1) return -1; // Establish the signal event memset(&sigprof, 0, sizeof(struct sigevent)); sigprof.sigev_notify = SIGEV_SIGNAL; sigprof.sigev_signo = SIGUSR2; sigprof.sigev_value.sival_ptr = &timerprof; if (timer_create(CLOCK_REALTIME, &sigprof, &timerprof) == -1) return -2; // Start the timer itsprof.it_interval.tv_sec = nsecprof/GIGA; itsprof.it_interval.tv_nsec = nsecprof%GIGA; itsprof.it_value.tv_sec = nsecprof/GIGA; itsprof.it_value.tv_nsec = nsecprof%GIGA; if (timer_settime(timerprof, 0, &itsprof, NULL) == -1) return -3; running = 1; return 0; }
err_t http_cache_init(struct ip_addr server, const char *path, void (*callback)(void)) { struct timer *cache_timer; /* timer for triggering cache timeouts */ init_callback = callback; /* FIXME: Start the trace */ #if ENABLE_WEB_TRACING printf("Starting tracing\n"); errval_t err = trace_control(TRACE_EVENT(TRACE_SUBSYS_NET, TRACE_EVENT_NET_START, 0), TRACE_EVENT(TRACE_SUBSYS_NET, TRACE_EVENT_NET_STOP, 0), 0); if(err_is_fail(err)) { USER_PANIC_ERR(err, "trace_control failed"); } trace_event(TRACE_SUBSYS_NET, TRACE_EVENT_NET_START, 0); #else // ENABLE_WEB_TRACING printf("Tracing not enabled\n"); #endif // ENABLE_WEB_TRACING my_nfs_client = nfs_mount(server, path, mount_callback, NULL); assert(my_nfs_client != NULL); /* creating the empty cache */ cache_table = NULL; create_404_page_cache(); cache_timer = timer_create(MAX_STALENESS, true, cache_timeout_event, cache_table); assert (cache_timer != NULL); if (cache_timer == NULL) { printf ("http_cache_init failed in timer_create\n"); return ERR_MEM; } timer_start(cache_timer); DEBUGPRINT ("http_cache_init done\n"); return ERR_OK; } /* end function: http_cache_init */
static int set_timer(timer_t *tid, unsigned int timeout) { struct sigevent sigev = {}; struct itimerspec it = {}; sigev.sigev_notify = SIGEV_SIGNAL; sigev.sigev_signo = SIGRTMIN; sigev.sigev_value.sival_ptr = tid; if (timer_create(CLOCK_MONOTONIC, &sigev, tid)) { ploop_err(errno, "timer_create"); return -1; } it.it_value.tv_sec = timeout; it.it_value.tv_nsec = 0; if (timer_settime(*tid, 0, &it, NULL)) { ploop_err(errno, "timer_settime"); return -1; } return 0; }
void streamVideo(send_frame_data_t *data) { // The following snippet is used to create and start a new timer that runs // every 40 ms. deleteTimer(data); struct sigevent play_event; struct itimerspec play_interval; memset(&play_event, 0, sizeof(play_event)); play_event.sigev_notify = SIGEV_THREAD; play_event.sigev_value.sival_ptr = data; play_event.sigev_notify_function = send_frame; play_interval.it_interval.tv_sec = 0; play_interval.it_interval.tv_nsec = 40 * 1000000; // 40 ms in ns play_interval.it_value.tv_sec = 0; play_interval.it_value.tv_nsec = 1; // can't be zero timer_create(CLOCK_REALTIME, &play_event, &data->play_timer); timer_settime(data->play_timer, 0, &play_interval, NULL); }
TEST(time, timer_create) { sigevent_t se; memset(&se, 0, sizeof(se)); se.sigev_notify = SIGEV_THREAD; se.sigev_notify_function = NoOpNotifyFunction; timer_t timer_id; ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); pid_t pid = fork(); ASSERT_NE(-1, pid) << strerror(errno); if (pid == 0) { // Timers are not inherited by the child. ASSERT_EQ(-1, timer_delete(timer_id)); ASSERT_EQ(EINVAL, errno); _exit(0); } AssertChildExited(pid, 0); ASSERT_EQ(0, timer_delete(timer_id)); }
void *time_update( void *ptr ) { struct sigevent event; struct itimerspec itime; timer_t timer_id; int chid, rcvid; my_message_t msg; chid = ChannelCreate(0); event.sigev_notify = SIGEV_PULSE; event.sigev_coid = ConnectAttach(ND_LOCAL_NODE, 0, chid, _NTO_SIDE_CHANNEL, 0); event.sigev_priority = getprio(0); event.sigev_code = MY_PULSE_CODE; timer_create(CLOCK_REALTIME, &event, &timer_id); itime.it_value.tv_sec = 0; /* 100 ms = .1 secs */ itime.it_value.tv_nsec = 100000000; itime.it_interval.tv_sec = 0; /* 100 ms = .1 secs */ itime.it_interval.tv_nsec = 100000000; timer_settime(timer_id, 0, &itime, NULL); // This for loop will update the global_time for every 100 ms which is 1 minute in simulation time. for (;;) { rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL); if (rcvid == 0) { /* we got a pulse */ if (msg.pulse.code == MY_PULSE_CODE) { if (global_time > 0) global_time--; else break; //printf("we got a pulse from our timer and time = %d\n", global_time); } /* else other pulses ... */ } /* else other messages ... */ } }
AlarmTimer::AlarmTimer(void) :timerId(0),armed(false),expired(false) { /* Initialize the alarm timer class if this is the first one: */ if(numAlarmTimers==0) { /* Install the signal handler: */ struct sigaction sigAction; sigAction.sa_sigaction=signalHandler; sigemptyset(&sigAction.sa_mask); sigAction.sa_flags=SA_SIGINFO; sigaction(SIGRTMIN,&sigAction,NULL); } ++numAlarmTimers; /* Create a per-process timer: */ struct sigevent timerEvent; timerEvent.sigev_notify=SIGEV_SIGNAL; timerEvent.sigev_signo=SIGRTMIN; timerEvent.sigev_value.sival_ptr=this; timer_create(CLOCK_REALTIME,&timerEvent,&timerId); }
int setTimer(long long int duration_ns, TimerCallback callback, int signo) { printf("duration_ns = %lld\n", duration_ns); struct sigaction action; struct sigevent evp; struct itimerspec ispec; timer_t timerid = 0; memset(&action, 0, sizeof(action)); memset(&evp, 0, sizeof(evp)); /* set signal handler */ action.sa_sigaction = SignalHandler; action.sa_flags = SA_SIGINFO | SA_RESTART; sigemptyset(&action.sa_mask); if(sigaction(SIGRTMIN + 1, &action, NULL) < 0){ perror("sigaction error"); exit(1); } /* set intarval timer (10ms) */ evp.sigev_notify = SIGEV_SIGNAL; evp.sigev_signo = signo; evp.sigev_value.sival_ptr = (void*)(callback); if(timer_create(CLOCK_REALTIME, &evp, &timerid) < 0){ perror("timer_create error"); exit(1); } ispec.it_interval.tv_sec = 0; ispec.it_interval.tv_nsec = 0; // 10000000; One shot timer ispec.it_value.tv_sec = int(duration_ns / 1000000000LL); ispec.it_value.tv_nsec = int(duration_ns % 1000000000LL); if(timer_settime(timerid, 0, &ispec, NULL) < 0){ perror("timer_settime error"); exit(1); } return 0; }
void GKI_init(void) { pthread_mutexattr_t attr; tGKI_OS *p_os; memset (&gki_cb, 0, sizeof (gki_cb)); gki_buffer_init(); gki_timers_init(); alarm_service_init(); gki_cb.com.OSTicks = (UINT32) times(0); pthread_mutexattr_init(&attr); #ifndef __CYGWIN__ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); #endif p_os = &gki_cb.os; pthread_mutex_init(&p_os->GKI_mutex, &attr); /* pthread_mutex_init(&GKI_sched_mutex, NULL); */ #if (GKI_DEBUG == TRUE) pthread_mutex_init(&p_os->GKI_trace_mutex, NULL); #endif /* pthread_mutex_init(&thread_delay_mutex, NULL); */ /* used in GKI_delay */ /* pthread_cond_init (&thread_delay_cond, NULL); */ struct sigevent sigevent; memset(&sigevent, 0, sizeof(sigevent)); sigevent.sigev_notify = SIGEV_THREAD; sigevent.sigev_notify_function = (void (*)(union sigval))bt_alarm_cb; sigevent.sigev_value.sival_ptr = NULL; if (timer_create(CLOCK_REALTIME, &sigevent, &posix_timer) == -1) { ALOGE("%s unable to create POSIX timer: %s", __func__, strerror(errno)); timer_created = false; } else { timer_created = true; } }
static int systimer_create(systimer_t* id, unsigned int period, int oneshot, systimer_proc callback, void* cbparam) { struct sigevent sev; struct itimerspec tv; timer_context_t* ctx; ctx = (timer_context_t*)malloc(sizeof(timer_context_t)); if(!ctx) return -ENOMEM;; memset(ctx, 0, sizeof(timer_context_t)); ctx->callback = callback; ctx->cbparam = cbparam; memset(&sev, 0, sizeof(sev)); sev.sigev_notify = SIGEV_THREAD; sev.sigev_value.sival_ptr = ctx; sev.sigev_notify_function = timer_schd_worker; if(0 != timer_create(CLOCK_MONOTONIC, &sev, &ctx->timerId)) { free(ctx); return -errno; } tv.it_interval.tv_sec = period / 1000; tv.it_interval.tv_nsec = (period % 1000) * 1000000; // 10(-9)second tv.it_value.tv_sec = tv.it_interval.tv_sec; tv.it_value.tv_nsec = tv.it_interval.tv_nsec; if(0 != timer_settime(ctx->timerId, 0, &tv, NULL)) { timer_delete(ctx->timerId); free(ctx); return -errno; } *id = (systimer_t)ctx; return 0; }
int main(void) { struct sigevent ev; timer_t tid; struct itimerspec its, oits; ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGCONT; its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; its.it_value.tv_sec = TIMERSEC; its.it_value.tv_nsec = 0; if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { perror("timer_create() did not return success\n"); return PTS_UNRESOLVED; } if (timer_settime(tid, 0, &its, &oits) != 0) { perror("timer_settime() did not return success\n"); return PTS_UNRESOLVED; } if ((0 == oits.it_value.tv_sec) && (0 == oits.it_value.tv_nsec) && (0 == oits.it_interval.tv_sec) && (0 == oits.it_interval.tv_nsec)) { printf("Test PASSED\n"); return PTS_PASS; } printf("Test FAILED: value: tv_sec %d tv_nsec %d\n", (int)oits.it_value.tv_sec, (int)oits.it_value.tv_nsec); printf("Test FAILED: interval: tv_sec %d tv_nsec %d\n", (int)oits.it_interval.tv_sec, (int)oits.it_interval.tv_nsec); return PTS_FAIL; }
int rb_ports_sched_event(struct ev_entry *event, int when) { timer_t *id; struct sigevent ev; port_notify_t not; struct itimerspec ts; event->comm_ptr = rb_malloc(sizeof(timer_t)); id = event->comm_ptr; memset(&ev, 0, sizeof(ev)); ev.sigev_notify = SIGEV_PORT; ev.sigev_value.sival_ptr = ¬ memset(¬, 0, sizeof(not)); not.portnfy_port = pe; not.portnfy_user = event; if(timer_create(CLOCK_REALTIME, &ev, id) < 0) { rb_lib_log("timer_create: %s\n", strerror(errno)); return 0; } memset(&ts, 0, sizeof(ts)); ts.it_value.tv_sec = when; ts.it_value.tv_nsec = 0; if(event->frequency != 0) ts.it_interval = ts.it_value; if(timer_settime(*id, 0, &ts, NULL) < 0) { rb_lib_log("timer_settime: %s\n", strerror(errno)); return 0; } return 1; }
int event_queue_add_timer(int eq, int *id, int sec) { static int timer_id = 0xffffff00; port_notify_t pnotif; struct sigevent sigev; itimerspec_t it; timer_t tid; timer_id++; pnotif.portnfy_port = eq; pnotif.portnfy_user = (void *) (long) timer_id; sigev.sigev_notify = SIGEV_PORT; sigev.sigev_value.sival_ptr = &pnotif; if (timer_create(CLOCK_REALTIME, &sigev, &tid) < 0) { uwsgi_error("timer_create()"); return -1; } it.it_value.tv_sec = sec; it.it_value.tv_nsec = 0; it.it_interval.tv_sec = sec; it.it_interval.tv_nsec = 0; if (timer_settime(tid, 0, &it, NULL) < 0) { uwsgi_error("timer_settime()"); return -1; } *id = timer_id; return *id; }
utimer_t* utimer( socket_t* socket, packet_t* packet, timer_type_t type) { utimer_t* new_timer = calloc(1, sizeof(utimer_t)); new_timer->event = calloc(1, sizeof(struct sigevent)); new_timer->event->sigev_notify = SIGEV_THREAD; new_timer->event->sigev_notify_function = socket_timer_handler; new_timer->event->sigev_notify_attributes = NULL; new_timer->event->sigev_signo = RUDP_SOCKET_SIGNAL; new_timer->event->sigev_value.sival_ptr = new_timer; new_timer->socket = socket; new_timer->packet = packet; new_timer->type = type; timer_create(CLOCK_REALTIME, new_timer->event, &new_timer->timer); debug_print("utimer(): %p\n", new_timer); return new_timer; }
int main(int argc, char *argv[]) { struct sigevent ev; timer_t tid; ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGALRM; if (timer_create(INVALIDCLOCKID, &ev, &tid) == -1) { if (EINVAL == errno) { printf("Test PASSED\n"); return PTS_PASS; } else { printf("errno != EINVAL\n"); printf("Test FAILED\n"); return PTS_FAIL; } } else { printf("timer_create returned success\n"); return PTS_UNRESOLVED; } return PTS_UNRESOLVED; }
void MW_setTaskPeriod(double periodInSeconds, int sigNo) { timer_t timerId; struct sigevent sev; struct itimerspec its; int ret; /* Create a timer */ sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = sigNo; sev.sigev_value.sival_ptr = &timerId; ret = timer_create(CLOCK_REALTIME, &sev, &timerId); CHECK_STATUS(ret, "timer_create"); /* Arm real-time scheduling timer */ its.it_value.tv_sec = (time_t)periodInSeconds; its.it_value.tv_nsec = (periodInSeconds - (time_t)periodInSeconds) * 1000000000; its.it_interval.tv_sec = its.it_value.tv_sec; its.it_interval.tv_nsec = its.it_value.tv_nsec; ret = timer_settime(timerId, 0, &its, NULL); CHECK_STATUS(ret, "timer_settime"); }
JL_DLLEXPORT int jl_profile_start_timer(void) { struct sigevent sigprof; // Establish the signal event memset(&sigprof, 0, sizeof(struct sigevent)); sigprof.sigev_notify = SIGEV_SIGNAL; sigprof.sigev_signo = SIGUSR1; sigprof.sigev_value.sival_ptr = &timerprof; if (timer_create(CLOCK_REALTIME, &sigprof, &timerprof) == -1) return -2; // Start the timer itsprof.it_interval.tv_sec = nsecprof/GIGA; itsprof.it_interval.tv_nsec = nsecprof%GIGA; itsprof.it_value.tv_sec = nsecprof/GIGA; itsprof.it_value.tv_nsec = nsecprof%GIGA; if (timer_settime(timerprof, 0, &itsprof, NULL) == -1) return -3; running = 1; return 0; }
TEST(time, timer_create_SIGEV_SIGNAL) { sigevent_t se; memset(&se, 0, sizeof(se)); se.sigev_notify = SIGEV_SIGNAL; se.sigev_signo = SIGUSR1; timer_t timer_id; ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler); ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); itimerspec ts; ts.it_value.tv_sec = 0; ts.it_value.tv_nsec = 1; ts.it_interval.tv_sec = 0; ts.it_interval.tv_nsec = 0; ASSERT_EQ(0, timer_settime(timer_id, TIMER_ABSTIME, &ts, NULL)); usleep(500000); ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); }
/* int timer_helper::m_settimer(int timerID,callback_func func,int expireMS, int intervalMS) { int res = -1; timer_t *t_timerid = new timer_t; struct sigevent evp; struct itimerspec its; struct sigaction sa; struct arg_callback *uc=new arg_callback; uc->timerid=timerID; uc->timerID=t_timerid; uc->mclass=this; sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = timer_helper::timer_callback; sigemptyset(&sa.sa_mask); sigaction(SIGUSR1, &sa, NULL); evp.sigev_value.sival_ptr = uc; evp.sigev_notify = SIGEV_SIGNAL; evp.sigev_signo = SIGUSR1; res = timer_create(CLOCK_REALTIME, &evp, t_timerid); this->tt.insert(std::pair<int,timer_t *>(timerID,t_timerid)); its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = intervalMS * 1000000; its.it_value.tv_sec = 0; its.it_value.tv_nsec = expireMS * 1000000; timer_settime(*t_timerid, 0, &its, NULL); return res; } */ void timer_helper::m_initTimer(timer_t *timerId, callback_func function) { struct sigevent sev; struct sigaction sa; struct arg_callback *uc=new arg_callback; uc->timerID = timerId; uc->func = function; uc->mclass = this; // sev.sigev_notify = SIGEV_THREAD; sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = timer_helper::timer_callback; sigemptyset(&sa.sa_mask); sigaction(SIGUSR1, &sa, NULL); sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGUSR1; sev.sigev_value.sival_ptr = uc; // sev.sigev_notify_function = function; sev.sigev_notify_attributes = NULL; if (timer_create(CLOCK_REALTIME, &sev, timerId) == -1){ throw std::runtime_error(std::string("ERROR:timer_create@timer_helper::m_initTimer!\n")); } this->ft.insert(std::pair<timer_t *,arg_callback *>(timerId,uc)); }
static bool lazy_initialize(void) { assert(alarms == NULL); pthread_mutex_init(&monitor, NULL); alarms = list_new(NULL); if (!alarms) { LOG_ERROR("%s unable to allocate alarm list.", __func__); return false; } struct sigevent sigevent; memset(&sigevent, 0, sizeof(sigevent)); sigevent.sigev_notify = SIGEV_THREAD; sigevent.sigev_notify_function = (void (*)(union sigval))timer_callback; if (timer_create(CLOCK_ID, &sigevent, &timer) == -1) { LOG_ERROR("%s unable to create timer: %s", __func__, strerror(errno)); return false; } alarm_expired = semaphore_new(0); if (!alarm_expired) { LOG_ERROR("%s unable to create alarm expired semaphore", __func__); return false; } callback_thread_active = true; callback_thread = thread_new("alarm_callbacks"); if (!callback_thread) { LOG_ERROR("%s unable to create alarm callback thread.", __func__); return false; } thread_set_priority(callback_thread, CALLBACK_THREAD_PRIORITY_HIGH); thread_post(callback_thread, callback_dispatch, NULL); return true; }
// XenonForceAlwaysOn is active - it doesn't need a timer, it is always on. // Xenon needs to be started once per process. // The number of milliseconds has to be greater than zero. // We need to create a semaphore and a thread. // If all of those happen, then we need a timer attached to a signal handler. void Xenon::start(uint64_t msec) { #ifndef __APPLE__ TRACE(1, "XenonForceAlwaysOn %d\n", RuntimeOption::XenonForceAlwaysOn); if (!RuntimeOption::XenonForceAlwaysOn && m_timerid == 0 && msec > 0 && sem_init(&m_timerTriggered, 0, 0) == 0 && pthread_create(&m_triggerThread, nullptr, s_waitThread, static_cast<void*>(&m_timerTriggered)) == 0) { time_t sec = msec / 1000; long nsec = (msec % 1000) * 1000000; TRACE(1, "Xenon::start periodic %ld seconds, %ld nanoseconds\n", sec, nsec); // for the initial timer, we want to stagger time for large installations unsigned int seed = time(nullptr); uint64_t msecInit = msec * (1.0 + rand_r(&seed) / (double)RAND_MAX); time_t fSec = msecInit / 1000; long fNsec = (msecInit % 1000) * 1000000; TRACE(1, "Xenon::start initial %ld seconds, %ld nanoseconds\n", fSec, fNsec); sigevent sev={}; sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGVTALRM; sev.sigev_value.sival_ptr = nullptr; // null for Xenon signals timer_create(CLOCK_REALTIME, &sev, &m_timerid); itimerspec ts={}; ts.it_value.tv_sec = fSec; ts.it_value.tv_nsec = fNsec; ts.it_interval.tv_sec = sec; ts.it_interval.tv_nsec = nsec; timer_settime(m_timerid, 0, &ts, nullptr); } #endif }
int CTimer::InitSignalTimer(TimerProc proc, void* proc_arg){ if(m_timer != 0){ NLOG_ERROR("Timer Inited ! m_timer(=%ld) != 0", (long)m_timer); return -1; } m_timerArg.proc = proc; m_timerArg.args = proc_arg; #if 0 sigset_t sigset; sigfillset (&sigset); sigdelset (&sigset, SIGUSR2); sigprocmask (SIG_SETMASK, &sigset, NULL); #endif struct sigaction action; memset(&action, 0, sizeof(action)); sigemptyset(&action.sa_mask); action.sa_flags = SA_SIGINFO|SA_RESTART; action.sa_sigaction = &s_sigaction; if(sigaction(SIGUSR2, &action, NULL) <0){ NLOG_ERROR("sigactin failed! err:%s", strerror(errno)); return -1; } struct sigevent evt; memset(&evt, 0, sizeof(evt)); evt.sigev_notify = SIGEV_SIGNAL; evt.sigev_signo = SIGUSR2; evt.sigev_value.sival_ptr = (void*)&m_timerArg; int ret = timer_create(CLOCK_REALTIME, &evt, &m_timer); if(m_name[0] == 0){//Name is NULL sprintf(m_name, "SignalTimer-%ld", (long)m_timer); } return ret; }
void storage_load(){ APP_LOG(APP_LOG_LEVEL_INFO,"storage_load()"); if(!persist_exists(STORAGE_KEY_VERSION)){ APP_LOG(APP_LOG_LEVEL_WARNING,"no version"); return; } if(persist_read_int(STORAGE_KEY_VERSION)!=STORAGE_VERSION){ APP_LOG(APP_LOG_LEVEL_WARNING,"wrong version"); return; } if(!persist_exists(STORAGE_KEY_COUNT)){ APP_LOG(APP_LOG_LEVEL_ERROR,"no count"); return; } uint8_t storage_count=(uint8_t)persist_read_int(STORAGE_KEY_COUNT); APP_LOG(APP_LOG_LEVEL_INFO,"storage count: %d",storage_count); Timer* timer; int result; for(uint8_t i=0;i<storage_count;i++){ if(!persist_exists(STORAGE_KEY_DATA+i)){ APP_LOG(APP_LOG_LEVEL_ERROR,"missing timer %d in storage %d",i,STORAGE_KEY_DATA+i); continue; } timer=timer_create(); result=persist_read_data(STORAGE_KEY_DATA+i,timer,sizeof(Timer)); if(result<0){ APP_LOG(APP_LOG_LEVEL_ERROR,"read error timer %d in storage %d",i,STORAGE_KEY_DATA+i); timer_destroy(timer); continue; } timers_add(timer); APP_LOG(APP_LOG_LEVEL_INFO,"loaded timer %d",i); } APP_LOG(APP_LOG_LEVEL_INFO,"loaded"); }
/** * @fn :tc_timer_timer_set_get_time * @brief :arm/disarm and fetch state of POSIX per-process timer * @scenario :arm/disarm and fetch state of POSIX per-process timer * API's covered :timer_create, timer_settime, timer_gettime * Preconditions :Creation of timer_id(timer_create) * Postconditions :none * @return :void */ static void tc_timer_timer_set_get_time(void) { int ret_chk = ERROR; clockid_t clockid = CLOCK_REALTIME; struct sigevent st_sigevent; struct itimerspec st_timer_spec_set; struct itimerspec st_timer_spec_get; timer_t timer_id; /* Set and enable alarm */ st_sigevent.sigev_notify = SIGEV_SIGNAL; st_sigevent.sigev_signo = sig_no; st_sigevent.sigev_value.sival_ptr = &timer_id; ret_chk = timer_create(clockid, &st_sigevent, &timer_id); TC_ASSERT_NEQ("timer_create", ret_chk, ERROR); TC_ASSERT_NOT_NULL("timer_create", timer_id); st_timer_spec_set.it_interval.tv_sec = 1; st_timer_spec_set.it_interval.tv_nsec = 0; /* interval; */ st_timer_spec_set.it_value.tv_sec = 1; st_timer_spec_set.it_value.tv_nsec = 0; /* expire; */ ret_chk = timer_settime(timer_id, 0, &st_timer_spec_set, NULL); /* Flag =1 :TIMER_ABSTIME */ TC_ASSERT_EQ_ERROR_CLEANUP("timer_settime", ret_chk, OK, errno, timer_delete(timer_id)); usleep(USECINT); ret_chk = timer_gettime(timer_id, &st_timer_spec_get); TC_ASSERT_EQ_ERROR_CLEANUP("timer_gettime", ret_chk, OK, errno, timer_delete(timer_id)); TC_ASSERT_GEQ_CLEANUP("timer_gettime", st_timer_spec_get.it_interval.tv_nsec, st_timer_spec_set.it_interval.tv_nsec, timer_delete(timer_id)); TC_ASSERT_GEQ_CLEANUP("timer_gettime", st_timer_spec_get.it_interval.tv_sec, st_timer_spec_set.it_interval.tv_sec, timer_delete(timer_id)); TC_ASSERT_GEQ_CLEANUP("timer_gettime", st_timer_spec_get.it_value.tv_sec, st_timer_spec_set.it_value.tv_sec, timer_delete(timer_id)); TC_ASSERT_GEQ_CLEANUP("timer_gettime", st_timer_spec_get.it_value.tv_nsec, st_timer_spec_set.it_value.tv_nsec, timer_delete(timer_id)); timer_delete(timer_id); TC_SUCCESS_RESULT(); }
/* Start the timeout after which we'll receive a SIGALRM. Round DURATION up to the next representable value. Treat out-of-range values as if they were maximal, as that's more useful in practice than reporting an error. '0' means don't timeout. */ static void settimeout (double duration, bool warn) { /* timer_settime() provides potentially nanosecond resolution. setitimer() is more portable (to Darwin for example), but only provides microsecond resolution and thus is a little more awkward to use with timespecs, as well as being deprecated by POSIX. Instead we fallback to single second resolution provided by alarm(). */ #if HAVE_TIMER_SETTIME struct timespec ts = dtotimespec (duration); struct itimerspec its = { {0, 0}, ts }; timer_t timerid; if (timer_create (CLOCK_REALTIME, NULL, &timerid) == 0) { if (timer_settime (timerid, 0, &its, NULL) == 0) return; else { if (warn) error (0, errno, _("warning: timer_settime")); timer_delete (timerid); } } else if (warn && errno != ENOSYS) error (0, errno, _("warning: timer_create")); #endif unsigned int timeint; if (UINT_MAX <= duration) timeint = UINT_MAX; else { unsigned int duration_floor = duration; timeint = duration_floor + (duration_floor < duration); } alarm (timeint); }