• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 _LOS_QUEUE_PRI_H
33 #define _LOS_QUEUE_PRI_H
34 
35 #include "los_queue.h"
36 
37 #ifdef __cplusplus
38 #if __cplusplus
39 extern "C" {
40 #endif /* __cplusplus */
41 #endif /* __cplusplus */
42 
43 typedef enum {
44     OS_QUEUE_READ = 0,
45     OS_QUEUE_WRITE = 1,
46     OS_QUEUE_N_RW = 2
47 } QueueReadWrite;
48 
49 typedef enum {
50     OS_QUEUE_HEAD = 0,
51     OS_QUEUE_TAIL = 1
52 } QueueHeadTail;
53 
54 #define OS_QUEUE_OPERATE_TYPE(ReadOrWrite, HeadOrTail) (((UINT32)(HeadOrTail) << 1) | (ReadOrWrite))
55 #define OS_QUEUE_READ_WRITE_GET(type) ((type) & 0x01U)
56 #define OS_QUEUE_READ_HEAD     (OS_QUEUE_READ | (OS_QUEUE_HEAD << 1))
57 #define OS_QUEUE_READ_TAIL     (OS_QUEUE_READ | (OS_QUEUE_TAIL << 1))
58 #define OS_QUEUE_WRITE_HEAD    (OS_QUEUE_WRITE | (OS_QUEUE_HEAD << 1))
59 #define OS_QUEUE_WRITE_TAIL    (OS_QUEUE_WRITE | (OS_QUEUE_TAIL << 1))
60 #define OS_QUEUE_OPERATE_GET(type) ((type) & 0x03U)
61 #define OS_QUEUE_IS_READ(type) (OS_QUEUE_READ_WRITE_GET(type) == OS_QUEUE_READ)
62 #define OS_QUEUE_IS_WRITE(type) (OS_QUEUE_READ_WRITE_GET(type) == OS_QUEUE_WRITE)
63 
64 /**
65  * @ingroup los_queue
66  * Queue information block structure
67  */
68 typedef struct TagQueueCB {
69     UINT8 *queueHandle; /**< Pointer to a queue handle */
70     UINT16 queueState; /**< Queue state */
71     UINT16 queueLen; /**< Queue length */
72     UINT16 queueSize; /**< Node size */
73     UINT32 queueID; /**< queueID */
74     UINT16 queueHead; /**< Node head */
75     UINT16 queueTail; /**< Node tail */
76     UINT16 readWriteableCnt[OS_QUEUE_N_RW]; /**< Count of readable or writable resources, 0:readable, 1:writable */
77     LOS_DL_LIST readWriteList[OS_QUEUE_N_RW]; /**< the linked list to be read or written, 0:readlist, 1:writelist */
78     LOS_DL_LIST memList; /**< Pointer to the memory linked list */
79 } LosQueueCB;
80 
81 /* queue state */
82 /**
83  *  @ingroup los_queue
84  *  Message queue state: not in use.
85  */
86 #define OS_QUEUE_UNUSED        0
87 
88 /**
89  *  @ingroup los_queue
90  *  Message queue state: used.
91  */
92 #define OS_QUEUE_INUSED        1
93 
94 /**
95  *  @ingroup los_queue
96  *  Not in use.
97  */
98 #define OS_QUEUE_WAIT_FOR_POOL 1
99 
100 /**
101  *  @ingroup los_queue
102  *  Normal message queue.
103  */
104 #define OS_QUEUE_NORMAL        0
105 
106 /**
107  *  @ingroup los_queue
108  *  Queue information control block
109  */
110 extern LosQueueCB *g_allQueue;
111 #ifndef LOSCFG_IPC_CONTAINER
112 #define IPC_ALL_QUEUE g_allQueue
113 #endif
114 
115 /**
116  * @ingroup los_queue
117  * COUNT | INDEX  split bit
118  */
119 #define QUEUE_SPLIT_BIT        16
120 /**
121  * @ingroup los_queue
122  * Set the queue id
123  */
124 #define SET_QUEUE_ID(count, queueID)    (((count) << QUEUE_SPLIT_BIT) | (queueID))
125 
126 /**
127  * @ingroup los_queue
128  * get the queue index
129  */
130 #define GET_QUEUE_INDEX(queueID)        ((queueID) & ((1U << QUEUE_SPLIT_BIT) - 1))
131 
132 /**
133  * @ingroup los_queue
134  * get the queue count
135  */
136 #define GET_QUEUE_COUNT(queueID)        ((queueID) >> QUEUE_SPLIT_BIT)
137 
138 /**
139  * @ingroup los_queue
140  * Obtain a handle of the queue that has a specified ID.
141  *
142  */
143 #define GET_QUEUE_HANDLE(queueID)       (((LosQueueCB *)IPC_ALL_QUEUE) + GET_QUEUE_INDEX(queueID))
144 
145 /**
146  * @ingroup los_queue
147  * Obtain the head node in a queue doubly linked list.
148  */
149 #define GET_QUEUE_LIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosQueueCB, readWriteList[OS_QUEUE_WRITE])
150 
151 /**
152  * @ingroup los_queue
153  * @brief Alloc a stationary memory for a mail.
154  *
155  * @par Description:
156  * This API is used to alloc a stationary memory for a mail according to queueID.
157  * @attention
158  * <ul>
159  * <li>Do not alloc memory in unblocking modes such as interrupt.</li>
160  * <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
161  * <li>The argument timeout is a relative time.</li>
162  * </ul>
163  *
164  * @param queueID        [IN]        Queue ID. The value range is [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
165  * @param mailPool        [IN]        The memory poll that stores the mail.
166  * @param timeout        [IN]        Expiry time. The value range is [0,LOS_WAIT_FOREVER].
167  *
168  * @retval   #NULL                     The memory allocation is failed.
169  * @retval   #pMem                     The address of alloc memory.
170  * @par Dependency:
171  * <ul><li>los_queue_pri.h: the header file that contains the API declaration.</li></ul>
172  * @see OsQueueMailFree
173  */
174 extern VOID *OsQueueMailAlloc(UINT32 queueID, VOID *mailPool, UINT32 timeout);
175 
176 /**
177  * @ingroup los_queue
178  * @brief Free a stationary memory of a mail.
179  *
180  * @par Description:
181  * This API is used to free a stationary memory for a mail according to queueID.
182  * @attention
183  * <ul>
184  * <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
185  * </ul>
186  *
187  * @param queueID        [IN]        Queue ID. The value range is [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
188  * @param mailPool        [IN]        The mail memory poll address.
189  * @param mailMem         [IN]        The mail memory block address.
190  *
191  * @retval   #LOS_OK                                 0x00000000: The memory free successfully.
192  * @retval   #OS_ERRNO_QUEUE_MAIL_HANDLE_INVALID     0x02000619: The handle of the queue passed-in when the memory
193  *                                                               for the queue is being freed is invalid.
194  * @retval   #OS_ERRNO_QUEUE_MAIL_PTR_INVALID        0x0200061a: The pointer to the memory to be freed is null.
195  * @retval   #OS_ERRNO_QUEUE_MAIL_FREE_ERROR         0x0200061b: The memory for the queue fails to be freed.
196  * @par Dependency:
197  * <ul><li>los_queue_pri.h: the header file that contains the API declaration.</li></ul>
198  * @see OsQueueMailAlloc
199  */
200 extern UINT32 OsQueueMailFree(UINT32 queueID, VOID *mailPool, VOID *mailMem);
201 
202 extern LosQueueCB *OsAllQueueCBInit(LOS_DL_LIST *freeQueueList);
203 
204 extern UINT32 OsQueueInit(VOID);
205 
206 #ifdef __cplusplus
207 #if __cplusplus
208 }
209 #endif /* __cplusplus */
210 #endif /* __cplusplus */
211 
212 #endif /* _LOS_QUEUE_PRI_H */
213