Exemple #1
0
static unsigned int my_sleep(unsigned int seconds){
   struct timespec ts;

    ts.tv_sec = seconds;
    ts.tv_nsec = 0;
    return my_nanosleep(&ts);
};
Exemple #2
0
void
rate_sleep(struct rate *r)
{
	struct timespec now, til;

	/* what clock to use depends on whether clock_nanosleep() is available */
#if HAVE_CLOCK_NANOSLEEP
	static const clockid_t rate_clock = CLOCK_MONOTONIC;
#else
	static const clockid_t rate_clock = CLOCK_REALTIME;
#endif

	if (r == NULL)
		return;

	/* update the event counter */
	r->count += 1;

	/* fetch the current time */
	clock_gettime(rate_clock, &now);

	/* special case: if this is the first call to rate_sleep(),
	 * calculate when the next tick will be. this is a little bit more
	 * accurate than calculating it in rate_init().
	 */
	if (r->count == 1) {
		r->start = now;
		r->next_tick = calc_next_tick(&now, &r->period);
	}

	/* adjust the rate and period every 'freq' events.
	 * skip the first window of 'freq' events.
	 * disabled if 'freq' is 0.
	 */
	if (r->freq != 0 && (r->count % r->freq) == 0 && r->count > r->freq)
		adjust_rate(r, &now);

	/* 'til', amount of time remaining until the next tick */
	til = r->next_tick;
	my_timespec_sub(&now, &til);

	/* if 'til' is in the past, don't bother sleeping */
	if (ts_nanos(&til) > 0) {
		/* do the sleep */
#if HAVE_CLOCK_NANOSLEEP
		clock_nanosleep(rate_clock, TIMER_ABSTIME, &r->next_tick, NULL);
#else
		struct timespec rel;
		rel = r->next_tick;
		my_timespec_sub(&now, &rel);
		my_nanosleep(&rel);
#endif

		/* re-fetch the current time */
		clock_gettime(rate_clock, &now);
	}

	/* calculate the next tick */
	r->next_tick = calc_next_tick(&now, &r->period);
}