Summary of several time delay methods under VxWorks

Source: Internet
Author: User

1 taskdelay
Taskdelay () provides a simple task sleep mechanism, which is also commonly used in applications that require a timing/latency mechanism.

The format is status taskdelay (INT ticks/* Number of ticks to delay task */),

We can see that the unit for implementing latency using this function is tick (generally, one tick in the system is MS-level ).

In VxWorks, The taskdelay () function can be used as follows: taskdelay (sysclkrateget () * 1 ).

The sysclkrateget () function returns the clock rate of the system, measured in tick count/second (the system clock rate can be changed using the sysclkrateset () function ). In POSIX, there is a function corresponding to taskdelay ()-nanosleep () (described below ). The two functions only have different delay units, and the effect is the same.
With taskdelay (), you can move the called task to the end of the ready queue with the same priority. In particular, you can call taskdelay (0) to deliver the CPU to other tasks with the same priority in the system. A call with a delay of 0 can only be used in taskdelay (). nanosleep () considers this call as incorrect.
Taskdelay () will cause the called task to discard the CPU during the specified delay period (counted by ticks), so that the task is in the delay state (therefore, it cannot be used to interrupt the service program ). It is usually affected by task scheduling, but it is useful when waiting for some external conditions that are not associated with the interruption. If the called task receives a signal indicating that it is not blocked or ignored, taskdelay () Returns Error and sets errno to eintr after the signal processing program runs.
2 Watchdog
VxWorks provides a watchdog timer mechanism. With the provided functions, any task can create a watchdog timer. After a specified delay, to run the specified program in the context of the system clock ISR. In VxWorks, the watchdog timer is maintained as part of the system clock interruption service program. Therefore, the function is associated with the watchdog timer at the system clock interruption level. When the watchdog timer is used to implement Timing/latency, the application interruption takes priority over the system clock interruption at the specified time. However, if the kernel cannot immediately execute this interrupt service program, this task is queued to the texctask work queue, In the texctask
The task priority in the work queue is texctask (usually 0 ). For interrupt service programs that use the watchdog timer, the general ISR rules must be followed.
You can use wdcreate () to create a watchdog timer. Call wdstart () to start the timer. The delay parameter is in the unit of tick. You must also specify the program to be called after the timer is completed. If your application requires multiple watchdog functions, use wdcreate () to generate an independent watchdog ID for each requirement. Because only the nearest wdstart () is valid for the given watchdog ID. You can call wdcancel () to cancel a watchdog timer before the specified tick count reaches.
Each time wdstart () is called, the watchdog timer is executed only once. For applications that require periodic execution. To achieve this effect, the timer function must call wdstart () recursively to restart the timer.
With the watchdog timer, the called task is not blocked because wdstart () is returned immediately. This method is used to implement latency. The associated functions are too limited, so many system calls are unavailable and depressing...
3 timer (POSIX)
3.1 Timer
VxWorks provides the ieee posix 1003.1b standard timer interface. With this timer mechanism, after a specified time interval, the task sends a signal to itself. The timer is built on the clock and signal.
You can create, set, and delete a timer. When the timer expires, the default signal (sigalrm) will be sent to the task ).
General timer process:
/* Create a timer */
If (timer_create (clock_realtime, 0, & mytimer) = Error)
Return (error );
/* Connect the user program to the timer */
If (timer_connect (mytimer, (voidfuncptr) my_handler, 0) = Error)
Return (error );
/* Set the timer value */
If (timer_settime (mytimer, 0, & Value, 0) = Error)
Return (error );
/* Latency */
......
/* Delete the timer */
If (timer_delete (mytimer) = Error)
Return (error );
When a timer is used, it is easy to ignore the process of sending signals to the task after the timer ends. That is to say, after the timer ends, the timer program will not be able to run if the timer creation task does not exist. The error message "interrupt: timerwdhandler: kill failed (timer = ******, tid = ******, errno = 0x16) "(there is) A few questions about the function!" Post, which mentions this issue ). When I was on the machine, I set a timer for a long time, and used the same latency operation (taskdelay () and nanosleep () in the task of creating a timer ), at this time, the program runs normally. After the timer ends, the program associated with the timer is correctly executed. After the timer ends, I delete the task of creating the timer under shell, the preceding error occurs after the scheduled task is completed. Therefore, when using the POSIX timer, be sure not to let the task of creating the timer end before triggering the Timer Program, otherwise... Prepare Reset
3.2 nanosleep ()
The function of nanosleep () is similar to taskdelay () provided by VxWorks. nanosleep () allows you to specify the scheduled/delayed time in seconds and seconds. taskdelay () tick is used as the Scheduled/delayed time. The two are only different delay units, rather than different precision, which are determined by the system clock frequency. Like taskdelay (), tasks that use nanosleep () for timing and latency are also affected by scheduling. Pay attention to this in actual operations.
When nanosleep () is used, the timing parameter uses the it_value in the itimerspe structure. You can specify the second and second in it_value as the Scheduled/delayed time. Functions are easy to use.

4 others
You can also use msgqreceive () and semtask () with a timeout value of timeout to implement latency (is there a person who uses this method for latency? No? Yes? No? Yes? No ?...).
This method will block the task and cause it to wait for timeout. To achieve latency, you can use the semtask (..., Timeout), msgqreceive (...,...,..., Timeout ). Call tasks are put into the blocking queue. (Note that the value of time cannot be no_wait when semtask and msgqreceive are used. If no_wait is specified, the system will return immediately, in this case, the latency is not achieved. ^_^ ). If you want to keep the task from being blocked, you can continue to execute the task. Sorry, this method is not feasible. Use the dog.
When the blocked task meets the conditions for continued execution, it will be placed in the ready queue. It is important to note that the task is put into the ready queue, which does not mean that the task is executed immediately. Think about it. If a task with a higher priority is occupying resources or waiting for resources, then... Latency will be uncertain.

The new Code timer uses 3. Timer

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.