1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** ThreadX Component                                                     */
16 /**                                                                       */
17 /**   Queue                                                               */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define TX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "tx_api.h"
28 #include "tx_trace.h"
29 #include "tx_queue.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _tx_queue_create                                    PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    William E. Lamie, Microsoft Corporation                             */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function creates a message queue.  The message size and depth  */
45 /*    of the queue is specified by the caller.                            */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    queue_ptr                         Pointer to queue control block    */
50 /*    name_ptr                          Pointer to queue name             */
51 /*    message_size                      Size of each queue message        */
52 /*    queue_start                       Starting address of the queue area*/
53 /*    queue_size                        Number of bytes in the queue      */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    TX_SUCCESS                        Successful completion status      */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    None                                                                */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application Code                                                    */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
72 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_tx_queue_create(TX_QUEUE * queue_ptr,CHAR * name_ptr,UINT message_size,VOID * queue_start,ULONG queue_size)76 UINT  _tx_queue_create(TX_QUEUE *queue_ptr, CHAR *name_ptr, UINT message_size,
77                         VOID *queue_start, ULONG queue_size)
78 {
79 
80 TX_INTERRUPT_SAVE_AREA
81 
82 UINT            capacity;
83 UINT            used_words;
84 TX_QUEUE        *next_queue;
85 TX_QUEUE        *previous_queue;
86 
87 
88     /* Initialize queue control block to all zeros.  */
89     TX_MEMSET(queue_ptr, 0, (sizeof(TX_QUEUE)));
90 
91     /* Setup the basic queue fields.  */
92     queue_ptr -> tx_queue_name =             name_ptr;
93 
94     /* Save the message size in the control block.  */
95     queue_ptr -> tx_queue_message_size =  message_size;
96 
97     /* Determine how many messages will fit in the queue area and the number
98        of ULONGs used.  */
99     capacity =    (UINT) (queue_size / ((ULONG) (((ULONG) message_size) * (sizeof(ULONG)))));
100     used_words =  capacity * message_size;
101 
102     /* Save the starting address and calculate the ending address of
103        the queue.  Note that the ending address is really one past the
104        end!  */
105     queue_ptr -> tx_queue_start =  TX_VOID_TO_ULONG_POINTER_CONVERT(queue_start);
106     queue_ptr -> tx_queue_end =    TX_ULONG_POINTER_ADD(queue_ptr -> tx_queue_start, used_words);
107 
108     /* Set the read and write pointers to the beginning of the queue
109        area.  */
110     queue_ptr -> tx_queue_read =   TX_VOID_TO_ULONG_POINTER_CONVERT(queue_start);
111     queue_ptr -> tx_queue_write =  TX_VOID_TO_ULONG_POINTER_CONVERT(queue_start);
112 
113     /* Setup the number of enqueued messages and the number of message
114        slots available in the queue.  */
115     queue_ptr -> tx_queue_available_storage =  (UINT) capacity;
116     queue_ptr -> tx_queue_capacity =           (UINT) capacity;
117 
118     /* Disable interrupts to put the queue on the created list.  */
119     TX_DISABLE
120 
121     /* Setup the queue ID to make it valid.  */
122     queue_ptr -> tx_queue_id =  TX_QUEUE_ID;
123 
124     /* Place the queue on the list of created queues.  First,
125        check for an empty list.  */
126     if (_tx_queue_created_count == TX_EMPTY)
127     {
128 
129         /* The created queue list is empty.  Add queue to empty list.  */
130         _tx_queue_created_ptr =                   queue_ptr;
131         queue_ptr -> tx_queue_created_next =      queue_ptr;
132         queue_ptr -> tx_queue_created_previous =  queue_ptr;
133     }
134     else
135     {
136 
137         /* This list is not NULL, add to the end of the list.  */
138         next_queue =      _tx_queue_created_ptr;
139         previous_queue =  next_queue -> tx_queue_created_previous;
140 
141         /* Place the new queue in the list.  */
142         next_queue -> tx_queue_created_previous =  queue_ptr;
143         previous_queue -> tx_queue_created_next =  queue_ptr;
144 
145         /* Setup this queues's created links.  */
146         queue_ptr -> tx_queue_created_previous =  previous_queue;
147         queue_ptr -> tx_queue_created_next =      next_queue;
148     }
149 
150     /* Increment the created queue count.  */
151     _tx_queue_created_count++;
152 
153     /* Optional queue create extended processing.  */
154     TX_QUEUE_CREATE_EXTENSION(queue_ptr)
155 
156     /* If trace is enabled, register this object.  */
157     TX_TRACE_OBJECT_REGISTER(TX_TRACE_OBJECT_TYPE_QUEUE, queue_ptr, name_ptr, queue_size, message_size)
158 
159     /* If trace is enabled, insert this event into the trace buffer.  */
160     TX_TRACE_IN_LINE_INSERT(TX_TRACE_QUEUE_CREATE, queue_ptr, message_size, TX_POINTER_TO_ULONG_CONVERT(queue_start), queue_size, TX_TRACE_QUEUE_EVENTS)
161 
162     /* Log this kernel call.  */
163     TX_EL_QUEUE_CREATE_INSERT
164 
165     /* Restore interrupts.  */
166     TX_RESTORE
167 
168     /* Return TX_SUCCESS.  */
169     return(TX_SUCCESS);
170 }
171 
172