Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Usage Analysis of rtthread

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the analysis of the use of rtthread for you. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

In RT-Thread, the thread does not actually have a running state, and the ready state is equivalent to the running state.

If a thread finishes running, the system automatically deletes the thread: automatically executes the rt_thread_exit () function, first deletes the thread from the system ready queue, then changes the thread's state to the closed state, no longer participates in the system scheduling, and then hangs it into the rt_thread_defunct zombie queue (the thread queue whose resources are not reclaimed and is in the closed state). Finally, the idle thread will reclaim the resources of the deleted thread.

Dynamic is create/delete, static is init/detach.

It should be noted that the stack header address provided by the user needs to be systemically aligned (for example, 4-byte alignment is required on ARM). The parameters and return values of the thread initialization interface rt_thread_init () are shown in the following table:

Make a thread give up processor resources

When the current thread runs out of time slices or voluntarily asks for processor resources, it will no longer occupy the processor and the scheduler will choose the next thread of the same priority to execute. After a thread calls this interface, the thread is still in the ready queue. The thread lets the processor use the following function interface:

Rt_err_t rt_thread_yield (void)

Normally, you should not use this function to suspend the thread itself. If you do need to use the rt_thread_suspend () function to suspend the current task, you need to call the rt_schedule () function immediately after calling the rt_thread_suspend () function to manually switch the thread context. Users only need to know the function of this interface, and it is not recommended.

An idle thread is a thread whose state is always ready, so the hook function must be set to ensure that the idle thread will not be suspended at any time, such as rt_thread_delay (), rt_sem_take () and other functions that may cause the thread to hang.

With regard to delete threads: most threads are executed in a loop and do not need to be deleted; for threads that can be run, RT-Thread automatically deletes the thread after the thread has finished running, and completes the delete action in rt_thread_exit (). Users only need to know what the interface does, and it is not recommended (it can be called by other threads or called in the timer timeout function to delete a thread, but this use is very rare).

The default mode of the RT-Thread timer is HARD_TIMER mode, that is, after the timer times out, the timeout function runs in the context of the system clock interrupt.

There are many ways to synchronize threads, and the core idea is that only one thread (or class) is allowed to run when accessing the critical area. There are many ways to enter / exit the critical zone:

1) call rt_hw_interrupt_disable () to enter the critical section, and call rt_hw_interrupt_enable () to exit the critical section. For more information, please see the global interrupt switch content of "interrupt Management".

2) call rt_enter_critical () to enter the critical section, and call rt_exit_critical () to exit the critical area.

Semaphores can achieve synchronization between threads and mutual exclusion, but it will cause priority inversion. It is recommended to use mutex locks, which will not cause priority inversion.

The use of semaphores

Semaphore is a very flexible synchronization method, which can be used in many situations. The formation of lock, synchronization, resource counting and other relations can also be easily used in the synchronization between threads and threads, interrupts and threads.

The mutex between interrupt and thread should not be in the way of semaphore (lock), but should be in the way of switch interrupt.

In RT-Thread operating system, mutexes can solve the problem of priority reversal, which implements the priority inheritance algorithm. Priority inheritance is to solve the problem caused by priority flipping by raising the priority of thread C to the priority of thread A during the period when thread A tries to obtain shared resources.

Event set is mainly used for synchronization between threads. Unlike semaphores, it can achieve one-to-many and many-to-many synchronization.

Where the event set is used

Event set can be used in a variety of situations, it can to some extent replace semaphores for synchronization between threads. A thread or interrupt service routine sends an event to the event set object, and then the waiting thread is awakened and handles the corresponding event. However, unlike the semaphore, the sending operation of the event cannot be accumulated before the event is cleared, while the release action of the semaphore is cumulative. Another feature of events is that the receiving thread can wait for multiple events, that is, multiple events correspond to one thread or multiple threads. At the same time, depending on the parameters that the thread is waiting for, you can choose whether it is a "logical or" trigger or a "logical and" trigger. This feature is also not available in semaphores. Semaphores can only identify a single release action and cannot wait for multiple types of release at the same time.

Mailbox 4*size, which can only transfer 4 bytes of data at a time

Message queuing n*size transmits n bytes of data each time.

Mailbox is a simple way of message delivery between threads, which is characterized by low overhead and high efficiency. In the implementation of the RT-Thread operating system, a 4-byte message can be delivered at a time, and the mailbox has a certain storage function and can cache a certain number of messages (the number of messages is determined by the capacity specified when creating and initializing the mailbox). The maximum length of a message in a mailbox is 4 bytes, so the mailbox can be used for message delivery of no more than 4 bytes. Because 4 bytes of content on the 32 system happens to place a pointer, when a large message needs to be passed between threads, a pointer to a buffer can be sent to the mailbox as a message, that is, the mailbox can also pass the pointer.

The use of message queues

Message queuing can be used to send variable-length messages, including message exchange between threads, and sending messages to threads in interrupt service routines (interrupt service routines cannot receive messages). The following is divided into two parts: sending messages and synchronous messages to introduce the use of message queues.

The working mechanism of signals

Signal is used as asynchronous communication in RT-Thread. POSIX standard defines sigset_t type to define a signal set. However, sigset_t type may be defined differently in different systems. In RT-Thread, sigset_t is defined as unsigned long type and named rt_sigset_t, and the signals that can be used by applications are SIGUSR1 (10) and SIGUSR2 (12).

The signal is essentially a soft interrupt, which is used to notify the thread that an asynchronous event has occurred. It is used for exception notification and emergency handling between threads. A thread does not have to wait for the signal to arrive through any operation. in fact, the thread does not know when the signal will arrive, and threads can send soft interrupt signals to each other by calling rt_thread_kill ().

Through the above calculation process, we can see some of the key factors: the smaller the amount of data sent, the faster the transmission speed, the greater the impact on data throughput. In the final analysis, it depends on the frequency of interruptions in the system. When a real-time system wants to improve data throughput, there are several ways to consider:

1) increase the length of each data transmission, and let the peripheral send as much data as possible each time.

2) change the interrupt mode to polling mode if necessary. At the same time, in order to solve the situation that the polling mode has been preempting the processor and other low-priority threads can not be run, the priority of the polling thread can be reduced appropriately.

Because turning off the global interrupt will cause the whole system to be unable to respond to the interrupt, it is necessary to ensure that the time to turn off the global interrupt is very short when using it as a means of mutually exclusive access to the critical area. for example, the time to run several machine instructions.

This is the end of the analysis on the use of rtthread. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report