Given the explanation of poll_diff in the previous sections, it might seem that implementing a selectable device is a daunting task. It's actually not as bad as it sounds--the example code may well be shorter than its explanation!
Program 14 shows the implementation of poll_diff in pager.c, which makes its notification interface (/dev/pager/notify) selectable. It is decomposed into a ``top half'' and ``bottom half'' function, exactly as we did for the blocking read implementation in Program 12. First, on lines 1-20, we see the the callback for poll_diff callback itself. It is virtually identical to the read callback in Program 12. The main difference is that it first checks (on line 12) to see if a poll_diff request is already outstanding when a new request comes in. If so, the out-of-date request is destroyed using fusd_destroy, as we described in Section 9.2.
The bottom half is shown on lines 22-46. First, on lines 32-35, it computes the current poll state--if a page has arrived that the client hasn't seen yet, the file is readable; otherwise, it isn't. Next, the driver compares the current poll state with the poll state that the kernel has cached. If the kernel's cache is out of date, the current state is returned to the kernel. Otherwise, it does nothing.
As with the read callback we saw previously, notice that pager_notify_complete_polldiff is called in two different cases:
With this poll_diff implementation, it is possible for a client to open /dev/pager/notify, and block in a select(2) system call. If another client writes page to /dev/pager/input, the first client's select will unblock, indicating the file has become readable.
For additional example code, take a look at the logring example program we first mentioned in Section 8.3. It also supports select by implementing a similar poll_diff callback.