rk3288 poll源码学习

系统调用在内核中都是sys_xxx命名的,如open、read、write在内核中分别对应sys_open、sys_read、sys_write。所以对应poll的内核函数,分析sys_poll就可以理解poll机制。

sys_poll函数

在文件fs/select.c中包含了sys_poll函数。SYSCALL_DEFINE3的宏定义于include/linux/syscalls.h,展开后就有sys_poll函数。
sys_poll中做了简单的处理后,就调用do_sys_poll。

SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
		int, timeout_msecs)
{
	struct timespec end_time, *to = NULL;
	int ret;

	if (timeout_msecs >= 0) {
		to = &end_time;
		poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
			NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
	}

	ret = do_sys_poll(ufds, nfds, to);						//这里调用do_sys_poll,传入基本的参数

	if (ret == -EINTR) {
		//错误处理
	}
	return ret;
}

do_sys_poll函数位于fs/select.c中,忽略其他代码,只看关键的

int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
		struct timespec *end_time)
{
	//...

	poll_initwait(&table);										//初始化了一个poll_wqueues变量table
	fdcount = do_poll(nfds, head, &table, end_time);			//这里调用do_poll,核心代码
	poll_freewait(&table);

	//...
}

void poll_initwait(struct poll_wqueues *pwq)
{
	init_poll_funcptr(&pwq->pt, __pollwait);
	pwq->polling_task = current;
	pwq->triggered = 0;
	pwq->error = 0;
	pwq->table = NULL;
	pwq->inline_index = 0;
}

static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
{
	pt->_qproc = qproc;
	pt->_key   = ~0UL; /* all events enabled */
}

/* poll_initwait->init_poll_funcptr:
 * table->pt->_qproc = __pollwait,这个__pollwait将在驱动poll函数里用到
 */

do_poll函数位于fs/select.c中,这是poll的核心代码

static int do_poll(unsigned int nfds,  struct poll_list *list,
		   struct poll_wqueues *wait, struct timespec *end_time)
{
	//.....

	for (;;) {
		struct poll_list *walk;
		bool can_busy_loop = false;

		for (walk = list; walk != NULL; walk = walk->next) {
			struct pollfd * pfd, * pfd_end;

			pfd = walk->entries;
			pfd_end = pfd + walk->len;
			for (; pfd != pfd_end; pfd++) {
				/*
				 * Fish for events. If we found one, record it
				 * and kill poll_table->_qproc, so we don't
				 * needlessly register any other waiters after
				 * this. They'll get immediately deregistered
				 * when we break out and return.
				 */
				if (do_pollfd(pfd, pt, &can_busy_loop,		//1.调用dol_pollfd驱动程序poll第一次被调用
					      busy_flag)) {
					count++;
					pt->_qproc = NULL;
					/* found something, stop busy polling */
					busy_flag = 0;
					can_busy_loop = false;
				}
			}
		}
		/*
		 * All waiters have already been registered, so don't provide
		 * a poll_table->_qproc to them on the next loop iteration.
		 */
		pt->_qproc = NULL;			//6.把pt->_qproc设置为NULL,第二次调用驱动程序的poll时,不会把线程方式某个队列
		if (!count) {
			count = wait->error;
			if (signal_pending(current))
				count = -EINTR;
		}
		if (count || timed_out)		//如果有返回值,count为非0,跳出循环
			break;

		//...

		if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))		//否则休眠一段时间,休眠事件到或者被中断唤醒,会再次循环再次调用驱动程序的poll
			timed_out = 1;
	}
	return count;
}

//fs/select.c
static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait,
				     bool *can_busy_poll,
				     unsigned int busy_flag)
{
	unsigned int mask;
	int fd;

	mask = 0;
	fd = pollfd->fd;
	if (fd >= 0) {
		struct fd f = fdget(fd);
		mask = POLLNVAL;
		if (f.file) {
			mask = DEFAULT_POLLMASK;
			if (f.file->f_op->poll) {
				pwait->_key = pollfd->events|POLLERR|POLLHUP;
				pwait->_key |= busy_flag;
				mask = f.file->f_op->poll(f.file, pwait);			//2.这里调用我们写的poll函数
				if (mask & busy_flag)								//这里的mask就是驱动程序返回的状态
					*can_busy_poll = true;
			}
			/* Mask out unneeded events. */
			mask &= pollfd->events | POLLERR | POLLHUP; 		//是否时app里定义的events需要的
			fdput(f);
		}
	}
	pollfd->revents = mask;				//把mask写入revents

	return mask;
}

//button_poll.c
static unsigned int button_poll(struct file *fp, poll_table * wait)
{
	printk("%s,button poll\n", __FUNCTION__);
	poll_wait(fp, &gpio_key_wait, wait);					//3.调用poll_wait
	return IsEmpty(irqBuff) ? 0 : POLLIN | POLLRDNORM;
}

//include/linux/poll.h
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
	if (p && p->_qproc && wait_address)
		p->_qproc(filp, wait_address, p);					//4.这里调用之前注册的__pollwait
}

static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
				poll_table *p)
{
	struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);
	struct poll_table_entry *entry = poll_get_entry(pwq);
	if (!entry)
		return;
	entry->filp = get_file(filp);
	entry->wait_address = wait_address;
	entry->key = p->_key;
	init_waitqueue_func_entry(&entry->wait, pollwake);
	entry->wait.private = pwq;
	add_wait_queue(wait_address, &entry->wait);			//5.把线程放入队列
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

习惯就好zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值