• 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 {
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 
112 /**
113  * @ingroup los_queue
114  * COUNT | INDEX  split bit
115  */
116 #define QUEUE_SPLIT_BIT        16
117 /**
118  * @ingroup los_queue
119  * Set the queue id
120  */
121 #define SET_QUEUE_ID(count, queueID)    (((count) << QUEUE_SPLIT_BIT) | (queueID))
122 
123 /**
124  * @ingroup los_queue
125  * get the queue index
126  */
127 #define GET_QUEUE_INDEX(queueID)        ((queueID) & ((1U << QUEUE_SPLIT_BIT) - 1))
128 
129 /**
130  * @ingroup los_queue
131  * get the queue count
132  */
133 #define GET_QUEUE_COUNT(queueID)        ((queueID) >> QUEUE_SPLIT_BIT)
134 
135 /**
136  * @ingroup los_queue
137  * Obtain a handle of the queue that has a specified ID.
138  *
139  */
140 #define GET_QUEUE_HANDLE(queueID)       (((LosQueueCB *)g_allQueue) + GET_QUEUE_INDEX(queueID))
141 
142 /**
143  * @ingroup los_queue
144  * Obtain the head node in a queue doubly linked list.
145  */
146 #define GET_QUEUE_LIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosQueueCB, readWriteList[OS_QUEUE_WRITE])
147 
148 /**
149  * @ingroup los_queue
150  * @brief Alloc a stationary memory for a mail.
151  *
152  * @par Description:
153  * This API is used to alloc a stationary memory for a mail according to queueID.
154  * @attention
155  * <ul>
156  * <li>Do not alloc memory in unblocking modes such as interrupt.</li>
157  * <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
158  * <li>The argument timeout is a relative time.</li>
159  * </ul>
160  *
161  * @param queueID        [IN]        Queue ID. The value range is [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
162  * @param mailPool        [IN]        The memory poll that stores the mail.
163  * @param timeout        [IN]        Expiry time. The value range is [0,LOS_WAIT_FOREVER].
164  *
165  * @retval   #NULL                     The memory allocation is failed.
166  * @retval   #pMem                     The address of alloc memory.
167  * @par Dependency:
168  * <ul><li>los_queue_pri.h: the header file that contains the API declaration.</li></ul>
169  * @see OsQueueMailFree
170  */
171 extern VOID *OsQueueMailAlloc(UINT32 queueID, VOID *mailPool, UINT32 timeout);
172 
173 /**
174  * @ingroup los_queue
175  * @brief Free a stationary memory of a mail.
176  *
177  * @par Description:
178  * This API is used to free a stationary memory for a mail according to queueID.
179  * @attention
180  * <ul>
181  * <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
182  * </ul>
183  *
184  * @param queueID        [IN]        Queue ID. The value range is [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
185  * @param mailPool        [IN]        The mail memory poll address.
186  * @param mailMem         [IN]        The mail memory block address.
187  *
188  * @retval   #LOS_OK                                 0x00000000: The memory free successfully.
189  * @retval   #OS_ERRNO_QUEUE_MAIL_HANDLE_INVALID     0x02000619: The handle of the queue passed-in when the memory
190  *                                                               for the queue is being freed is invalid.
191  * @retval   #OS_ERRNO_QUEUE_MAIL_PTR_INVALID        0x0200061a: The pointer to the memory to be freed is null.
192  * @retval   #OS_ERRNO_QUEUE_MAIL_FREE_ERROR         0x0200061b: The memory for the queue fails to be freed.
193  * @par Dependency:
194  * <ul><li>los_queue_pri.h: the header file that contains the API declaration.</li></ul>
195  * @see OsQueueMailAlloc
196  */
197 extern UINT32 OsQueueMailFree(UINT32 queueID, VOID *mailPool, VOID *mailMem);
198 
199 extern UINT32 OsQueueInit(VOID);
200 
201 #ifdef __cplusplus
202 #if __cplusplus
203 }
204 #endif /* __cplusplus */
205 #endif /* __cplusplus */
206 
207 #endif /* _LOS_QUEUE_PRI_H */
208