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