示例#1
0
static int make_periodic (unsigned int period, struct periodic_info *info)
{
	int ret;
	unsigned int ns;
	unsigned int sec;
	int fd;
	struct itimerspec itval;

	/* Create the timer */
	fd = timerfd_create (CLOCK_MONOTONIC, 0);
	info->wakeups_missed = 0;
	info->timer_fd = fd;
	if (fd == -1)
		return fd;

	/* Make the timer periodic */
	sec = period/1000000;
	ns = (period - (sec * 1000000)) * 1000;
	itval.it_interval.tv_sec = sec;
	itval.it_interval.tv_nsec = ns;
	itval.it_value.tv_sec = sec;
	itval.it_value.tv_nsec = ns;
	ret = timerfd_settime (fd, 0, &itval, NULL);
	return ret;
}
示例#2
0
文件: 4.c 项目: guoqunabc/C-Cplusplus
int main(int argc, const char *argv[])
{
    //创建fd

    int timerfd = timerfd_create(CLOCK_REALTIME, 0);
    if(timerfd == -1)
        ERR_EXIT("timerfd_create");

    //设置时间
    struct itimerspec tv;
    memset(&tv, 0, sizeof tv);
    tv.it_value.tv_sec = 3;
    tv.it_interval.tv_sec = 1;
    if(timerfd_settime(timerfd, 0, &tv, NULL) == -1)
        ERR_EXIT("timerfd_settime");

    //判断fd可读
    uint64_t val;
    int ret;
    while(1)
    {
        ret = read(timerfd, &val, sizeof val);
        if(ret == -1 && errno == EINTR)
            continue;
        printf("ret = %d\n", ret);
        foo();
    }

    close(timerfd);

    return 0;
}
示例#3
0
static int async_usleep(struct sip_msg* msg, async_ctx *ctx, int *useconds)
{
	struct itimerspec its;
	int fd;

	LM_DBG("sleep %d useconds\n", *(unsigned int *)useconds);

	/* create the timer fd */
	if ( (fd=timerfd_create( CLOCK_REALTIME, 0))<0 ) {
		LM_ERR("failed to create new timer FD (%d) <%s>\n",
			errno, strerror(errno));
		return -1;
	}

	/* set the time */
	its.it_value.tv_sec = (*(unsigned int *)useconds / 1000000);
	its.it_value.tv_nsec = (*(unsigned int *)useconds % 1000000) * 1000;
	its.it_interval.tv_sec = 0;
	its.it_interval.tv_nsec = 0;
	if (timerfd_settime( fd, 0, &its, NULL)<0) {
		LM_ERR("failed to set timer FD (%d) <%s>\n",
			errno, strerror(errno));
		return -1;
	}

	/* start the async wait */
	ctx->resume_param = (void*)(unsigned long)
		(((unsigned long)-1) & (get_uticks()+*(unsigned int *)useconds));
	ctx->resume_f = resume_async_sleep;
	async_status = fd;

	return 1;
}
示例#4
0
int create_timer(int interval_value , int epoll_fd)
{
    int timer_fd = timerfd_create(CLOCK_REALTIME , 0);
    if(timer_fd < 0) 
    {
        LOG_ERROR("In create_timer : timerfd_create error : %s" , STR_ERROR);
        return -1;
    }
    struct timespec interval;
    interval.tv_sec = interval_value;
    interval.tv_nsec = 0;

    struct itimerspec spec;
    spec.it_value = interval;
    spec.it_interval = interval;

    if(timerfd_settime(timer_fd , TFD_TIMER_ABSTIME , &spec , NULL) < 0)
    {
        LOG_ERROR("In create_timer : timerfd_settime error : %s" , STR_ERROR);
        close(timer_fd);
        return -1;
    }

    if(add_to_epoll(epoll_fd , timer_fd , EPOLLIN , NULL) < 0)
    {
        LOG_ERROR("In create_timer : all timer %d to epoll error !" , timer_fd);
        close(timer_fd);
        return -1;
    }

    return timer_fd;
}
示例#5
0
// Enable The Timer And Set Interval, Timer Will First Run After It's Specified Interval
int Timer::SetInterval(int sec, int msec) {
	struct itimerspec new_time; 		
	struct timespec current;

	clock_gettime(CLOCK_REALTIME, &current);	

	new_time.it_value.tv_sec = current.tv_sec + sec;
	new_time.it_value.tv_nsec = current.tv_nsec + 1000000 * msec;

	// If Nanosecond Field Exceeds 1000000000, Second Field Increase
	if (new_time.it_value.tv_nsec >= 1000000000) {
		new_time.it_value.tv_sec++;
		new_time.it_value.tv_nsec -= 1000000000;
	}

	if (once_run_) {
		new_time.it_interval.tv_sec = 0;
		new_time.it_interval.tv_nsec = 0;

	} else {
		new_time.it_interval.tv_sec = sec;
		new_time.it_interval.tv_nsec = 1000000 * msec;
	}

	interval_ = new_time.it_interval;

	return timerfd_settime(timerfd_, TFD_TIMER_ABSTIME, &new_time, NULL);
}
示例#6
0
 int CTimerHandler::open(uint32_t start_second, int interval_second)
 {
     this->m_start_second = start_second;
     this->m_interval_second = interval_second;
     
     if((m_sock_handle = timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
     {
         LOG_ERROR("Timer fd create failed. %d, %s", errno, strerror(errno));
         return -1;
     }
     
     struct itimerspec new_value;
     // struct itimerspec old_value;
     // bzero(&new_value, sizeof(new_value));
     // bzero(&old_value,sizeof(old_value));
     
     struct timespec start, interval;
     start.tv_sec = start_second;
     start.tv_nsec = 0;
     
     interval.tv_sec = interval_second;
     interval.tv_nsec = 0;
     
     new_value.it_value = start;
     new_value.it_interval = interval;
     
     if( timerfd_settime(m_sock_handle, 0, &new_value, NULL) < 0) // 0 relative time
     {
         LOG_ERROR("Settime error, %d, %s\n", errno, strerror(errno));
         return -1;
     }
     
     return CEventHandler::open();
 }
示例#7
0
文件: timerfd.c 项目: CarterTsai/rr
int main(void) {
  int fd = timerfd_create(CLOCK_MONOTONIC, 0);
  struct itimerspec spec, old;
  uint64_t num_expirations;

  atomic_printf("created timerfd %d\n", fd);
  test_assert(fd >= 0);

  memset(&spec, 0, sizeof(spec));
  spec.it_value.tv_nsec = 100000000;
  atomic_printf("setting timer to expire in {sec:%ld,nsec:%ld}\n",
                spec.it_value.tv_sec, spec.it_value.tv_nsec);
  timerfd_settime(fd, 0, &spec, &old);

  atomic_printf("  (old expiration was {sec:%ld,nsec:%ld})\n",
                old.it_value.tv_sec, old.it_value.tv_nsec);
  test_assert(0 == old.it_value.tv_sec && 0 == old.it_value.tv_nsec);

  atomic_puts("sleeping 50ms ...");
  usleep(50000);

  timerfd_gettime(fd, &old);
  atomic_printf("  expiration now in {sec:%ld,nsec:%ld})\n",
                old.it_value.tv_sec, old.it_value.tv_nsec);
  test_assert(0 == old.it_value.tv_sec && old.it_value.tv_nsec <= 50000000);

  atomic_puts("waiting for timer to expire ...");
  read(fd, &num_expirations, sizeof(num_expirations));

  atomic_printf("  timer expired %" PRIu64 " times\n", num_expirations);
  test_assert(1 == num_expirations);

  atomic_puts("EXIT-SUCCESS");
  return 0;
}
示例#8
0
int			create_timer(t_cc* data, t_evqueue *evq, int p_id)
{
  struct itimerspec	new_value;
  t_evq			*tdata;
  t_evq			d = {-1, NULL, p_id, data->fd, NULL};
  int			timer_fd;

  tdata = malloc(sizeof(t_evq));
  check_mem(tdata);
  new_value.it_value.tv_sec = 0;
  new_value.it_value.tv_nsec = 0;
  new_value.it_interval.tv_sec = 0;
  new_value.it_interval.tv_nsec = 0;
  timer_fd = timerfd_create(CLOCK_MONOTONIC, O_NONBLOCK);
  *evq = create_evqueue(timer_fd);
  d.eventq = *evq;
  memcpy(tdata, &d , sizeof(t_evq));
  timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &new_value, NULL);
  tdata->timer_fd = timer_fd;
  tdata->read_func = execute_event;
  data->e->event.events = EPOLLIN | EPOLLRDHUP;
  data->e->event.data.ptr = tdata;
  epoll_ctl(data->e->efd, EPOLL_CTL_ADD, timer_fd, &data->e->event);
  return (timer_fd);
 error:
  return (-1);
}
static dbus_bool_t addTimeout(DBusTimeout *timeout, void *data)
{
   (void)data;
   dbus_bool_t ret = FALSE;

   if(ARRAY_SIZE(gPollInfo.fds) > (unsigned int)(gPollInfo.nfds))
   {
      const int interval = dbus_timeout_get_interval(timeout);
      if ((0<interval)&&(TRUE==dbus_timeout_get_enabled(timeout)))
      {
         const int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
         if (-1!=tfd)
         {
            const struct itimerspec its = { .it_value= {interval/1000, interval%1000} };
            if (-1!=timerfd_settime(tfd, 0, &its, NULL))
            {
               tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
               pEntry->objtype = OT_TIMEOUT;
               pEntry->timeout = timeout;
               gPollInfo.fds[gPollInfo.nfds].fd = tfd;
               gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
               ++gPollInfo.nfds;
               ret = TRUE;
            }
            else
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout - _settime() failed"), DLT_STRING(strerror(errno)) );
            }
         }
         else
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout - _create() failed"), DLT_STRING(strerror(errno)) );
         }
      }
   }
示例#10
0
static int timerfd_timer_set_rate(int handle, unsigned int rate)
{
	struct timerfd_timer *our_timer, find_helper = {
		.handle = handle,
	};
	int res = 0;

	if (handle == -1) {
		ast_log(LOG_ERROR, "Attempting to set rate on timerfd handle -1");
		return -1;
	}

	if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
		ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
		return -1;
	}
	ao2_lock(our_timer);

	our_timer->saved_timer.it_value.tv_sec = 0;
	our_timer->saved_timer.it_value.tv_nsec = rate ? (long) (1000000000 / rate) : 0L;
	our_timer->saved_timer.it_interval.tv_sec = our_timer->saved_timer.it_value.tv_sec;
	our_timer->saved_timer.it_interval.tv_nsec = our_timer->saved_timer.it_value.tv_nsec;

	if (!our_timer->is_continuous) {
		res = timerfd_settime(handle, 0, &our_timer->saved_timer, NULL);
	}

	ao2_unlock(our_timer);
	ao2_ref(our_timer, -1);

	return res;
}
示例#11
0
timerfd*
timerfd_new(uint32_t timeout,void *ud)
{
	timerfd *t = calloc(1,sizeof(*t));

	((handle*)t)->fd = timerfd_create(CLOCK_MONOTONIC,TFD_CLOEXEC|TFD_NONBLOCK);
	if(((handle*)t)->fd < 0){
		free(t);
		return NULL;
	}
	struct itimerspec spec;
    struct timespec now;
	clock_gettime(CLOCK_MONOTONIC, &now);
	int32_t sec = timeout/1000;
	int32_t ms = timeout%1000;    
	int64_t nosec = (now.tv_sec + sec)*1000*1000*1000 + now.tv_nsec + ms*1000*1000;
	spec.it_value.tv_sec = nosec/(1000*1000*1000);
    	spec.it_value.tv_nsec = nosec%(1000*1000*1000);
    	spec.it_interval.tv_sec = sec;
    	spec.it_interval.tv_nsec = ms*1000*1000;	
	
	if(0 != timerfd_settime(((handle*)t)->fd,TFD_TIMER_ABSTIME,&spec,0))
	{
		close(((handle*)t)->fd);
		free(t);
		return NULL;
	}
	((handle*)t)->on_events = on_timeout;
	((handle*)t)->imp_engine_add = imp_engine_add;
	t->ud = ud;
	return t;
}
示例#12
0
文件: event.c 项目: sashka/uwsgi
int event_queue_add_timer(int eq, int *id, int sec) {

	struct itimerspec it;
	int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);

	if (tfd < 0) {
		uwsgi_error("timerfd_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 (timerfd_settime(tfd, 0, &it, NULL)) {
		uwsgi_error("timerfd_settime()");
		close(tfd);
		return -1;
	}
	
	*id = tfd;
	return event_queue_add_fd_read(eq, tfd);
}
示例#13
0
void Timer::runTimer()
{
    struct pollfd event[1];
    event[0].fd= timerfd_;
    event[0].events = POLLIN;
    char buf[1024];
    int nready;

    if(timerfd_settime(timerfd_, 0, &howlong_, NULL) == -1)
        ERR_EXIT("timerfd_settime");
    
    is_Started_ = true;
    while(is_Started_)
    {
    
        nready = poll(event, 1, 10000);
        if(nready == -1)
        {
            ERR_EXIT("poll");
            
        }
        else if(nready == 0)
            printf("timeout\n");
        else
        {
            if(read(timerfd_, buf, sizeof buf) == -1)
                ERR_EXIT("read");
            timeCallback_();
        }
    }

}
示例#14
0
void Timer::cancelTimer()
{
    memset(&howlong_, 0, sizeof howlong_);
    if(timerfd_settime(timerfd_, 0, &howlong_, NULL) == -1)
        ERR_EXIT("timerfd_settime");
    is_Started_ = false;
}
示例#15
0
int
evfilt_timer_knote_create(struct filter *filt, struct knote *kn)
{
    struct epoll_event ev;
    struct itimerspec ts;
    int tfd;

    kn->kev.flags |= EV_CLEAR;

    tfd = timerfd_create(CLOCK_MONOTONIC, 0);
    if (tfd < 0) {
        dbg_printf("timerfd_create(2): %s", strerror(errno));
        return (-1);
    }
    dbg_printf("created timerfd %d", tfd);

    convert_msec_to_itimerspec(&ts, kn->kev.data, kn->kev.flags & EV_ONESHOT);
    if (timerfd_settime(tfd, 0, &ts, NULL) < 0) {
        dbg_printf("timerfd_settime(2): %s", strerror(errno));
        close(tfd);
        return (-1);
    }

    memset(&ev, 0, sizeof(ev));
    ev.events = EPOLLIN;
    ev.data.ptr = kn;
    if (epoll_ctl(filter_epfd(filt), EPOLL_CTL_ADD, tfd, &ev) < 0) {
        dbg_printf("epoll_ctl(2): %d", errno);
        close(tfd);
        return (-1);
    }

    kn->data.pfd = tfd;
    return (0);
}
示例#16
0
static int
rb_epoll_sched_event_timerfd(struct ev_entry *event, int when)
{
	struct itimerspec ts;
	static char buf[FD_DESC_SZ + 8];
	int fd;
	rb_fde_t *F;

	if((fd = timerfd_create(CLOCK_REALTIME, 0)) < 0)
	{
		rb_lib_log("timerfd_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(timerfd_settime(fd, 0, &ts, NULL) < 0)
	{
		rb_lib_log("timerfd_settime: %s\n", strerror(errno));
		close(fd);
		return 0;
	}
	rb_snprintf(buf, sizeof(buf), "timerfd: %s", event->name);
	F = rb_open(fd, RB_FD_UNKNOWN, buf);
	rb_set_nb(F);
	event->comm_ptr = F;
	rb_setselect(F, RB_SELECT_READ, rb_read_timerfd, event);
	return 1;
}
static int timerfd_init(const struct itimerspec *ti)
{
	int fd = -1;
	int ret = -1;
	if (ti == NULL)
		return -1;
	/* create new timer */
	fd = timerfd_create(CLOCK_MONOTONIC, 0);
	if (fd < 0) {
		printf("Failed to create timer\n");
		return -1;
	}

	/* set to non-blocking */
	ret = fcntl(fd, F_SETFL, O_NONBLOCK);
	if (ret) {
		printf("Failed to set to non blocking mode\n");
		close(fd);
		return -1;
	}

	/* set timeout */
	ret = timerfd_settime(fd, 0, ti, NULL);
	if (ret) {
		printf("Failed to set timer duration\n");
		close(fd);
		return -1;
	}
	printf("%s fd %d\n", __func__, fd);
	return fd;
}
示例#18
0
/* Register a timeout file descriptor */
static inline int _mk_event_timeout_create(mk_event_ctx_t *ctx, int expire)
{
    int ret;
    int timer_fd;
    struct itimerspec its;

    /* expiration interval */
    its.it_interval.tv_sec  = expire;
    its.it_interval.tv_nsec = 0;

    /* initial expiration */
    its.it_value.tv_sec  = time(NULL) + expire;
    its.it_value.tv_nsec = 0;

    timer_fd = timerfd_create(CLOCK_REALTIME, 0);
    if (timer_fd == -1) {
        mk_libc_error("timerfd");
        return -1;
    }

    ret = timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL);
    if (ret < 0) {
        mk_libc_error("timerfd_settime");
        return -1;
    }

    /* register the timer into the epoll queue */
    ret = _mk_event_add(ctx, timer_fd, MK_EVENT_READ);
    if (ret != 0) {
        close(timer_fd);
        return ret;
    }

    return timer_fd;
}
示例#19
0
int vc_stop_timer (vc_data *data) {

	if(data->fds[FD_INDEX_TIMER].fd < 0){
		debug_printf("%s: attempted to operate on an invalid timerfd\n",
				__func__);
		return -1;
	}

	struct itimerspec its;
	its.it_interval.tv_sec = 0;
	its.it_interval.tv_nsec = 0;

	its.it_value.tv_sec = 0;
	its.it_value.tv_nsec = 0;

	int ret = 0;
	if((ret = timerfd_settime(data->fds[FD_INDEX_TIMER].fd, 0,
			(const struct itimerspec *) &its, NULL)) < 0)
		return -1;

	data->fds[FD_INDEX_TIMER].events = 0;

	return 0;

}
示例#20
0
ANW_RSP_STATUS AnwTimer::arm_timer()
{
  struct itimerspec curr_value;
  ANW_RSP_STATUS anw_status = ANW_STATUS_SUCCESS;
  AnwApp *anw_app = NULL;
  epoll_event ev;
  curr_value.it_value.tv_sec = _it_value / 1000;
  curr_value.it_value.tv_nsec = (_it_value % 1000) * 1000 * 1000;
  curr_value.it_interval.tv_sec = _interval / 1000;
  curr_value.it_interval.tv_nsec = (_interval % 1000 ) * 1000 * 1000;
  if( -1 == timerfd_settime(_timerid, 0, &curr_value, 0))
  {
    _valid = false;
    return ANW_STATUS_FAILURE;
  }
  _valid = true;
  ev.events = EPOLLIN | EPOLLPRI ;
  //ev.events = EPOLLIN | EPOLLET ;
  //_epoll_event_impl = new epoll_event_impl(ANW_EPOLL_IMPL_TYPE_TIMER, _timer_impl);
  //ev.data.ptr = _epoll_event_impl;  //pass the pointer to AnwTimerImpl
  ev.data.ptr = this; 
  anw_app = AnwApp::CreateInstance();
  if (unlikely(NULL == anw_app))
  {
    ANW_DEBUG_ASSERT(0);
    return ANW_STATUS_FAILURE;
  }

  anw_status = anw_app->add_timer(_timerid, ev);
  return anw_status;
}
示例#21
0
文件: timerfd1.c 项目: tsuyopon/cpp
int main(int argc, char ** argv ) {

    struct timespec cur;
    struct timeval  tv1,tv2;

    if( argc < 4 ) {
	fprintf( stderr, "Usage: %s <num> <num> <num>...\n", argv[0] );
	exit( EXIT_FAILURE );
    }

    int fd = timerfd_create( CLOCK_MONOTONIC, 0 );
    clock_gettime( CLOCK_MONOTONIC, &cur );

    struct itimerspec val;
    val.it_value.tv_sec     = cur.tv_sec + atoi( argv[1] );
    val.it_value.tv_nsec    = 0;
    val.it_interval.tv_sec  = atoi( argv[2] );
    val.it_interval.tv_nsec = 0;
    timerfd_settime( fd, TFD_TIMER_ABSTIME, &val, 0 );
    gettimeofday( &tv1, 0 );

    uint64_t read_cnt;
    int      cnt;
    for( cnt = 0; cnt < atoi( argv[3] ); cnt ++ ) {
	read( fd, &read_cnt, sizeof( uint64_t ));
	gettimeofday( &tv2, 0 );
	double rtn = TV2SEC( tv2 ) - TV2SEC( tv1 );
	printf( "timerfd %d Path... [%f]\n", cnt + 1, rtn );
	tv1 = tv2;
    }
    close( fd );
    exit( EXIT_SUCCESS );
}
示例#22
0
文件: event.c 项目: pavlix/netresolve
netresolve_timeout_t
netresolve_timeout_add(netresolve_query_t query, time_t sec, long nsec,
		netresolve_timeout_callback_t callback, void *data)
{
	netresolve_watch_t watch;
	int fd;
	struct itimerspec timerspec = {{0, 0}, {sec, (sec != 0 || nsec != 0) ? nsec : 1}};

	if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) == -1)
		return NULL;

	if (timerfd_settime(fd, 0, &timerspec, NULL) == -1) {
		close(fd);
		return NULL;
	}

	debug_query(query, "adding timeout: fd=%d sec=%d nsec=%ld", fd, (int) sec, nsec);

	watch = netresolve_watch_add(query, fd, POLLIN, NULL, data);

	if (watch && callback) {
		watch->callback = timeout_watch_callback;
		watch->timeout_callback = callback;
	}

	assert(watch);

	return watch;
}
void suspend(void)
{
	int power_state_fd;
	struct sigevent event = {};
	int timerfd;
	int err;
	struct itimerspec spec = {};

	if (getuid() != 0)
		ksft_exit_skip("Please run the test as root - Exiting.\n");

	power_state_fd = open("/sys/power/state", O_RDWR);
	if (power_state_fd < 0)
		ksft_exit_fail_msg(
			"open(\"/sys/power/state\") failed %s)\n",
			strerror(errno));

	timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
	if (timerfd < 0)
		ksft_exit_fail_msg("timerfd_create() failed\n");

	spec.it_value.tv_sec = 5;
	err = timerfd_settime(timerfd, 0, &spec, NULL);
	if (err < 0)
		ksft_exit_fail_msg("timerfd_settime() failed\n");

	if (write(power_state_fd, "mem", strlen("mem")) != strlen("mem"))
		ksft_exit_fail_msg("Failed to enter Suspend state\n");

	close(timerfd);
	close(power_state_fd);
}
bool PeriodicThreadImplTimerfd::setPeriod(const icl_core::TimeSpan& period)
{
  m_period = period;

  int ret = -1;
  if (timer_created)
  {
    /* Make the timer periodic */
    unsigned int ns;
    unsigned int sec;
    struct itimerspec itval;

    sec = period.tsSec();
    ns = period.tsNSec();
    itval.it_interval.tv_sec = sec;
    itval.it_interval.tv_nsec = ns;
    itval.it_value.tv_sec = sec;
    itval.it_value.tv_nsec = ns;
    ret = timerfd_settime(m_info->timer_fd, 0, &itval, NULL);
  }
  if (ret == -1)
  {
    return false;
  }
  else
  {
    return true;
  }
}
示例#25
0
int main()
{
	struct itimerspec time;
	struct timeval now;
	
	gettimeofday(&now, NULL);

	time.it_interval.tv_sec = 1;
	time.it_interval.tv_nsec = 0;
	time.it_value.tv_sec = now.tv_sec + 1; 
	time.it_value.tv_nsec = now.tv_usec * 1000;
	
	int fd = timerfd_create(CLOCK_REALTIME, 0);
	assert(fd > 0);
	
	timerfd_settime(fd, TFD_TIMER_ABSTIME, &time, NULL);
	
	int count = 0;

	while(1)
	{
		uint64_t exp;
		int n = read(fd, &exp, sizeof(exp));
		assert(n == sizeof(exp));
		printf("time out %d.\n", ++count);
	}

	return 0;
}
示例#26
0
// Send Modbus packet
int send_modbus_packet(uint8_t device_address, uint8_t sequence_number, Packet* packet_to_send) {
    int bytes_written = 0;
    uint16_t packet_crc16 = 0;
    uint8_t crc16_first_byte_index = 0;
    int packet_size = packet_to_send->header.length + MODBUS_PACKET_OVERHEAD;
    uint8_t modbus_packet[packet_size];
    
    //printf(">>>>>>>>>>>>>>>>>>>>> SEN %llu\n", microseconds());
    
    // Assemble Modbus packet header    
    modbus_packet[0] = device_address;
    modbus_packet[1] = RS485_EXTENSION_MODBUS_FUNCTION_CODE;
    modbus_packet[2] = sequence_number;
    
    // Assemble Tinkerforge packet header
    memcpy(&modbus_packet[3], &packet_to_send->header, sizeof(PacketHeader));
    
    // Assemble Tinkerforge packet payload (if any)
    memcpy(&modbus_packet[3+sizeof(PacketHeader)], &packet_to_send->payload,
           packet_to_send->header.length - TINKERFORGE_HEADER_LENGTH);

    // Calculating CRC16
    packet_crc16 = crc16(modbus_packet, packet_to_send->header.length + MODBUS_PACKET_HEADER_LENGTH);
    
    // Assemble the calculated CRC16 to the Modbus packet
    crc16_first_byte_index = packet_to_send->header.length +
                             MODBUS_PACKET_HEADER_LENGTH;
    modbus_packet[crc16_first_byte_index] = packet_crc16 >> 8;
    modbus_packet[++crc16_first_byte_index] = packet_crc16 & 0x00FF;

    // Enabling TX
    start = microseconds();
    gpio_output_set(_tx_pin);
    
    // Sending packet
    bytes_written = write(_rs485_serial_fd, modbus_packet, sizeof(modbus_packet));
    
    if (bytes_written <= 0) {
        // Disabling TX
        gpio_output_clear(_tx_pin);
        end = microseconds();
        send_verify_flag = 0;
        log_error("RS485: Error sending packet through serial interface");
        return -1;
    }

    // Save the packet as byte array
    memcpy(current_request_as_byte_array, modbus_packet, packet_size);

    // Start the send verify timer
    setup_timer(&send_verify_timer, TIME_UNIT_NSEC, SEND_VERIFY_TIMEOUT);
    timerfd_settime(_send_verify_event, 0, &send_verify_timer, NULL);
    
    // Set send verify flag
    send_verify_flag = 1;
    
    log_debug("RS485: Packet sent through serial interface");
    
    return bytes_written;
}
示例#27
0
static int TimerStart(unsigned int period, struct TimerInfo *info)
{
    int rc;
    unsigned int ns;
    unsigned int sec;
    int fd;
    struct itimerspec itval;

    // Create the timer
    fd = timerfd_create (CLOCK_MONOTONIC, 0);
    info->wakeups_missed = 0;
    info->timer_fd = fd;
    if (fd == -1)
    {
        return fd;
    }

    // Make the timer periodic
    sec = period/1000000;
    ns = (period - (sec * 1000000)) * 1000;
    itval.it_interval.tv_sec = sec;
    itval.it_interval.tv_nsec = ns;
    itval.it_value.tv_sec = sec;
    itval.it_value.tv_nsec = ns;

    rc = timerfd_settime (fd, 0, &itval, NULL);

    return rc;
}
static jlong init_timerfd()
{
    int epollfd;
    int fds[N_ANDROID_TIMERFDS];

    epollfd = epoll_create(N_ANDROID_TIMERFDS);
    if (epollfd < 0) {
        ALOGV("epoll_create(%zu) failed: %s", N_ANDROID_TIMERFDS,
                strerror(errno));
        return 0;
    }

    for (size_t i = 0; i < N_ANDROID_TIMERFDS; i++) {
        fds[i] = timerfd_create(android_alarm_to_clockid[i], 0);
        if ((fds[i] < 0) && (android_alarm_to_clockid[i] == CLOCK_POWEROFF_ALARM)) {
            ALOGV("timerfd does not support CLOCK_POWEROFF_ALARM, using CLOCK_REALTIME_ALARM instead");
            fds[i] = timerfd_create(CLOCK_REALTIME_ALARM, 0);
        }
        if (fds[i] < 0) {
            ALOGV("timerfd_create(%u) failed: %s",  android_alarm_to_clockid[i],
                    strerror(errno));
            close(epollfd);
            for (size_t j = 0; j < i; j++) {
                close(fds[j]);
            }
            return 0;
        }
    }

    AlarmImpl *ret = new AlarmImplTimerFd(fds, epollfd, wall_clock_rtc());

    for (size_t i = 0; i < N_ANDROID_TIMERFDS; i++) {
        epoll_event event;
        event.events = EPOLLIN | EPOLLWAKEUP;
        event.data.u32 = i;

        int err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], &event);
        if (err < 0) {
            ALOGV("epoll_ctl(EPOLL_CTL_ADD) failed: %s", strerror(errno));
            delete ret;
            return 0;
        }
    }

    struct itimerspec spec;
    memset(&spec, 0, sizeof(spec));
    /* 0 = disarmed; the timerfd doesn't need to be armed to get
       RTC change notifications, just set up as cancelable */

    int err = timerfd_settime(fds[ANDROID_ALARM_TYPE_COUNT],
            TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, NULL);
    if (err < 0) {
        ALOGV("timerfd_settime() failed: %s", strerror(errno));
        delete ret;
        return 0;
    }

    return reinterpret_cast<jlong>(ret);
}
void CStdMultimediaTimerProc::SetDelay(uint32_t inDelay)
{
	struct itimerspec nv, ov;
	nv.it_interval.tv_sec = inDelay / 1000;
	nv.it_interval.tv_nsec = (inDelay % 1000) * 1000000;
	nv.it_value = nv.it_interval;
	timerfd_settime(fd, 0, &nv, &ov);
}
void CStdMultimediaTimerProc::Set()
{
	struct itimerspec nv, ov;
	timerfd_gettime(fd, &nv);
	nv.it_value.tv_sec = 0;
	nv.it_value.tv_nsec = 1;
	timerfd_settime(fd, 0, &nv, &ov);
}