1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** POSIX wrapper for THREADX                                             */
17 /**                                                                       */
18 /**                                                                       */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 /* Include necessary system files.  */
24 
25 #include "tx_api.h"     /* Threadx API */
26 #include "pthread.h"    /* Posix API */
27 #include "px_int.h"     /* Posix helper functions */
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    posix_get_new_queue                                 PORTABLE C      */
35 /*                                                           6.1.7        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function attempts to build a ThreadX message queue             */
43 /*    for variable length message queues                                  */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    maxnum                                Number of queue entries       */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    q_ptr                                 Pointer to posix queue        */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    posix_memory_allocate                 Memory allocate               */
56 /*    posix_memory_release                  Memory free                   */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    POSIX internal Code                                                 */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
67 /*                                                                        */
68 /**************************************************************************/
posix_get_new_queue(ULONG maxnum)69 POSIX_MSG_QUEUE  * posix_get_new_queue(ULONG maxnum)
70 {
71 
72 ULONG                   i;
73 POSIX_MSG_QUEUE        *q_ptr;
74 VOID                   *bp;
75 INT                     retval;
76 ULONG                   size;
77 
78 
79     /* Determine how much memory we need for the queue.
80        The queue holds "maxnum" entries;  each entry is 2 ULONG.  */
81     size   = (maxnum * (TX_4_ULONG * sizeof(ULONG)));
82 
83     /* Now attempt to allocate memory for the queue.  */
84     retval = posix_memory_allocate(size, &bp);
85 
86     /* Make sure we obtained the memory we requested.  */
87     if (retval)
88     {
89         return((POSIX_MSG_QUEUE  *)NULL);
90     }
91     /* Loop through the list of queues -  */
92     /* try to find one that is not "in use".  */
93     /* Search the queue pool for an available queue.  */
94    for (i = 0, q_ptr = &(posix_queue_pool[0]);
95             i < POSIX_MAX_QUEUES;
96             i++, q_ptr++)
97    {
98         /* Make sure the queue is not "in use".   */
99         if (q_ptr->in_use == TX_FALSE)
100         {
101             /* This queue is now in use.  */
102             q_ptr->in_use = TX_TRUE;
103 
104             /* Point to allocated memory.  */
105             q_ptr->storage = bp;
106 
107             q_ptr->px_queue_id = PX_QUEUE_ID;
108 
109             /* Stop searching.  */
110             break;
111         }
112    }
113    /* If we didn't find a free queue, free the allocated memory. */
114    if ( i >= POSIX_MAX_QUEUES)
115    {
116        posix_memory_release(bp);
117         return((POSIX_MSG_QUEUE  *)0);
118    }
119    /* Return home.  */
120    return(q_ptr);
121 }
122