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_priority_search                               PORTABLE C      */
34 /*                                                           6.2.0        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This routine returns the no. of messages of the same priority       */
42 /*    in the message queue.                                               */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    msgQId                 message queue ID                             */
47 /*    priority               priority of the message                      */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    order                  Returns the number of same priority messages.*/
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    None                                                                */
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 /*  10-31-2022      Scott Larson            Add 64-bit support,           */
67 /*                                            resulting in version 6.2.0  */
68 /*                                                                        */
69 /**************************************************************************/
posix_priority_search(mqd_t msgQId,ULONG priority)70 ULONG posix_priority_search(mqd_t msgQId, ULONG priority)
71 {
72 
73 TX_QUEUE            *queue;
74 POSIX_MSG_QUEUE     *q_ptr;
75 ULONG                order = 1;
76 ULONG                numMsgs;
77 UINT                 index;
78 ULONG               *source;
79 ULONG                msgp;
80 
81     queue   = &(msgQId->f_data->queue);
82     q_ptr   = (POSIX_MSG_QUEUE * )queue;
83 
84     /* No. of messages in the queue.  */
85     numMsgs = q_ptr -> queue.tx_queue_enqueued;
86 
87     /* retrieving the message pointer.  */
88     source  =  q_ptr->queue.tx_queue_read;
89 
90     /* check for same priority.  */
91     for(index = 0; index < numMsgs; index++)
92     {
93         source += TX_POSIX_QUEUE_PRIORITY_OFFSET;
94         msgp = *source;
95         source += (TX_POSIX_MESSAGE_SIZE - TX_POSIX_QUEUE_PRIORITY_OFFSET);
96 
97         /* If we're at end of queue, go to start. */
98         if(source == q_ptr->queue.tx_queue_end)
99             source = q_ptr->queue.tx_queue_start;
100 
101         /* Increment priority count. */
102         if(priority == msgp)
103             order += 1;
104     }
105 
106     /* Return the number of same priority messages. */
107     return(order);
108 }
109