Spice server timer mechanism

Source: Internet
Author: User

1. Definition of Ring

typedef struct Ring RingItem;typedef struct Ring {    RingItem *prev;    RingItem *next;} Ring;

2. Definition of spicetimer

typedef struct SpiceTimer {      RingItem link;      SpiceTimerFunc func;      struct timeval tv_start;      int ms;      void *opaque;  } Timer;    Ring timers;

(1) The link element of the two-way linked list is used to connect multiple timers to a two-way cyclic queue. The link is mounted to the ring as something similar to a handle or pointer, the macro can be used to obtain the timer structure through link.

(2) The function pointer func points to an executable function. When the timer expires, it will execute this function.

(3) TV _start is the scheduled start time.
(4) ms is the time length to be scheduled
(5) opaque should be other information

3. Add a timer

Static spicetimer * timer_add (spicetimerfunc func, void * opaque) {spicetimer * timer = calloc (sizeof (spicetimer), 1 ); // apply for the memory address timer for the timer-> func = func; // register the function timer called by the timer-> opaque = opaque; // input the opgger parameter ring_add (& timers, & timer-> link); // Add the timer to the timers ring and return timer; // returns the timer pointer}

4. Start a timer

Static void timer_start (spicetimer * timer, uint32_t MS) {assert (MS); // assert that Ms is 0, not to continue running, if yes, exit the function gettimeofday (& timer-> TV _start, null); // get the current precise time, put the TV _start timer-> MS = MS; // assign the timer wait time ms to timer-> MS // already add MS to timer value add_ms_to_timeval (& timer-> TV _start, MS); // The current time plus ms, in this way, TV _start stores the timer to the point}

5. Cancel a timer

Static void timer_cancel (spicetimer * timer) {timer-> MS = 0; // assign the timer time ms to 0}

6. delete a timer.

Static void timer_remove (spicetimer * timer) {ring_remove (& timer-> link); // delete from the timer Ring}

7. Summary

Because the timer mechanism is frequently used in SPICE, it is relatively difficult to read the code. In fact, as long as we grasp the key points of the timer mechanism, in this case, you will have a clear understanding of function calls in many places in the Code. First, if a function requires a timer mechanism, then it will be added to the timer ring by using the timer_add function as the timer func function. Second, it will set the timer time of the timer and start the timer until the time reaches, the func function will be executed. That is to say, you should not see this function in this place and he will execute it. Maybe it just came out and said hello to you.

 

 

 

 

 

 

 

 

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.