summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/px_mq_get_new_queue.c
blob: c9479806745e8a908462658cec79479815cdf9ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** POSIX wrapper for THREADX                                             */
/**                                                                       */
/**                                                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/* Include necessary system files.  */

#include "tx_api.h"     /* Threadx API */
#include "pthread.h"    /* Posix API */
#include "px_int.h"     /* Posix helper functions */


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    posix_get_new_queue                                 PORTABLE C      */
/*                                                           6.1.7        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function attempts to build a ThreadX message queue             */
/*    for variable length message queues                                  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    maxnum                                Number of queue entries       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    q_ptr                                 Pointer to posix queue        */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    posix_memory_allocate                 Memory allocate               */
/*    posix_memory_release                  Memory free                   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    POSIX internal Code                                                 */
/*                                                                        */
/**************************************************************************/
POSIX_MSG_QUEUE  * posix_get_new_queue(ULONG maxnum)
{

ULONG                   i;
POSIX_MSG_QUEUE        *q_ptr;
VOID                   *bp;
INT                     retval;
ULONG                   size;


    /* Determine how much memory we need for the queue.
       The queue holds "maxnum" entries;  each entry is 2 ULONG.  */
    size   = (maxnum * (TX_4_ULONG * sizeof(ULONG)));

    /* Now attempt to allocate memory for the queue.  */
    retval = posix_memory_allocate(size, &bp);

    /* Make sure we obtained the memory we requested.  */
    if (retval)
    {
        return((POSIX_MSG_QUEUE  *)NULL);
    }
    /* Loop through the list of queues -  */
    /* try to find one that is not "in use".  */
    /* Search the queue pool for an available queue.  */
   for (i = 0, q_ptr = &(posix_queue_pool[0]);
            i < POSIX_MAX_QUEUES;
            i++, q_ptr++)
   {
        /* Make sure the queue is not "in use".   */
        if (q_ptr->in_use == TX_FALSE)
        {
            /* This queue is now in use.  */
            q_ptr->in_use = TX_TRUE;

            /* Point to allocated memory.  */
            q_ptr->storage = bp;

            q_ptr->px_queue_id = PX_QUEUE_ID;

            /* Stop searching.  */
            break;
        }
   }
   /* If we didn't find a free queue, free the allocated memory. */
   if ( i >= POSIX_MAX_QUEUES)
   {
       posix_memory_release(bp);
        return((POSIX_MSG_QUEUE  *)0);
   }
   /* Return home.  */
   return(q_ptr);
}