更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
Message queue

结构体

struct  mq_attr
 

类型定义

typedef UINTPTR mqd_t
 

函数

mqd_t mq_open (const char *mqName, int openFlag,...)
 
int mq_close (mqd_t personal)
 
int mq_unlink (const char *mqName)
 
int mq_send (mqd_t personal, const char *msg, size_t msgLen, unsigned int msgPrio)
 
ssize_t mq_receive (mqd_t personal, char *msg, size_t msgLen, unsigned int *msgPrio)
 
int mq_getsetattr (mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr *MqOldAttr)
 
int mq_timedsend (mqd_t personal, const char *msg, size_t msgLen, unsigned int msgPrio, const struct timespec *absTimeout)
 
ssize_t mq_timedreceive (mqd_t personal, char *msg, size_t msgLen, unsigned int *msgPrio, const struct timespec *absTimeout)
 

详细描述

类型定义说明

◆ mqd_t

typedef UINTPTR mqd_t

Handle type of a message queue

在文件 mqueue.h127 行定义.

函数说明

◆ mq_close()

int mq_close ( mqd_t  personal)
Description:
This API is used to close a message queue that has a specified descriptor.
注意
  • If the message queue is empty, it will be reclaimed, which is similar to when mq_unlink is called.
参数
personal[IN] Message queue descriptor.
返回值
0The message queue is successfully closed.
-1The message queue fails to be closed, with either of the following error codes in errno.
Errors
  • EBADF: Invalid message queue descriptor.
  • EAGAIN: Failed to delete the message queue.
  • EFAULT: Failed to free the message queue.
  • EINVAL: Invalid parameter.
Dependency:
参见
mq_open

在文件 mqueue.c550 行定义.

551{
552 INT32 ret = -1;
553 struct mqpersonal *privateMqPersonal = NULL;
554
556
557 /* Get the personal sysFd and reset personal fd -1 */
558 privateMqPersonal = MqGetPrivDataBuff(personal);
559 if (privateMqPersonal == NULL) {
560 goto OUT_UNLOCK;
561 }
562
563 if (privateMqPersonal->mq_status != MQ_USE_MAGIC) {
564 errno = EBADF;
565 goto OUT_UNLOCK;
566 }
567
568 if (!MqTryClose(privateMqPersonal)) {
569 ret = 0;
570 goto OUT_UNLOCK;
571 }
572
573 ret = DoMqueueClose(privateMqPersonal);
574 if (ret < 0) {
575 goto OUT_UNLOCK;
576 }
577 MqFreeSysFd(personal);
578
579OUT_UNLOCK:
581 return ret;
582}
signed int INT32
Definition: los_typedef.h:60
STATIC pthread_mutex_t g_mqueueMutex
Definition: mqueue.c:54
STATIC VOID MqFreeSysFd(mqd_t personal)
Definition: mqueue.c:328
STATIC struct mqpersonal * MqGetPrivDataBuff(mqd_t personal)
Definition: mqueue.c:290
STATIC INT32 DoMqueueClose(struct mqpersonal *privateMqPersonal)
Definition: mqueue.c:251
STATIC INT32 MqTryClose(struct mqpersonal *privateMqPersonal)
Definition: mqueue.c:361
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
UINT32 mq_status
Definition: mqueue.h:108
函数调用图:
这是这个函数的调用关系图:

◆ mq_getsetattr()

int mq_getsetattr ( mqd_t  personal,
const struct mq_attr mqSetAttr,
struct mq_attr MqOldAttr 
)
Description:
This API is used to obtain or modify attributes of the message queue that has a specified descriptor.
注意
  • The mq_maxmsg, mq_msgsize, and mq_curmsgs attributes are not modified in the message queue attribute setting.
参数
personal[IN] Message queue descriptor.
mqSetAttr[IN] New attribute of the message queue.
MqOldAttr[OUT] Old attribute of the message queue.
返回值
0The message queue attributes are successfully set or get.
-1The message queue attributes fail to be set or get, with either of the following error codes in the errno.
Errors
  • EBADF: Invalid message queue.
  • EINVAL: Invalid parameter.
Dependency:
参见
sys_mq_getsetattr

在文件 mqueue.c648 行定义.

649{
650 if (new == NULL) {
651 return OsMqGetAttr(mqd, old);
652 }
653 return OsMqSetAttr(mqd, new, old);
654}
int OsMqSetAttr(mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr *mqOldAttr)
Definition: mqueue.c:615
int OsMqGetAttr(mqd_t personal, struct mq_attr *mqAttr)
Definition: mqueue.c:584
函数调用图:
这是这个函数的调用关系图:

◆ mq_open()

mqd_t mq_open ( const char *  mqName,
int  openFlag,
  ... 
)
Description:
This API is used to open an existed message queue that has a specified name or create a new message queue.
注意
  • A message queue does not restrict the read and write permissions.
  • The length of mqueue name must less than 256.
  • This operation and closed mqueue scheduling must be used in coordination to release the resource.
  • The parameter "mode" is not supported.
  • The "mq_curmsgs" member of the mq_attr structure is not supported.
参数
mqName[IN] Message queue name.
openFlag[IN] Permission attributes of the message queue. The value range is [O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NONBLOCK].
mode[IN] Message queue mode (variadic argument). When oflag is O_CREAT, it requires two additional arguments: mode, which shall be of type mode_t, and attr, which shall be a pointer to an mq_attr structure.
attr[IN] Message queue attribute (variadic argument).
返回值
mqd_tThe message queue is successfully opened or created.
(mqd_t)-1The message queue fails to be opened or created, with any of the following error codes in errno.
Errors
  • ENOENT: O_CREAT flag is not set for oflag, and the message queue specified by name does not exist.
  • EEXIST: Both O_CREAT and O_EXCL are set for oflag, but the message queue specified by name already exists.
  • EINVAL: invalid parameter.
  • ENFILE: The number of opened message queues exceeds the maximum limit.
  • ENOSPC: insufficient memory.
  • ENAMETOOLONG: The message queue name specified by name is too long.
Dependency:
参见
mq_close

在文件 mqueue.c486 行定义.

487{
488 struct mqarray *mqueueCB = NULL;
489 struct mqpersonal *privateMqPersonal = (struct mqpersonal *)-1;
490 struct mq_attr *attr = NULL;
491 struct mq_attr defaultAttr = { 0, MQ_MAX_MSG_NUM, MQ_MAX_MSG_LEN, 0 };
492 va_list ap;
493 int sysFd;
494 mqd_t mqFd = -1;
495 unsigned int mode = 0;
496
497 if (MqNameCheck(mqName) == -1) {
498 return (mqd_t)-1;
499 }
500
502 mqueueCB = GetMqueueCBByName(mqName);
503 if ((UINT32)openFlag & (UINT32)O_CREAT) {
504 if (mqueueCB != NULL) {
505 if (((UINT32)openFlag & (UINT32)O_EXCL)) {
506 errno = EEXIST;
507 goto OUT;
508 }
509 privateMqPersonal = DoMqueueOpen(mqueueCB, openFlag);
510 } else {
511 va_start(ap, openFlag);
512 mode = va_arg(ap, unsigned int);
513 attr = va_arg(ap, struct mq_attr *);
514 va_end(ap);
515
516 if (GetMqueueAttr(&defaultAttr, attr)) {
517 goto OUT;
518 }
519 privateMqPersonal = DoMqueueCreate(&defaultAttr, mqName, openFlag, mode);
520 }
521 /* Set mode data bit ,just for the first node */
522 if (MqueueModeAnalysisSet(privateMqPersonal)) {
523 if ((INT32)(UINTPTR)privateMqPersonal > 0) {
524 (VOID)DoMqueueClose(privateMqPersonal);
525 }
526 goto OUT;
527 }
528 } else {
529 if (GetPermissionOfVisitor(mqueueCB)) {
530 goto OUT;
531 }
532 privateMqPersonal = DoMqueueOpen(mqueueCB, openFlag);
533 }
534
535 if ((INT32)(UINTPTR)privateMqPersonal > 0) {
536 /* alloc sysFd */
537 sysFd = MqAllocSysFd(MAX_MQ_FD, privateMqPersonal);
538 if (sysFd == -1) {
539 /* there are no more mq sysFd to use, close the personal */
540 (VOID)DoMqueueClose(privateMqPersonal);
541 errno = ENFILE;
542 }
543 mqFd = (mqd_t)sysFd;
544 }
545OUT:
547 return mqFd;
548}
UINTPTR mqd_t
Definition: mqueue.h:127
unsigned long UINTPTR
Definition: los_typedef.h:68
unsigned int UINT32
Definition: los_typedef.h:57
STATIC struct mqpersonal * DoMqueueCreate(const struct mq_attr *attr, const CHAR *mqName, INT32 openFlag, UINT32 mode)
Definition: mqueue.c:163
STATIC INT32 GetPermissionOfVisitor(struct mqarray *mqueueCB)
Definition: mqueue.c:425
STATIC struct mqpersonal * DoMqueueOpen(struct mqarray *mqueueCB, INT32 openFlag)
Definition: mqueue.c:221
STATIC INT32 MqAllocSysFd(int maxfdp, struct mqpersonal *privateMqPersonal)
Definition: mqueue.c:311
STATIC INLINE INT32 MqNameCheck(const CHAR *mqName)
Definition: mqueue.c:58
STATIC INT32 MqueueModeAnalysisSet(struct mqpersonal *privateMqPersonal)
Definition: mqueue.c:378
STATIC INT32 GetMqueueAttr(struct mq_attr *defaultAttr, struct mq_attr *attr)
Definition: mqueue.c:470
STATIC INLINE struct mqarray * GetMqueueCBByName(const CHAR *name)
Definition: mqueue.c:93
Definition: mqueue.h:90
函数调用图:
这是这个函数的调用关系图:

◆ mq_receive()

ssize_t mq_receive ( mqd_t  personal,
char *  msg,
size_t  msgLen,
unsigned int msgPrio 
)
Description:
This API is used to remove the oldest message from the message queue that has a specified descriptor, and puts it in the buffer pointed to by msg_ptr.
注意
  • Priority-based message processing is not supported.
  • The msg_len should be same to the length of string which msg_ptr point to.
参数
personal[IN] Message queue descriptor.
msg[IN] Pointer to the message content to be received.
msgLen[IN] Length of the message to be received.
msgPrio[OUT] Priority of the message to be received because priority-based message processing is not supported, this parameter is useless).
返回值
0The message is successfully received.
-1The message fails to be received, with any of the following error codes in the errno.
Errors
  • EINTR: An interrupt is in progress while the message is being received.
  • EBADF: The message queue is invalid or not readable.
  • EAGAIN: The message queue is empty.
  • EINVAL: invalid parameter.
  • EMSGSIZE: The message to be received is too long.
  • ETIMEDOUT: The operaton times out.
Dependency:
参见
mq_send

在文件 mqueue.c863 行定义.

864{
865 return mq_timedreceive(personal, msg_ptr, msg_len, msg_prio, NULL);
866}
ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *msgPrio, const struct timespec *absTimeout)
Definition: mqueue.c:801
函数调用图:

◆ mq_send()

int mq_send ( mqd_t  personal,
const char *  msg,
size_t  msgLen,
unsigned int  msgPrio 
)
Description:
This API is used to put a message with specified message content and length into a message queue that has a specified descriptor.
注意
  • Priority-based message processing is not supported.
  • The msg_len should be same to the length of string which msg_ptr point to.
参数
personal[IN] Message queue descriptor.
msg[IN] Pointer to the message content to be sent.
msgLen[IN] Length of the message to be sent.
msgPrio[IN] Priority of the message to be sent (the value of this parameter must be 0 because priority-based message sending is not supported. If the value is not 0, this API will cease to work.)
返回值
0The message is successfully sent.
-1The message fails to be sent, with any of the following error codes in errno.
Errors
  • EINTR: An interrupt is in progress while the message is being sent.
  • EBADF: The message queue is invalid or not writable.
  • EAGAIN: The message queue is full.
  • EINVAL: Invalid parameter.
  • ENOSPC: Insufficient memory.
  • EMSGSIZE: The message to be sent is too long.
  • EOPNOTSUPP: The operation is not supported.
  • ETIMEDOUT: The operation times out.
Dependency:
参见
mq_receive

在文件 mqueue.c858 行定义.

859{
860 return mq_timedsend(personal, msg_ptr, msg_len, msg_prio, NULL);
861}
int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, unsigned int msgPrio, const struct timespec *absTimeout)
Definition: mqueue.c:759
函数调用图:

◆ mq_timedreceive()

ssize_t mq_timedreceive ( mqd_t  personal,
char *  msg,
size_t  msgLen,
unsigned int msgPrio,
const struct timespec *  absTimeout 
)
Description:
This API is used to obtain a message with specified message content and length from a message queue message that has a specified descriptor.
注意
  • Priority-based message processing is not supported.
  • The expiry time must be later than the current time.
  • The wait time is a relative time.
  • The msg_len should be same to the length of string which msg_ptr point to.
参数
personal[IN] Message queue descriptor.
msg[IN] Pointer to the message content to be received.
msgLen[IN] Length of the message to be received.
msgPrio[OUT] Priority of the message to be received (because priority-based message processing is not supported, this parameter is useless ).
absTimeout[IN] Scheduled time at which the messagewill be received. If the value is 0, the message is an instant message.
返回值
0The message is successfully received.
-1The message fails to be received, with any of the following error codes in errno.
Errors
  • EINTR: An interrupt is in progress while the message is being received.
  • EBADF: The message queue is invalid or not readable.
  • EAGAIN: The message queue is empty.
  • EINVAL: invalid parameter.
  • EMSGSIZE: The message to be received is too long.
  • ETIMEDOUT: The operation times out.
Dependency:
参见
mq_send

在文件 mqueue.c801 行定义.

803{
804 UINT32 mqueueID, err;
805 UINT32 receiveLen;
806 UINT64 absTicks;
807 struct mqarray *mqueueCB = NULL;
808 struct mqpersonal *privateMqPersonal = NULL;
809
810 if (!MqParamCheck(personal, msg, msgLen)) {
811 goto ERROUT;
812 }
813
814 if (msgPrio != NULL) {
815 *msgPrio = 0;
816 }
817
819 privateMqPersonal = MqGetPrivDataBuff(personal);
820 if (privateMqPersonal == NULL || privateMqPersonal->mq_status != MQ_USE_MAGIC) {
821 errno = EBADF;
822 goto ERROUT_UNLOCK;
823 }
824
825 mqueueCB = privateMqPersonal->mq_posixdes;
826 if (msgLen < (size_t)(mqueueCB->mqcb->queueSize - sizeof(UINT32))) {
827 errno = EMSGSIZE;
828 goto ERROUT_UNLOCK;
829 }
830
831 if (((UINT32)privateMqPersonal->mq_flags & (UINT32)O_WRONLY) == (UINT32)O_WRONLY) {
832 errno = EBADF;
833 goto ERROUT_UNLOCK;
834 }
835
836 if (ConvertTimeout(privateMqPersonal->mq_flags, absTimeout, &absTicks) == -1) {
837 goto ERROUT_UNLOCK;
838 }
839
840 receiveLen = msgLen;
841 mqueueID = mqueueCB->mq_id;
843
844 err = LOS_QueueReadCopy(mqueueID, (VOID *)msg, &receiveLen, (UINT32)absTicks);
845 if (map_errno(err) == ENOERR) {
846 return (ssize_t)receiveLen;
847 } else {
848 goto ERROUT;
849 }
850
851ERROUT_UNLOCK:
853ERROUT:
854 return -1;
855}
LITE_OS_SEC_TEXT UINT32 LOS_QueueReadCopy(UINT32 queueID, VOID *bufferAddr, UINT32 *bufferSize, UINT32 timeout)
接口函数定时读取消息队列
Definition: los_queue.c:377
long unsigned int UINT64
Definition: los_typedef.h:66
INT64 ssize_t
Definition: los_typedef.h:79
int map_errno(UINT32 err)
Definition: map_error.c:38
STATIC INT32 ConvertTimeout(long flags, const struct timespec *absTimeout, UINT64 *ticks)
Definition: mqueue.c:686
STATIC INLINE BOOL MqParamCheck(mqd_t personal, const char *msg, size_t msgLen)
Definition: mqueue.c:707
UINT16 queueSize
Definition: los_queue_pri.h:92
LosQueueCB * mqcb
Definition: mqueue.h:99
UINT32 mq_id
Definition: mqueue.h:91
struct mqarray * mq_posixdes
Definition: mqueue.h:104
int mq_flags
Definition: mqueue.h:106
函数调用图:
这是这个函数的调用关系图:

◆ mq_timedsend()

int mq_timedsend ( mqd_t  personal,
const char *  msg,
size_t  msgLen,
unsigned int  msgPrio,
const struct timespec *  absTimeout 
)
Description:
This API is used to put a message with specified message content and length into a message queue that has a descriptor at a scheduled time.
注意
  • Priority-based message processing is not supported.
  • The expiry time must be later than the current time.
  • The wait time is a relative time.
  • The msg_len should be same to the length of string which msg_ptr point to.
参数
mqdes[IN] Message queue descriptor.
msg[IN] Pointer to the message content to be sent.
msgLen[IN] Length of the message to be sent.
msgPrio[IN] Priority of the message to be sent (the value of this parameter must be 0 because priority-based message processing is not supported).
absTimeout[IN] Scheduled time at which the message will be sent. If the value is 0, the message is an instant message.
返回值
0The message is successfully sent.
-1The message fails to be sent, with any of the following error codes in errno.
Errors
  • EINTR: An interrupt is in progress while the message is being sent.
  • EBADF: The message queue is invalid or not writable.
  • EAGAIN: The message queue is full.
  • EINVAL: Invalid parameter.
  • EMSGSIZE: The message to be sent is too long.
  • EOPNOTSUPP: The operation is not supported.
  • ETIMEDOUT: The operation times out.
Dependency:
参见
mq_receive

在文件 mqueue.c759 行定义.

761{
762 UINT32 mqueueID, err;
763 UINT64 absTicks;
764 struct mqarray *mqueueCB = NULL;
765 struct mqpersonal *privateMqPersonal = NULL;
766
767 OS_MQ_GOTO_ERROUT_IF(!MqParamCheck(personal, msg, msgLen), errno);
768 OS_MQ_GOTO_ERROUT_IF(msgPrio > (MQ_PRIO_MAX - 1), EINVAL);
769
771 privateMqPersonal = MqGetPrivDataBuff(personal);
772
773 OS_MQ_GOTO_ERROUT_UNLOCK_IF(privateMqPersonal == NULL || privateMqPersonal->mq_status != MQ_USE_MAGIC, EBADF);
774
775 mqueueCB = privateMqPersonal->mq_posixdes;
776 OS_MQ_GOTO_ERROUT_UNLOCK_IF(msgLen > (size_t)(mqueueCB->mqcb->queueSize - sizeof(UINT32)), EMSGSIZE);
777
778 OS_MQ_GOTO_ERROUT_UNLOCK_IF((((UINT32)privateMqPersonal->mq_flags & (UINT32)O_WRONLY) != (UINT32)O_WRONLY) &&
779 (((UINT32)privateMqPersonal->mq_flags & (UINT32)O_RDWR) != (UINT32)O_RDWR),
780 EBADF);
781
782 OS_MQ_GOTO_ERROUT_UNLOCK_IF(ConvertTimeout(privateMqPersonal->mq_flags, absTimeout, &absTicks) == -1, errno);
783 mqueueID = mqueueCB->mq_id;
785
786 if (LOS_ListEmpty(&mqueueCB->mqcb->readWriteList[OS_QUEUE_READ])) {
787 MqSendNotify(mqueueCB);
788 }
789
790 err = LOS_QueueWriteCopy(mqueueID, (VOID *)msg, (UINT32)msgLen, (UINT32)absTicks);
791 if (map_errno(err) != ENOERR) {
792 goto ERROUT;
793 }
794 return 0;
795ERROUT_UNLOCK:
797ERROUT:
798 return -1;
799}
LITE_OS_SEC_ALW_INLINE STATIC INLINE BOOL LOS_ListEmpty(LOS_DL_LIST *list)
Identify whether a specified doubly linked list is empty. | 判断链表是否为空
Definition: los_list.h:321
LITE_OS_SEC_TEXT UINT32 LOS_QueueWriteCopy(UINT32 queueID, VOID *bufferAddr, UINT32 bufferSize, UINT32 timeout)
接口函数 从队列尾部开始写
Definition: los_queue.c:411
@ OS_QUEUE_READ
读队列
Definition: los_queue_pri.h:63
static void MqSendNotify(struct mqarray *mqueueCB)
Definition: mqueue.c:724
LOS_DL_LIST readWriteList[OS_QUEUE_N_RW]
Definition: los_queue_pri.h:98
函数调用图:
这是这个函数的调用关系图:

◆ mq_unlink()

int mq_unlink ( const char *  mqName)
Description:
This API is used to remove a message queue that has a specified name.
注意
  • If the message queue is empty, it will be reclaimed, which is similar to when mq_close is called.
  • The length of mqueue name must less than 256.
参数
mqName[IN] Message queue name.
返回值
0The message queue is successfully removed.
-1The message queue fails to be removed, with any of the following error codes in errno.
Errors
  • ENOENT: The message queue specified by name does not exist.
  • EAGAIN: Failed to delete the message queue.
  • EBUSY: The message queue to be removed is being used.
  • EINVAL: Invalid parameter.
  • ENAMETOOLONG: The name of mqueue is too long.
Dependency:
参见
mq_close

在文件 mqueue.c656 行定义.

657{
658 INT32 ret = 0;
659 struct mqarray *mqueueCB = NULL;
660
661 if (MqNameCheck(mqName) == -1) {
662 return -1;
663 }
664
666 mqueueCB = GetMqueueCBByName(mqName);
667 if (mqueueCB == NULL) {
668 errno = ENOENT;
669 goto ERROUT_UNLOCK;
670 }
671
672 if (mqueueCB->mq_personal != NULL) {
673 mqueueCB->unlinkflag = TRUE;
674 } else if (mqueueCB->unlink_ref == 0) {
675 ret = DoMqueueDelete(mqueueCB);
676 }
677
679 return ret;
680
681ERROUT_UNLOCK:
683 return -1;
684}
STATIC INT32 DoMqueueDelete(struct mqarray *mqueueCB)
Definition: mqueue.c:110
UINT32 unlink_ref
Definition: mqueue.h:94
UINT32 unlinkflag
Definition: mqueue.h:92
struct mqpersonal * mq_personal
Definition: mqueue.h:100
函数调用图:
这是这个函数的调用关系图: