13th-day timer for 30-day self-made operating system (2)

Source: Internet
Author: User

In this section, we will only talk about the optimization of the buffer zone.

We maintain a FIFO buffer for each keyboard, mouse, and timer. If there are 100 timers, we will create 100 FIFO buffers. This is not elegant.

The role of the FIFO buffer: For timer 1, how do we know that timer 1 times out. Assuming that the timeout time is 10 s, after 10 s, Timer 1 is notified of timeout, and data is written to its FIFO buffer, so that elsewhere, we only need to check whether there is data in its FIFO buffer to see if it has timed out. If it times out, we will prompt the corresponding work. In fact, the FIFO buffer plays a role as a correspondent.

Back to our "Elegance" issue, we don't have to maintain a FIFO buffer for every timer, and 100 timers share a buffer so that we can differentiate: after each timer times out, it writes different data to the FIFO buffer. Then we read the data and know which timer times out based on the data. But there is a problem. If there are two timers with the same timeout time, will it be messy? Assuming that the data they write to the FIFO buffer is 5 (5 is generally their timeout), we only need to traverse the timer when we read 5 to see who has the timeout value of 5, the two timers will be processed, so don't worry.

In this way, the first 100 buffers are reduced to one, freeing the following code for checking all the buffer states:

if (fifo8_status(&keyfifo) + fifo8_status(&mousefifo) + fifo8_status(&timerfifo) == 0) {

Otherwise, follow the steps below to export o8_status (& timer1_o2), export o8_status (& timer1_o3 ),.... Similarly, we can merge the buffers of the keyboard, mouse, and timer, and use the number stored in the buffer to distinguish between them.


(Data interruption type written to FIFO)

0 ~ 1 .................................... use a timer to flash the cursor

3 .................................. 3 seconds timer

10 ...... 10 seconds timer

256 ~ 511 ...... enter the keyboard (the value read from the keyboard controller plus 256)

512 ~ 767 ........................ enter the mouse (the value read from the mouse controller plus 512)


In this way, we can use a buffer zone for unified management.

In this way, you only need to check the status of a buffer, instead of checking the status of 1000 or 3, which greatly improves the performance.

The following is the comparison before and after optimization. The larger the number in the counter window, the better the performance.


Before optimization:




After optimization:



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.