Poll with sleep

Source: Internet
Author: User
Tags linux sleep sleep function

There are many callback functions in file_operations. These callback functions implement VFS and VFS provides a mechanism. These callback functions provide different policies, which means VFS is implemented, it should be said that these functions should not have any restrictions, but only one poll callback function is different. It cannot sleep. Why?

All callback functions except poll have direct semantics. For example, read is read and write is write. Therefore, the system call layer can directly submit the execution path to VFS. For example, in the sys_read function, after almost a simple judgment, I immediately called the READ function of file_operations in the real file system, but the poll function is special and has no simple semantics. In fact, it is round robin, however, it is not as uniform as the system call layer and VFS layer like read and write. poll in the VFS layer means "check whether this file has any action ", however, in the System Call layer, the meaning is to "see which action exists in these files". This is different. In order to smooth the semantics of the System Call layer to the VFS layer, only the insertion mechanism must be seen in system calls and VFS. This mechanism implements poll and also includes select. In the implementation of poll, the state of the process is used to synchronize sleep/Wakeup actions. It is not to sleep immediately after the process is added to the sleep queue, instead of sleep, wait until all the file descriptors of poll are added to the queue and then sleep. In fact, it is only the left scheduling. The overall framework is as follows:

For (;;)

Set_current_state (task_interruptible)


For each FD to poll

Ask driver if I/O can happen

Add current process to driver wait queue

If one or more FDS are ready

Break

Schedule_timeout_range (...)

Note: At the beginning, the Process status is set to task_interruptible but not sleep. In The Middle Of The for loop, the process is added to the sleep queue one after another, and the process is switched at the end, it's just sleep. Let's look at this poor implementation. After the task_interruptible state is set, the process will be switched in the freshman year. This is ugly, the ugly nature is that the poll implemented by VFS is a file descriptor of poll, but the semantics of system calls is a lot of file descriptors of poll. without the need to add an adaptation mechanism, we had to use the process status to achieve this. The 2.6.29 kernel really cannot see this situation, so we proposed that poll can also sleep like other file_operations callback functions, in addition, the traditional sleep wake-up function can be used to wake up the process:

+ Static int pollwake (wait_queue_t
* Wait, unsigned mode, int sync, void * key)

+ {

+ Struct
Poll_wqueues * pwq = wait-> private;

+ Declare_waitqueue (dummy_wait,
Pwq-> polling_task );

+

+ Set_mb (pwq-> triggered,
1 );

+

+ /*
Perform the default wake up operation */

+ Return
Default_wake_function (& dummy_wait, mode, sync, key );

+}

Static void _ pollwait (struct File
* Filp, wait_queue_head_t * wait_address, poll_table * P)

{

-Struct
Poll_table_entry * entry = poll_get_entry (P );

+ Struct
Poll_wqueues * pwq = container_of (p, struct poll_wqueues, pt );

+ Struct
Poll_table_entry * entry = poll_get_entry (pwq );

If
(! Entry)

Return;

Get_file (filp );

Entry-> filp
= Filp;

Entry-> wait_address
= Wait_address;

-Init_waitqueue_entry (& Entry-> wait,
Current );

+ Init_waitqueue_func_entry (& Entry-> wait,
Pollwake );

+ Entry-> wait. Private
= Pwq;

Add_wait_queue (wait_address,
& Entry-> wait );

}

+ Int poll_schedule_timeout (struct
Poll_wqueues * pwq, int state,

+ Ktime_t * expires, unsigned long Slack)

+ {

+ Int
Rc =-eintr;

+

+ Set_current_state (State );

+ If
(! Pwq-> triggered)

+ RC
= Schedule_hrtimeout_range (expires, slack, hrtimer_mode_abs );

+ _ Set_current_state (task_running );

+

+ /*
Clear triggered for the next iteration */

+ Pwq-> triggered
= 0;

+

+ Return
RC;

+}

 

Int do_select (int n, fd_set_bits
* FDS, S

For
(;;){

Unsigned
Long * rinp, * routp, * rexp, * indium, * outp, * exp;

-Set_current_state (task_interruptible );

Indium
= FDS-> In; outp = FDS-> out; exp = FDS-> ex;

Rinp
= FDS-> res_in; routp = FDS-> res_out; rexp = FDS-> res_ex;

@-411,10 + 436,10 @ int
Do_select (int n, fd_set_bits * FDS, S

To
= & Expire;

}

-If
(! Schedule_hrtimeout_range (to, slack, hrtimer_mode_abs ))

+ If
(! Poll_schedule_timeout (& table, task_interruptible,

+ To, slack ))

Timed_out
= 1;

}

-_ Set_current_state (task_running );

Poll_freewait (& table );

We can see that in this sleep poll patch, we removed the statements deliberately added to adapt to the process state setting, and added a unified Linux sleep/Wakeup mechanism, poll_schedule_timeout is a newly added function. In fact, it is a sleep function in poll, which is essentially different from wait_event. In this way, add these functions, the implementation of poll becomes unified with other callback functions.

In fact, I found that in kernel 2.6.29, the Code became more unified, and the kernel logic became more unified, just like the cred I mentioned in the previous article separating from task_struct, poll sleep makes a lot of sense. For example, in the future, to add a new mechanism for code modification, the callback functions in file_operations are at least unified, in this way, you can separate it into a module without mixing it with other modules.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.