Development Guidelines

Available APIs

Category

API

Description

Creating or deleting a message queue

LOS_QueueCreate

Creates a message queue. The system dynamically allocates the queue space.

LOS_QueueDelete

Deletes the specified queue based on the queue ID.

Reading or writing data in a queue (without the content contained in the address)

LOS_QueueRead

Reads data in the head node of the specified queue. The data in the queue node is an address.

LOS_QueueWrite

Writes the value of the input parameter bufferAddr (buffer address) to the tail node of the specified queue.

LOS_QueueWriteHead

Writes the value of the input parameter bufferAddr (buffer address) to the head node of the specified queue.

Reading or writing in a queue (with the content contained in the address)

LOS_QueueReadCopy

Reads data from the head node of the specified queue.

LOS_QueueWriteCopy

Writes the data saved in the input parameter bufferAddr to the tail node of the specified queue.

LOS_QueueWriteHeadCopy

Writes the data saved in the input parameter bufferAddr to the head node of the specified queue.

Obtaining queue information

LOS_QueueInfoGet

Obtains information about the specified queue, including the queue ID, queue length, message node size, head node, tail node, number of readable nodes, number of writable nodes, tasks waiting for read operations, and tasks waiting for write operations.

How to Develop

  1. Call LOS_QueueCreate to create a queue. The queue ID is returned when the queue is created.
  2. Call LOS_QueueWrite or LOS_QueueWriteCopy to write messages to the queue.
  3. Call LOS_QueueRead or LOS_QueueReadCopy to read messages from the queue.
  4. Call LOS_QueueInfoGet to obtain queue information.
  5. Call LOS_QueueDelete to delete the queue.

NOTE:

  • The maximum number of queues supported by the system is the total number of queue resources of the system, not the number of queue resources available to users. For example, if the system software timer occupies one more queue resource, the number of queue resources available to users decreases by one.
  • The input parameters queue name and flags passed when a queue is created are reserved for future use.
  • The input parameter timeOut in the queue interface function is relative time.
  • LOS_QueueReadCopy, LOS_QueueWriteCopy, and LOS_QueueWriteHeadCopy are a group of APIs that must be used together. LOS_QueueRead, LOS_QueueWrite, and LOS_QueueWriteHead are a group of APIs that must be used together.
  • As LOS_QueueWrite, LOS_QueueWriteHead, and LOS_QueueRead are used to manage data addresses, you must ensure that the memory directed by the pointer obtained by calling LOS_QueueRead is not modified or released abnormally when the queue is being read. Otherwise, unpredictable results may occur.
  • LOS_QueueWrite, LOS_QueueWriteHead, and LOS_QueueRead are called to manage data addresses, which means that the actual data read or written is pointer data. Therefore, before using these APIs, ensure that the message node size is the pointer length during queue creation, to avoid waste and read failures.

Development Example

Example Description

Create a queue and two tasks. Enable task 1 to call the queue write API to send messages, and enable task 2 to receive messages by calling the queue read API.

  1. Create task 1 and task 2 by calling LOS_TaskCreate.
  2. Create a message queue by calling LOS_QueueCreate.
  3. Enable messages to be sent in task 1 by calling SendEntry.
  4. Enable messages to be received in task 2 by calling RecvEntry.
  5. Delete the queue by calling LOS_QueueDelete.

Sample Code

The sample code is as follows:

#include "los_task.h"
#include "los_queue.h"
static UINT32 g_queue;
#define BUFFER_LEN 50

VOID SendEntry(VOID)
{
    UINT32 ret = 0;
    CHAR abuf[] = "test message";
    UINT32 len = sizeof(abuf);

    ret = LOS_QueueWriteCopy(g_queue, abuf, len, 0);
    if(ret != LOS_OK) {
        printf("Failed to send the message. Error: %x\n", ret);
    }
}

VOID RecvEntry(VOID)
{
    UINT32 ret = 0;
    CHAR readBuf[BUFFER_LEN] = {0};
    UINT32 readLen = BUFFER_LEN;

    ret = LOS_QueueReadCopy(g_queue, readBuf, &readLen, 0);
    if(ret != LOS_OK) {
        printf("Failed to receive the message. Error: %x\n", ret);
    }

    printf("Message received: %s\n", readBuf);

    ret = LOS_QueueDelete(g_queue);
    if(ret != LOS_OK) {
        printf("Failed to delete the queue. Error: %x\n", ret);
    }

    printf("Queue deleted.\n");
}

UINT32 ExampleQueue(VOID)
{
    printf("Start queue example.\n");
    UINT32 ret = 0;
    UINT32 task1, task2;
    TSK_INIT_PARAM_S initParam = {0};

    initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)SendEntry;
    initParam.usTaskPrio = 9;
    initParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    initParam.pcName = "SendQueue";

    LOS_TaskLock();
    ret = LOS_TaskCreate(&task1, &initParam);
    if(ret != LOS_OK) {
        printf("Failed to create task1. Error: %x\n", ret);
        return ret;
    }

    initParam.pcName = "RecvQueue";
    initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)RecvEntry;
    initParam.usTaskPrio = 10;
    ret = LOS_TaskCreate(&task2, &initParam);
    if(ret != LOS_OK) {
        printf("Failed to create task2. Error: %x\n", ret);
        return ret;
    }

    ret = LOS_QueueCreate("queue", 5, &g_queue, 0, 50);
    if(ret != LOS_OK) {
        printf("Failed to create the queue. Error: %x\n", ret);
    }

    printf("Queue created.\n");
    LOS_TaskUnlock();
    return ret;
}

Verification

The development is successful if the return result is as follows:

Start queue example.
Queue created.
Message received: test message.
Queue deleted.