当前位置:网站首页>Porting RT thread to s5p4418 (V): thread communication
Porting RT thread to s5p4418 (V): thread communication
2022-06-30 06:40:00 【Mnnk】
RT-Thread Three inter thread communication modes are implemented , mailbox (mailbox)、 Message queue (messagequeue)、 and The signal (signal).
mailbox
Each email in the mailbox can only hold fixed 4 Byte content . Any thread can send or receive mail messages .
Mailbox control block
struct rt_mailbox
{
struct rt_ipc_object parent;
rt_uint32_t* msg_pool; /* Start address of mailbox buffer */
rt_uint16_t size; /* Size of mailbox buffer */
rt_uint16_t entry; /* The number of messages in the mailbox */
rt_uint16_t in_offset, out_offset; /* Incoming and outgoing pointer of mailbox buffer */
rt_list_t suspend_sender_thread; /* Send thread's pending wait queue */
};
typedef struct rt_mailbox* rt_mailbox_t;

New Mailbox
rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
.msg_poolIt's a circular queue ,FIFO, adopt.in_offset,.out_offsetRecord the entry and exit position subscript ..suspend_sender_threadWhen the mailbox is full , The sending thread will hang on the chain .
Send E-mail
rt_err_t rt_mb_send_wait (rt_mailbox_t mb, rt_uint32_t value)
When sending mail , It can only be sent when the mailbox is not full , If the mailbox is full , You can wait according to the waiting time set by the user , When mail in the mailbox is collected and space is made available , Waiting for the suspended sending thread to be awakened to continue the sending process , When the waiting time is up and the email has not been sent yet , Or wait time is not set , Failed to send mail at this time , The thread sending the mail or the interrupt program will receive an error code ( -RT_EFULL). If the email is sent successfully , It will arouse the thread waiting to receive mail . Threads can send mail with blocking , But you can't send mail in any way with blocking during interruption .
Take mail
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
When receiving mail , According to... In the mailbox control block entry Determine whether there are messages in the queue , If the email in the mailbox is not empty , Then according to out_offset Find the mail that was first sent to the mailbox and receive it . When receiving, if the mailbox is empty , If the user sets the wait timeout , The system will suspend the current thread , When the set timeout is reached , When the email is still not received , Then the thread will wake up and return -RT_ETIMEOUT. If there is a message in the mailbox , Then the receiving thread will copy the 4 Bytes of mail to the receiving thread . If the reception is successful , It will arouse the thread waiting for sending .
Engineering documents
- rtt2a9_mailbox
Threads 2 Send E-mail , Threads 1 Wait for the key press event to receive the mail message .
Message queue
Message queuing is another common way of inter thread communication , Is an extension of the mailbox . It can be used in many occasions : Message exchange between threads 、 Use serial port to receive variable length data, etc .
- Compared to mailbox , Message queues can send messages of variable length , But there is no pending list of sending threads .
- Support sending emergency messages , The emergency message will be added directly to the right person .
Message queuing control block
struct rt_messagequeue
{
struct rt_ipc_object parent;
void* msg_pool; /* Pointer to the buffer where the message is stored */
rt_uint16_t msg_size; /* The length of each message */
rt_uint16_t max_msgs; /* The maximum number of messages that can be accommodated */
rt_uint16_t entry; /* Number of messages already in the queue */
void* msg_queue_head; /* Message chain header */
void* msg_queue_tail; /* The end of the message chain */
void* msg_queue_free; /* Idle message linked list */
};
typedef struct rt_messagequeue* rt_mq_t;

Create a message queue
rt_mq_t rt_mq_create(const char *name,
rt_size_t msg_size,
rt_size_t max_msgs,
rt_uint8_t flag);
.msg_size Pay attention to alignment . After creation , The entire linked list is a free block ,.msg_queue_free Point to the meter head ,.msg_queue_head and .msg_queue_tail Point to RT_NULL.
Send a message
rt_err_t rt_mq_send(rt_mq_t mq, void *buffer, rt_size_t size);
Remove from the free linked list .msg_queue_free Free block to , Add to the end of the message queue . If the message queue is empty , Update .msg_queue_head and .msg_queue_tail. When sending an emergency message , Add the message directly to the right end .
receive messages
rt_err_t rt_mq_recv(rt_mq_t mq,
void *buffer,
rt_size_t size,
rt_int32_t timeout);
adopt .entry Determine whether there are messages in the queue , No, , Suspend or return directly . Yes , Then get the message from the header , After copying the data, hang the memory block to the idle queue .
Engineering documents
Threads 2 Send a message , There is a semaphore in the message , For threads 1 Notify the thread after receiving the message 2 Keep sending messages .
The signal
The signal ( Also called soft interrupt signal ), At the software level, it is a simulation of interrupt mechanism , In principle , A thread receiving a signal is similar to a processor receiving an interrupt request .
Signal related structure :
struct siginfo
{
rt_uint16_t si_signo;
rt_uint16_t si_code;
union sigval si_value;
};
typedef struct siginfo siginfo_t;
struct rt_slist_node
{
struct rt_slist_node *next; /**< point to next node. */
};
typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */
struct siginfo_node
{
siginfo_t si; /* Signal value ; Signal type */
struct rt_slist_node list; /* list.next Point to next siginfo_node */
};
static struct rt_mempool *_rt_siginfo_pool;
Signal related attributes in thread control block :
#if defined(RT_USING_SIGNALS)
rt_sigset_t sig_pending; /**< the pending signals */
rt_sigset_t sig_mask; /**< the mask bits of signal */
void *sig_ret; /**< the return stack pointer from signal */
rt_sighandler_t *sig_vectors; /**< vectors of signal handler */
void *si_list; /**< the signal infor list */
#endif
.sig_pending,32bit Express 32 Medium signal , Set up 1 Indicates that the relevant signal occurs , be in pending state ..sig_mask, Shield bit , Set up 1, Indicates interest in related signals ..sig_ret, Record the top value of the original stack ..sig_vectors, Similar to the exception vector table , Record the address of each signal processing function ..si_list, Hang untreatedsiginfo_nodenode
Signal initialization
Apply for a memory pool , The size of each memory block is sizeof(struct siginfo_node).
int rt_system_signal_init(void){
_rt_siginfo_pool = rt_mp_create("signal", RT_SIG_INFO_MAX, sizeof(struct siginfo_node));
if (_rt_siginfo_pool == RT_NULL)
{
// LOG_E("create memory pool for signal info failed.");
RT_ASSERT(0);
}
return 0;
}
Installing signals for threads
rt_sighandler_t rt_signal_install(int signo, rt_sighandler_t handler);
If the thread wants to process a signal , Then install the signal in the thread . The interface is based on handler Modify the thread's .sig_vectors surface ,SIG_IGN Indicates that the signal is ignored ,SIG_DFL Indicates that the signal is processed in the system default way , Or pass in the handler address specified by the user . Successfully return the... Before installing the signal handler value .
Blocking signals
void rt_signal_mask(int signo);
Will thread the sig_mask Corresponding position 0, Indicates no interest in the signal . The signal will not be processed after it occurs .
Unblock the signal
void rt_signal_unmask(int signo);
Will thread the sig_mask Corresponding position 1, Indicates interest in the signal . stay RT-Thread During installation of medium signal , The default is blocked . If the signal is found to be suspended after the signal blocking is removed , Call immediately void _signal_deliver(rt_thread_t tid) Signal transmission .
_signal_deliver The function is :
If the target thread is suspended , Will wake up the thread , And set the signal status bit of the thread .
If the target thread is ready ( Waiting to be dispatched ), And the target thread is the current thread , Call
void rt_thread_handle_sig(rt_bool_t clean_state);To process the signal ; If the target thread is not the current thread , First initialize the context of the signal processing function , thenrt_schedule().
Sending signal
int rt_thread_kill(rt_thread_t tid, int sig);
Send a signal to the target thread through this interface . After sending the signal, the signal node of the thread control block points to .
- If the signal is not suspended , Then suspend , Then apply
siginfo_nodenode , And hang the node ontid->si_listOn . - If the signal is suspended , Update relevant information .
- signal.c: 346
signal processing
void rt_thread_handle_sig(rt_bool_t clean_state);
- First, judge whether the signal is suspended , And
tid->sig_maskLocation 1. - And then from
tid->si_listRemove the signal control node from the chain , fromtid->sig_vectorsGet the address of the function processing the signal in the table . - Clear the pending flag bit after processing .
- signal.c: 141
Timing of signal processing :
- When the thread is scheduled . Would call
rt_thread_handle_sig(RT_TRUE);Check thread status . - When sending a signal . adopt
_signal_deliver(rt_thread_t tid);Transmitting signals .
Engineering documents
- 111_rtt2a9_signal
In the last project ( Message queue ) On the basis of modification , Key 2 To thread 1 send outSIGUSR1The signal . Threads 1 Print the signal value after receiving the installed signal .
边栏推荐
- 1.3 - Code System
- RT thread Kernel Implementation (VI): time slice
- The most complete sentence in history
- Judge whether H5 is in wechat environment or enterprise wechat environment at both ends
- Unclear about glide loading picture
- When to use redis
- Installing googleplay environment on Huawei mobile phones
- 【微信小程序:单选、多选样式,背景色,圆角】
- SOC项目AHB_SD_HOST控制器设计
- 1.5 - 逻辑运算
猜你喜欢

【我的创作纪念日】一周年随笔

File transfer protocol, FTP file sharing server

Detailed description of methods in the interface

Why does ETL often become ELT or even let?

0基础转行软件测试,如何实现月薪9.5k+

SOC_AHB_SD_IF

MySQL中的InnoDB引擎

Wechat applet mall project

Using C language pure for loop to implement ilovey

Unable to access the Internet at win10 /11 hotspot
随机推荐
IO streams (common streams)
ROS multi machine
ETL为什么经常变成ELT甚至LET?
1.5 - logical operation
RT thread Kernel Implementation (I): threads and scheduling
List in set (2)
1.4 - fixed and floating point numbers
New project folder based on PIO plug-in in vscode -- Interpretation
Bat 使用细节2
Software tools_ Shortcut_ Operation summary
ini解析学习文档
Initial love with mqtt
Traitement d'images 7 - amélioration d'images
Ini parsing learning documents
【微信小程序:单选、多选样式,背景色,圆角】
Pay attention to this live broadcast and learn about the path to achieve the dual carbon goal of the energy industry
Why does the verification code not refresh when clicked
Simple example of class template
1.8 - multi level storage
SOC_AHB_SD_IF

