struct TASK *task_init(struct MEMMAN *memman) { int i; struct TASK *task; struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) ADR_GDT; taskctl = (struct TASKCTL *) memman_alloc_4k(memman, sizeof(struct TASKCTL)); for (i = 0; i < MAX_TASKS; i++) { taskctl->tasks0[i].flags = 0; taskctl->tasks0[i].sel = (TASK_GDT0 + i) * 8; set_segmdesc(gdt + TASK_GDT0 + i, 103, (int) &taskctl->tasks0[i].tss, AR_TSS32); } task = task_alloc(); task->flags = 2; taskctl->running = 1; taskctl->now = 0; taskctl->tasks[0] = task; load_tr(task->sel); task_timer = timer_alloc(); timer_settime(task_timer, 2); return task; }
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); }
struct TASK *task_init(struct MEMMAN *memman) { int i; struct TASK *task, *idle; struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) ADR_GDT; taskctl = (struct TASKCTL *) memman_alloc_4k(memman, sizeof (struct TASKCTL)); for (i = 0; i < MAX_TASKS; i++) { taskctl->tasks0[i].flags = 0; taskctl->tasks0[i].sel = (TASK_GDT0 + i) * 8; set_segmdesc(gdt + TASK_GDT0 + i, 103, (int) &taskctl->tasks0[i].tss, AR_TSS32); } for (i = 0; i < MAX_TASKLEVELS; i++) { taskctl->level[i].running = 0; taskctl->level[i].now = 0; } task = task_alloc(); task->flags = 2; /* 動作中マーク */ task->priority = 2; /* 0.02秒 */ task->level = 0; /* 最高レベル */ task_add(task); task_switchsub(); /* レベル設定 */ load_tr(task->sel); task_timer = timer_alloc(); timer_settime(task_timer, task->priority); idle = task_alloc(); idle->tss.esp = memman_alloc_4k(memman, 64 * 1024) + 64 * 1024; idle->tss.eip = (int) &task_idle; idle->tss.es = 1 * 8; idle->tss.cs = 2 * 8; idle->tss.ss = 1 * 8; idle->tss.ds = 1 * 8; idle->tss.fs = 1 * 8; idle->tss.gs = 1 * 8; task_run(idle, MAX_TASKLEVELS - 1, 1); return task; }
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(); }
void GPS_Init(void) { struct sigevent evp; struct itimerspec ts; timer_t timer; int ret; fprintf(stderr, "Initial GPS...\n"); serial_initial("/dev/ttySAC2", &GpsDevice, 9600); Mileage = 0; // initial the rwlock. pthread_rwlock_init(&GPS_rwlock, NULL); // set a timer for mileage integral. memset(&evp, 0, sizeof(evp)); evp.sigev_value.sival_ptr = &timer; evp.sigev_notify = SIGEV_THREAD; evp.sigev_notify_function = Mileage_Integral; ret = timer_create(CLOCK_REALTIME, &evp, &timer); if(ret){ perror("timer_Create"); } ts.it_interval.tv_sec = 10; ts.it_interval.tv_nsec = 0; ts.it_value.tv_sec = 10; ts.it_value.tv_nsec = 0; ret = timer_settime(timer, TIMER_ABSTIME, &ts, NULL); if( ret ){ perror("timer_settime"); } gps_tmp.lat = 3853.1876; gps_tmp.lon = 12131.8528; gps_tmp.speed = 32; }
/* 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); }
int main(void) { 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; }
t_Error drawInfoBar(info_data_t data) { infoBar.channelNumber = data.channelNumber; infoBar.teletextExist = data.teletextExist; infoBar.audioPID = data.audioPID; infoBar.videoPID = data.videoPID; strcpy(infoBar.date, data.date); infoBar.visible = 1; render(); // Specify the timer timeout time and set new timer. infoTimerSpec.it_value.tv_sec = INFO_TIME; infoTimerSpec.it_value.tv_nsec = 0; if (-1 == timer_settime(infoTimerId, 0, &infoTimerSpec, &infoTimerSpecOld)) { printf("ERROR: %s failed while creating timer.\n", __func__); return ERROR; } return NO_ERROR; }
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; }
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); }
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 ... */ } }
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; }
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 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; }
/***************************************************************************** * Function * mtk_nfc_sys_timer_stop * DESCRIPTION * Start a timer * PARAMETERS * timer_slot [IN] a valid timer slot * RETURNS * NONE *****************************************************************************/ void mtk_nfc_sys_timer_stop ( MTK_NFC_TIMER_E timer_slot ) { struct itimerspec its = {{0, 0}, {0, 0}}; if (timer_slot >= MTK_NFC_TIMER_MAX_NUM) { #ifdef DEBUG_LOG ALOGD("[TIMER]timer_slot(%d) exceed max num of nfc timer\r\n", timer_slot); #endif return; } if (nfc_timer_table[timer_slot].is_used == 0) { #ifdef DEBUG_LOG ALOGD("[TIMER]timer_slot(%d) already be deleted\r\n", timer_slot); #endif return; } if (nfc_timer_table[timer_slot].is_stopped == 1) { #ifdef DEBUG_LOG ALOGD("[TIMER]timer_slot(%d) already be stopped\r\n", timer_slot); #endif return; } nfc_timer_table[timer_slot].is_stopped = 1; timer_settime(nfc_timer_table[timer_slot].handle, 0, &its, NULL); #ifdef DEBUG_LOG ALOGD("[TIMER]timer_slot(%d) stop, handle(%d)\r\n", timer_slot, nfc_timer_table[timer_slot].handle); #endif }
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; }
static void set_alarm (void) { if (atimers) { #ifdef HAVE_SETITIMER struct itimerval it; #endif struct timespec now, interval; #ifdef HAVE_ITIMERSPEC if (alarm_timer_ok) { struct itimerspec ispec; ispec.it_value = atimers->expiration; ispec.it_interval.tv_sec = ispec.it_interval.tv_nsec = 0; if (timer_settime (alarm_timer, 0, &ispec, 0) == 0) return; } #endif /* Determine interval till the next timer is ripe. Don't set the interval to 0; this disables the timer. */ now = current_timespec (); interval = (timespec_cmp (atimers->expiration, now) <= 0 ? make_timespec (0, 1000 * 1000) : timespec_sub (atimers->expiration, now)); #ifdef HAVE_SETITIMER memset (&it, 0, sizeof it); it.it_value = make_timeval (interval); setitimer (ITIMER_REAL, &it, 0); #else /* not HAVE_SETITIMER */ alarm (max (interval.tv_sec, 1)); #endif /* not HAVE_SETITIMER */ } }
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; }
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; }
static void drm_hdcp_stop_link_checking() { int ret; struct itimerspec its; if (g_hdcpStatusCheckTimer == 0) { ALOGV("HDCP status checking timer has been deleted."); return; } its.it_value.tv_sec = 0; its.it_value.tv_nsec = 0; its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; ret = timer_settime(g_hdcpStatusCheckTimer, TIMER_ABSTIME, &its, NULL); if (ret != 0) { ALOGE("Failed to reset HDCP status checking timer."); } timer_delete(g_hdcpStatusCheckTimer); g_hdcpStatusCheckTimer = 0; }
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"); }
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); }
static int iv_fd_port_set_poll_timeout(struct iv_state *st, const struct timespec *abs) { struct itimerspec val; int ret; if (st->u.port.timer_id == -1 && !iv_fd_port_timer_create(st)) { method = &iv_fd_poll_method_port; return 0; } val.it_interval.tv_sec = 0; val.it_interval.tv_nsec = 0; val.it_value = *abs; ret = timer_settime(st->u.port.timer_id, TIMER_ABSTIME, &val, NULL); if (ret < 0) { iv_fatal("iv_fd_port_set_poll_timeout: got error %d[%s]", errno, strerror(errno)); } return 1; }
void WaitableTimerSignalHandler(int signum, siginfo_t* siginfo, void* arg) { WINPR_TIMER* timer = siginfo->si_value.sival_ptr; if (!timer || (signum != SIGALRM)) return; if (timer->pfnCompletionRoutine) { timer->pfnCompletionRoutine(timer->lpArgToCompletionRoutine, 0, 0); if (timer->lPeriod) { timer->timeout.it_interval.tv_sec = (timer->lPeriod / 1000); /* seconds */ timer->timeout.it_interval.tv_nsec = ((timer->lPeriod % 1000) * 1000000); /* nanoseconds */ if ((timer_settime(timer->tid, 0, &(timer->timeout), NULL)) != 0) { WLog_ERR(TAG,"timer_settime"); } } } }
void StopTimer( timer_t timer_id, struct itimerspec *timerdata ) { static int firstTime = 1; struct itimerspec *pOld; struct itimerspec odata; /* * We do not care about the old value. But we need to exercise * getting and not getting the return value back. */ pOld = (firstTime == 1) ? NULL : &odata; firstTime = 0; timerdata->it_value.tv_sec = 0; timerdata->it_value.tv_nsec = 0; if (timer_settime(timer_id,POSIX_TIMER_RELATIVE,timerdata,pOld) == -1) { perror ("Error in timer setting\n"); rtems_test_exit(0); } }
/* Time are expressed in us */ t_uint32 METH(startTimer)(t_uint32 fisrtAlarm, t_uint32 period) { struct itimerspec timer; if (fisrtAlarm) { timer.it_value.tv_sec = fisrtAlarm/1000000; timer.it_value.tv_nsec = (fisrtAlarm%1000000)*1000; } else if (period) { timer.it_value.tv_sec = period/1000000; timer.it_value.tv_nsec = (period%1000000)*1000; } else { /* If it_value is zero, the timer is disarmed; that's not what we want */ timer.it_value.tv_sec = 0; timer.it_value.tv_nsec = 1; } timer.it_interval.tv_sec = period/1000000; timer.it_interval.tv_nsec = (period%1000000)*1000; if (timer_settime(timerid, 0, &timer, NULL)) { perror("timer_settime() in Timer failed"); return (t_uint32)NMF_INTEGRATION_ERROR0; } return 0; }
void task_switch(void) { struct _tasklevel *tl = &taskctl->level[taskctl->now_lv]; struct _task *new_task, *now_task = tl->tasks[tl->now]; /* Switch to current level:next task */ tl->now++; /* Reset to beginning when proceed to the end */ if (tl->now == tl->running) { tl->now = 0; } /* If it requires level switching */ if (taskctl->lv_change != 0) { task_switchsub(); tl = &taskctl->level[taskctl->now_lv]; } new_task = tl->tasks[tl->now]; /* Timeslice allocation depends on its priority */ timer_settime(task_timer, new_task->priority); /* Only do JMP when there is different task */ if (new_task != now_task) { farjmp(0, new_task->sel); } return; }
static void dynticks_rearm_timer(struct qemu_alarm_timer *t) { timer_t host_timer = t->timer; struct itimerspec timeout; int64_t nearest_delta_ns = INT64_MAX; int64_t current_ns; assert(alarm_has_dynticks(t)); if (!active_timers[QEMU_CLOCK_REALTIME] && !active_timers[QEMU_CLOCK_VIRTUAL] && !active_timers[QEMU_CLOCK_HOST]) return; nearest_delta_ns = qemu_next_alarm_deadline(); if (nearest_delta_ns < MIN_TIMER_REARM_NS) nearest_delta_ns = MIN_TIMER_REARM_NS; /* check whether a timer is already running */ if (timer_gettime(host_timer, &timeout)) { perror("gettime"); fprintf(stderr, "Internal timer error: aborting\n"); exit(1); } current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec; if (current_ns && current_ns <= nearest_delta_ns) return; timeout.it_interval.tv_sec = 0; timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ timeout.it_value.tv_sec = nearest_delta_ns / 1000000000; timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000; if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { perror("settime"); fprintf(stderr, "Internal timer error: aborting\n"); exit(1); } }
// 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 }