summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/px_mq_priority_search.c
blob: da7c599697845345df68993bb51fae35b0e8fa1e (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
/***************************************************************************
 * 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_priority_search                               PORTABLE C      */
/*                                                           6.2.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This routine returns the no. of messages of the same priority       */
/*    in the message queue.                                               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    msgQId                 message queue ID                             */
/*    priority               priority of the message                      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    order                  Returns the number of same priority messages.*/
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    POSIX internal Code                                                 */
/*                                                                        */
/**************************************************************************/
ULONG posix_priority_search(mqd_t msgQId, ULONG priority)
{

TX_QUEUE            *queue;
POSIX_MSG_QUEUE     *q_ptr;
ULONG                order = 1;
ULONG                numMsgs;
UINT                 index;
ULONG               *source;
ULONG                msgp;

    queue   = &(msgQId->f_data->queue);
    q_ptr   = (POSIX_MSG_QUEUE * )queue;

    /* No. of messages in the queue.  */
    numMsgs = q_ptr -> queue.tx_queue_enqueued;

    /* retrieving the message pointer.  */
    source  =  q_ptr->queue.tx_queue_read;

    /* check for same priority.  */
    for(index = 0; index < numMsgs; index++)
    {
        source += TX_POSIX_QUEUE_PRIORITY_OFFSET;
        msgp = *source;
        source += (TX_POSIX_MESSAGE_SIZE - TX_POSIX_QUEUE_PRIORITY_OFFSET);

        /* If we're at end of queue, go to start. */
        if(source == q_ptr->queue.tx_queue_end)
            source = q_ptr->queue.tx_queue_start;

        /* Increment priority count. */
        if(priority == msgp)
            order += 1;
    }

    /* Return the number of same priority messages. */
    return(order);
}