Embodiment
A kind of implementation method and device of the timer that provides is provided the embodiment of the invention, below is elaborated respectively.
Embodiment one,
Please refer to Fig. 1, present embodiment provides a kind of implementation method of timer, comprises step:
101, the timer processing task sorts each timer to generate a timer chain meter.
In this step, at first create a timer processing task, prepare to carry out timer processing, the corresponding semaphore X of this task, this task is stopped up by semaphore X at ordinary times, and so-called semaphore just is equivalent to a sign; Obstruction is exactly to refer to just there is not semaphore when timer is less than the time, and task can not be performed, and timer is after semaphore is activated during to the time, and task could be carried out, and improves system effectiveness with this.
The timer processing task of this establishment with each timer according to predetermined rule compositor, and the timer chain meter of generation that is connected to each other, to prepare after semaphore X is activated, handling overdue task of timer successively according to the ordering of timer in this timer chain meter.
Described herein timer can be a software timer.
102, timer interrupts judging whether timer counter reaches default interrupting value, if, then enter Interrupt Process, send an activation signal to described timer processing task.
Timer is set interrupts (being WatchDog Timer), when timer counter during to default interrupting value, cause that timer interrupts, system enters Interrupt Process, otherwise timer continues to count.It is fixed that this interrupting value comes according to reality, is set at 1 second usually and gets final product, if some special occasions needs the Millisecond timer, by sysClkRateSet system clock tick number is made as 1000 and gets final product.
In Interrupt Process, timer interrupt enable semaphore X, that is, timer interrupts sending an activation signal to the timer Processing tasks, handles to be carried out task of timer by the timer processing task; Timer interrupts restarting subsequently, withdraws from Interrupt Process.
103, the timer processing task is carried out task of timer according to the ordering of each timer in the timer chain meter and is handled after receiving activation signal.
The timer processing task detects all semaphores of corresponding various tasks in real time, when finding that semaphore X is activated, promptly, the timer processing task is after receiving activation signal, the task of timer processing task call semaphore X correspondence is handled overdue task of timer, and this task is stopped up by semaphore X after finishing dealing with, in other words, stopping to carry out task of timer exactly handles.
In one embodiment, step 101 can specifically comprise:
101a, timer chain meter head of generation.This timer chain meter head can comprise: point to first timer in the described timer chain meter first pointer, point to second pointer and the chained list expiration time of last timer in the described timer chain meter, described chained list expiration time equals the expiration time of described last timer.
101b, with the expiration time of timer as sort by, each each timer is connected to described timer chain meter head generates timer chain meter afterwards, wherein, the timer that expiration time is identical, is connected in the timer chain meter as sort by with the precedence called.
Each timer in the timer chain meter can comprise: point to the last pointer of a timer, the following pointer that points to next timer, the function pointer of function that points to the task of timer correspondence and the expiration time and the deletion time of this timer, the expiration time that the described deletion time equals this timer deducts the expiration time of a timer.
In one embodiment, step 103 specifically can comprise:
The timer processing task judges whether to reach the expiration time of first timer in the described timer chain meter after receiving activation signal, if, then handle this task of timer, and this timer taken out from described timer chain meter, otherwise, stop to carry out task of timer and handle.
Thereby, when first timer in chained list expires, handle this task of timer, and this timer taken out from timer chain meter; So second timer originally becomes first new timer, then the timer processing task is judging once more whether first new timer expires, thereby handles or stop; Till undue timer finally became first timer, the timer processing task stops to carry out task of timer to be handled, and receives new activation signal by the time.
In one embodiment, also comprise after the step 101: be priority of timer processing task setting.The timer processing task is carried out task of timer according to the priority of setting and is handled.Generally speaking, the setting range of priority is 1-100, and the priority of this task can be provided with higherly, so just can in time handle overdue timer, if low excessively, some overdue timer just may be delayed processing.
Adopt the present embodiment method, can handle overdue task of timer successively by ordering timely, can not produce a large amount of interruptions, thereby not influence other task executions; By ordering whether task of timer expired and to check, needn't check whole task of timer, can reduce the required time of checking, thereby can improve the stability and the reliability of operating system.
Below, how timer processing task in the preferred implementation is generated timer chain meter and how to carry out the task of timer processing according to the ordering in this timer chain meter, be further described in detail.
The corresponding structure of each timer, this structure has two pointer members, a member next points to next timer, another member prev points to a timer, this structure also has a function pointer member func, when expiring, carries out by timer the function (this task of timer) of this pointed, this structure also has a member time to represent when this timer expires, this structure also has a member delta, represent that when delta is 0 timer expires, can carry out this task of timer and this timer is deleted from timer chain meter.
It is the header structure of timer (being linked list head) that a structure is arranged in addition, this structure has two pointer members, a member fwd points to first timer, and another member bwd points to last timer, and this structure also has a member time to represent last timer time then.
Timer is stored in the timer chain meter, and when application program was created a timer, it will add a node in timer chain meter.Suppose now to create a timer T, t1 expires after second, the process that node adds is such: (1) is if first timer is sky, then first timer and last timer with timer chain meter all points to this node T, and the time of timer chain meter head and the delta of T are made as t1.(2) if first timer is not sky and the t1 time more than or equal to the timer chain meter head, just T is put into the last of timer chain meter, the delta of T is made as the time that t1 deducts the timer chain meter head, and the time of timer chain meter head is made as t1.(3) not meeting the two kinds of situations in front just handles by this situation, begin to add up one by one delta from first timer, accumulated value has surpassed t1 when supposing to be added to certain node P, at this moment T is inserted into before the P, the delta of P is made as accumulated value deducts t1, the delta of T is made as the original delta of P and deducts the current delta of P.The change procedure of chained list when adding timer with real example below.
When the most initial, it all is empty that the timer chain meter head points to the timer pointer, as shown in Figure 3.
At first add a timer A, expire after 1 second, by top step (1) operation, timer chain meter becomes as shown in Figure 4.
Then add a timer B, expire after 2 seconds, by top step (2) operation, timer chain meter becomes as shown in Figure 5.
Then add a timer C, expire after 4 seconds, by top step (2) operation, timer chain meter becomes as shown in Figure 6.
Then add a timer D, expire after 2 seconds, by top step (3) operation, timer chain meter becomes as shown in Figure 7.
The task of timer processing procedure is as follows: (1) checks first timer, if first timer is blocked in semaphore X there for empty just end inspection, if first timer does not just subtract 1 with the time of timer chain meter head for empty, the delta of first timer is subtracted 1.(2) begin to check from first timer, if the delta of first timer 0 just finishes to check and is blocked in semaphore X there, if being 0 this timer of expression, the delta of first timer expired, so first timer is extractd from timer chain meter, carry out the function of first timer correspondence, jump to step (2) after executing and continue to carry out.Situation about carrying out below with the real example task of timer.
Suppose that the current moment is any one time between 0 second to 1 second, suppose that current timer has A, B, C, four of D, timer chain meter is as shown in Figure 7.
When time is up 1 second the time, timer down trigger timer processing task, this task is at first looked for first timer, and at this moment first timer is A, becomes 3 so the time of timer chain meter head subtracts 1, and the delta of A subtracts 1 and has become 0.The delta of A is that 0 expression has expired, and just A is extractd from timer chain meter, carries out the function of A correspondence.Extract after the A, first timer of timer chain meter has become B, as shown in Figure 8.Task continues to check first timer, and the delta of B is that 1 expression is also not yet due, so the task termination inspection is blocked in semaphore X there.
When time is up 2 seconds the time, timer down trigger timer processing task, this task is at first looked for first timer, and at this moment first timer is B, becomes 2 so the time of timer chain meter head subtracts 1, and the delta of B subtracts 1 and has become 0.The delta of B is that 0 expression has expired, and just will extract in the B timer chain meter, carries out the function of B correspondence.Extract after the B, first timer of timer chain meter has become D, as shown in Figure 9.Task continues to check first timer, and the delta of D is that 0 expression has expired, so D is extractd from timer chain meter, carries out the function of D correspondence.Extract after the D, first timer of timer chain meter has become C, as shown in figure 10.Task continues to check first timer, and the delta of C is that 2 expressions are also not yet due, so the task termination inspection is blocked in semaphore X there.
When time is up 3 seconds the time, timer down trigger timer processing task, this task is at first looked for first timer, at this moment first timer is C, so the time of timer chain meter head subtracts 1 and becomes 1, the delta of C subtracts 1 and has become 1, and the timer timer chain meter as shown in figure 11.The delta of C is that 1 expression is also not yet due, so the task termination inspection is blocked in semaphore X there.
When time is up 4 seconds the time, timer down trigger timer processing task, this task is at first looked for first timer, and at this moment first timer is C, becomes 0 so the time of timer chain meter head subtracts 1, and the delta of C subtracts 1 and has become 0.The delta of C is that 0 expression has expired, and just C is extractd from timer chain meter, carries out the function of C correspondence.Extract after the C, timer chain meter has become as shown in Figure 3.Task continues to check first timer, because first timer is empty, so the task termination inspection is blocked in semaphore X there.
When time is up 5 seconds the time, timer down trigger timer processing task, this task is at first looked for first timer, and at this moment first timer is empty, so the task termination inspection is blocked in semaphore X there.
Embodiment two,
Please refer to Fig. 2, the embodiment of the invention provides a kind of implement device of timer, comprising: timer processing TU task unit 201 and timer interrupt location 202, described timer processing TU task unit comprises sequencing unit 201a and processing unit 201b.
Described timer interrupt location 202 is used to judge whether timer counter reaches default interrupting value, if, then enter Interrupt Process, send an activation signal to described timer processing TU task unit.
Described sequencing unit 201a is used for each timer ordering to generate a timer chain meter;
Described processing unit 201b is used to receive the activation signal that described timer interrupt location sends, and carries out task of timer according to the ordering of each timer in the described timer chain meter and handles.
In one embodiment, described sequencing unit 201a, specifically be used to generate a timer chain meter head, with the expiration time of timer as sort by, each each timer is connected to described timer chain meter head generates timer chain meter afterwards, wherein, the timer that expiration time is identical, is connected in the timer chain meter as sort by with the precedence called.
In one embodiment, described processing unit 201b, specifically be used for judging whether reaching the expiration time of first timer of described timer chain meter, if, then handle this task of timer, and this timer taken out from described timer chain meter, otherwise, stop to carry out task of timer and handle.
In one embodiment, timer processing TU task unit 201 comprises that also priority is provided with unit 201c, and this priority is provided with unit 201c, is used to priority of timer processing task setting.
Adopt the present embodiment device, can handle overdue task of timer successively by ordering timely, can not produce a large amount of interruptions, thereby not influence other task executions; By ordering whether task of timer expired and to check, needn't check whole task of timer, can reduce the required time of checking, thereby can improve the stability and the reliability of operating system.
One of ordinary skill in the art will appreciate that all or part of step in the whole bag of tricks of the foregoing description is to instruct relevant hardware to finish by program, this program can be stored in the computer-readable recording medium, and storage medium can comprise: ROM (read-only memory), random-access memory, disk or CD etc.
More than the implementation method and the device of the timer that the embodiment of the invention provided is described in detail; having used specific case herein sets forth principle of the present invention and embodiment; the explanation of above embodiment just is used for helping to understand method of the present invention and core concept thereof; should not be construed as limitation of the present invention; within the spirit and principles in the present invention all; any modification of being done, be equal to and replace and improvement etc., all should be included within protection scope of the present invention.