This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2340R5: How to create a non-blocking message queue?

Part Number: CC2340R5

I'm trying to create a non-blocking message queue by changing attr.mq_flags to O_NONBLOCK in BLEAppUtil_createQueue(), but it did not seem to work. The queue still blocked when it was full, and when I debugged into mq_open(), I found that msgQueueDesc->flags was 0, not the expected O_NONBLOCK.

Does the BLEAppUtil APIs support to create a non-blocking queue? If yes, how to make the queue non-blocking?

Best regards,

Shuyang

  • Hi Shuyang, 

    Thank you for reaching out. 
    To be sure I understand properly, can you please confirm you expect the queue to return an error when trying to enqueue a message while not having enough memory. Is it correct? 

    Best regards, 

  • Yes, that is what I’m expecting.

    Regards,

    Shuyang

  • Hi Shuyang,

    It seems BLEAppUtil_createQueue() forces the use of a blocking queue.This means we have not tested the code for non-blocking queues, but you can run a test.

    I would suggest you alter the BLEAppUtil_createQueue() function as following for a test:

    static bStatus_t BLEAppUtil_createQueue(void)
    {
         struct mq_attr attr;
    
         //attr.mq_flags = 0; //Blocking
         attr.mq_flags = O_NONBLOCK; //Non-blocking
         attr.mq_curmsgs = 0;
         attr.mq_maxmsg = 8;
         attr.mq_msgsize = sizeof(BLEAppUtil_appEvt_t);
    
         /* Create the message queue */
         BLEAppUtil_theardEntity.queueHandle = mq_open("BLEAppUtil_theardQueue", O_CREAT , 0, &attr);
    
         if (BLEAppUtil_theardEntity.queueHandle == (mqd_t)-1)
         {
             return FAILURE;
         }
         return SUCCESS;
    }
    

    Best regards,

  • Hi Clement,

    Thanks for the reply. I did some tests and actually setting mq_flags = O_NONBLOCK will not work, because mq_flags is ignored when O_CREAT is true. This is from POSIX document:

    Following the document, I used O_NONBLOCK along with O_CREAT as the oflags parameter into mq_open(), from my test this works. When the message queue is full and user tries to put another message into the queue, it returns EAGAIN instead of blocking the call.

    Maybe the comment in BLEAppUtil_createQueue should be modified to avoid misguiding customers to believe changing mq_flags will create a non-blocking queue?

    attr.mq_flags = 0; //Blocking

    Best regards,

    Shuyang

  • Hi Shuyang,

    Great findings, thank you for sharing!

    I have reported your suggestions.

    Thank you for helping us improving our product.

    Kind regards,