xref: /kernel/liteos_m/kal/posix/src/mqueue_impl.h (revision 3d8536b4)
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 *    conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 *    of conditions and the following disclaimer in the documentation and/or other materials
13 *    provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 *    to endorse or promote products derived from this software without specific prior written
17 *    permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _MQUEUE_IMPL_H
33#define _MQUEUE_IMPL_H
34
35#define _GNU_SOURCE
36
37#include <mqueue.h>
38#include <errno.h>
39#include <pthread.h>
40#include <unistd.h>
41#include <securec.h>
42#include <fcntl.h>
43#include <limits.h>
44#include <sys/select.h>
45#include "los_queue.h"
46#include "los_memory.h"
47#include "los_task.h"
48#include "los_compiler.h"
49
50#define OS_QUEUE_OPERATE_TYPE(ReadOrWrite, HeadOrTail, PointOrNot)  \
51                (((UINT32)(PointOrNot) << 2) | ((UINT32)(HeadOrTail) << 1) | (ReadOrWrite))
52#define OS_QUEUE_READ_WRITE_GET(type) ((type) & (0x01))
53#define OS_QUEUE_READ_HEAD     (OS_QUEUE_READ | (OS_QUEUE_HEAD << 1))
54#define OS_QUEUE_READ_TAIL     (OS_QUEUE_READ | (OS_QUEUE_TAIL << 1))
55#define OS_QUEUE_WRITE_HEAD    (OS_QUEUE_WRITE | (OS_QUEUE_HEAD << 1))
56#define OS_QUEUE_WRITE_TAIL    (OS_QUEUE_WRITE | (OS_QUEUE_TAIL << 1))
57#define OS_QUEUE_OPERATE_GET(type) ((type) & (0x03))
58#define OS_QUEUE_IS_POINT(type)    ((type) & (0x04))
59#define OS_QUEUE_IS_READ(type)     (OS_QUEUE_READ_WRITE_GET(type) == OS_QUEUE_READ)
60#define OS_QUEUE_IS_WRITE(type)    (OS_QUEUE_READ_WRITE_GET(type) == OS_QUEUE_WRITE)
61#define OS_READWRITE_LEN           2
62
63/**
64 * @ingroup mqueue
65 * Maximum number of messages in a message queue
66 */
67#define MQ_MAX_MSG_NUM    16
68
69/**
70 * @ingroup mqueue
71 * Maximum size of a single message in a message queue
72 */
73#define MQ_MAX_MSG_LEN    64
74
75#define ENOERR 0
76
77/* CONSTANTS */
78
79#define MQ_USE_MAGIC  0x89abcdef
80#define MQ_PRIO_MAX 1
81
82#ifndef FNONBLOCK
83#define FNONBLOCK   O_NONBLOCK
84#endif
85
86#define QUEUE_SPLIT_BIT        16
87
88#define SET_QUEUE_ID(count, queueID)    (((count) << QUEUE_SPLIT_BIT) | (queueID))
89
90/**
91 * @ingroup los_queue
92 * get the queue index
93 */
94#define GET_QUEUE_INDEX(queueID)        ((queueID) & ((1U << QUEUE_SPLIT_BIT) - 1))
95
96/**
97 * @ingroup los_queue
98 * get the queue count
99 */
100#define GET_QUEUE_COUNT(queueID)        ((queueID) >> QUEUE_SPLIT_BIT)
101
102
103/**
104 * @ingroup los_queue
105 * Obtain the head node in a queue doubly linked list.
106 */
107#define GET_QUEUE_LIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosQueueCB, readWriteList[OS_QUEUE_WRITE])
108
109#define STATIC static
110#define INLINE inline
111
112typedef union send_receive_t {
113    unsigned oth : 3;
114    unsigned grp : 6;
115    unsigned usr : 9;
116    short data;
117} mode_s;
118
119/* TYPE DEFINITIONS */
120struct mqarray {
121    UINT32 mq_id : 31;
122    UINT32 unlinkflag : 1;
123    char *mq_name;
124    UINT32 unlink_ref;
125    mode_s mode_data; /* mode data of mqueue */
126    uid_t euid; /* euid of mqueue */
127    gid_t egid; /* egid of mqueue */
128    fd_set mq_fdset; /* mqueue sysFd bit map */
129    LosQueueCB *mqcb;
130    struct mqpersonal *mq_personal;
131};
132
133struct mqpersonal {
134    struct mqarray *mq_posixdes;
135    struct mqpersonal *mq_next;
136    int mq_flags;
137    int mq_mode;  /* Mode of mqueue */
138    UINT32 mq_status;
139    UINT32 mq_refcount;
140};
141
142#endif
143