Rt_thread Message Queuing

Source: Internet
Author: User
Tags error code message queue
One, Message Queue control block: in Include/rtdef.h #ifdef rt_using_messagequeue
/**
* Message Queue structure
*/
struct Rt_messagequeue
{
struct Rt_ipc_object parent; /**< inherit from Ipc_object *///Inherit from IPC object

void *msg_pool; /**< start address of messages queue *///Message Queuing header

rt_uint16_t msg_size; /**< message size of each message * *//Maximum length of messages
rt_uint16_t max_msgs; /**< max number of messages *///Message Queue maximum capacity of message bars

rt_uint16_t entry; /**< index of messages in the queue *///the number of message bars present in the current message queue

void *msg_queue_head; /**< List Head *//team First
void *msg_queue_tail; /**< List Tail *//Team Tail
void *msg_queue_free; /**< Pointer indicated the free node of the queue *///points to the idle queue
};
typedef struct RT_MESSAGEQUEUE *rt_mq_t;
#endif Copy code above is the definition of a Message Queuing control block, what is the definition of each message element contained within a message queue? defined in SRC/IPC.C: struct rt_mq_message
{
struct Rt_mq_message *next;//points to the next message element
};
Maybe everyone will have a question, how does the message element not have the specific data content of the message?
In fact, the above message element can only be counted as the message header, like the text message title generally, the real internal following the message header, until the next message header in all memory data is the true content of the message. Rt-thread uses a static queue to implement the IPC function of Message Queuing. The message header is simply used to index the function, through which it can be indexed to the location of the next message header. If you still do not understand, then you can first see how the message queue is initialized, you will understand why there is such a message header. Copy code two, Message Queuing-related interfaces: in SRC/IPC.C: Creating a message queue:
rt_mq_t rt_mq_create (const char *name,//Name of message queue
rt_size_t msg_size,//maximum length per message in Message Queuing
rt_size_t max_msgs,//maximum capacity of Message Queuing
rt_uint8_t flag); Wait mode used by Message Queuing (Fifo/prio)
When creating a message queue, a Message Queuing object control block is created, then a chunk of memory is allocated to the message queue and organized into an idle message list, which is equal to the product of the message size + message header (for linked list connections) and the capacity of the message queue, and then initializes the message queue, at which time the message queue is empty.

To delete a message queue:
rt_err_t Rt_mq_delete (rt_mq_t MQ);
When you delete a message queue, if the thread is suspended on the message queue waiting queue, the kernel wakes up all threads that are pending on the message waiting queue (The return value is-rt_error), and then releases the memory used by Message Queuing, and finally deletes the Message Queuing object. Replication code initializes Message Queuing:
rt_err_t Rt_mq_init (rt_mq_t MQ,//handle to a static Message Queuing object
const char *name,//Name of message queue
void *msgpool,//buffer header address for storing messages
rt_size_t msg_size,//maximum length per message in Message Queuing
rt_size_t pool_size,//buffer size for storing messages
rt_uint8_t flag); Wait mode used by Message Queuing (Fifo/prio)
When Message Queuing is initialized, the interface needs to obtain a handle to the Message Queuing object (that is, a pointer to the control block of the Message Queuing object), the message queue name, the message buffer pointer, the message size, and the message queue capacity. After the message queue is initialized, all messages are hung on the idle message list, and Message Queuing is empty.

Out of Message Queuing:
rt_err_t Rt_mq_detach (rt_mq_t MQ);
With this function interface, the kernel wakes all threads that hang on the message Waiting queue object (The return value is-rt_error), and then removes the Message Queuing object from the Kernel object manager. Copy code to send a message:
rt_err_t Rt_mq_send (rt_mq_t mq, void *buffer, rt_size_t size);
Either the thread or the Interrupt service program can send messages to the message queue. When a message is sent, the Message Queuing object takes the next idle message block from the idle message list, copies the message content sent by the thread or interrupt service program to the message block, and then hangs the message block to the end of the message queue. The sender can successfully send a message only if there is an idle message block available on the idle message list, and when no message block is available on the idle message list stating that the message queue is full, the thread or interrupt program that sent the message receives an error code (-RT_EFULL).
When sending a message, the sender specifies the object handle (that is, a pointer to the Message Queuing control block) sent to the message queue, and specifies the message content and message size to be sent. After sending a normal message, the first message block removed from the idle message list is transferred to the end of the message queue.

Send an emergency message:
rt_err_t rt_mq_urgent (rt_mq_t mq, void *buffer, rt_size_t size);
The process of sending an emergency message is almost the same as sending a message, except that when an emergency message is sent, the first message block from the list of idle messages is not hung to the end of the queue, but rather to the head of the message queue, so that the recipient receives the emergency message first and the message is processed in a timely manner.

Receive Message:
rt_err_t Rt_mq_recv (rt_mq_t MQ,//handle to Message Queuing object
void *buffer,//block of data for receiving messages
rt_size_t size,//message sizes
rt_int32_t timeout); Specified time-out
When there is a message in the message queue, the recipient can receive the message, or the receiver sets or suspends the queue of waiting threads on the message queue, or returns directly, depending on the time-out.
When receiving a message, the receiver specifies a handle to the message queue object that stores the message, and specifies a memory buffer in which the received message content will be copied into the buffer. In addition, you need to specify the time-out when the message is not being picked up in time.
When a message is received, the first message on the message queue is transferred to the end of the idle message list. Copy code Control Message Queuing:
rt_err_t Rt_mq_control (rt_mq_t MQ, rt_uint8_t cmd, void *arg);
Only the Rt_ipc_cmd_reset command is supported, which means that Message Queuing is reset. Copy Code

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.