poll机制实现timer定时器计时功能

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <sys/poll.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>


/**
 * @brief timer handler
 *
 * @param[in] fd timer file descriptor
 */
static int timer_process_fd(int fd)
{
	uint64_t nb_expired = 0;
	int status = -1;
	ssize_t count;

	do
	{
		count = read(fd, &nb_expired, sizeof(nb_expired));
	}
	while (count < 0 && errno == EINTR);

	if (count < 0)
	{
		ULOGE("timer read (fd=%d): err=%d(%s)", fd, errno, strerror(errno));
	}
	else if (sizeof(nb_expired) != count)
	{
		ULOGE("timer unexpected read return: %zd != %zd", count, sizeof(nb_expired));
	}
	else
	{
//		status = gmsl2_check_ready();
	        status = touch_input_check_ready();
	}

	return status;
}

int main(int argv,char *argc[])
{
	struct pollfd poll_fd;
	struct itimerspec nval;
	int fd_timer = -1;
	int ret;

	fd_timer = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
	nval.it_value.tv_sec  = 0;
	nval.it_value.tv_nsec = 100000000; //100ms
	nval.it_interval.tv_sec  = nval.it_value.tv_sec;
	nval.it_interval.tv_nsec = nval.it_value.tv_nsec;
	ret = timerfd_settime(fd_timer, 0, &nval, NULL);
	poll_fd.fd = fd_timer;
	poll_fd.events = POLLIN;
	printf("timer fd: %d", poll_fd.fd);


	for (;;)
	{
		do
		{
			ret = poll(&poll_fd, 1, -1);
		}
		while (ret < 0 && errno == EINTR);
		printf("timer enter...");
		if (ret < 0)
		{
			printf("poll error=%d(%s)", errno, strerror(errno));
			printf("poll fd, revents %i", poll_fd.revents);
		}

		// timer fd
		if (poll_fd.revents != 0)
		{
			printf("timer enter, poll_fd.revents");
			ret =1;
//			ret = timer_process_fd(poll_fd.fd);
			if (ret == 0)
			{
				printf("timer all ready, stop timer");
				close(fd_timer);
				poll_fd.fd = -1;
				fd_timer = -1;
				break;
			}
		}
	}

	if (fd_timer != -1) close(fd_timer);
	printf("screenInfoMgr exiting");

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值