summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/px_mq_arrange_msg.c
diff options
context:
space:
mode:
Diffstat (limited to 'utility/rtos_compatibility_layers/posix/px_mq_arrange_msg.c')
-rw-r--r--utility/rtos_compatibility_layers/posix/px_mq_arrange_msg.c149
1 files changed, 80 insertions, 69 deletions
diff --git a/utility/rtos_compatibility_layers/posix/px_mq_arrange_msg.c b/utility/rtos_compatibility_layers/posix/px_mq_arrange_msg.c
index 6bddee43..2a3a3131 100644
--- a/utility/rtos_compatibility_layers/posix/px_mq_arrange_msg.c
+++ b/utility/rtos_compatibility_layers/posix/px_mq_arrange_msg.c
@@ -12,8 +12,8 @@
/**************************************************************************/
/**************************************************************************/
-/** */
-/** POSIX wrapper for THREADX */
+/** */
+/** POSIX wrapper for THREADX */
/** */
/** */
/** */
@@ -32,14 +32,14 @@
/* FUNCTION RELEASE */
/* */
/* posix_arrange_msg PORTABLE C */
-/* 6.1.7 */
+/* 6.2.0 */
/* AUTHOR */
/* */
/* William E. Lamie, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
-/* arrange messages from the queue */
+/* Return the oldest, highest priority message from the queue. */
/* */
/* INPUT */
/* */
@@ -62,117 +62,128 @@
/* */
/* DATE NAME DESCRIPTION */
/* */
-/* 06-02-2021 William E. Lamie Initial Version 6.1.7 */
+/* 06-02-2021 William E. Lamie Initial Version 6.1.7 */
+/* 10-31-2022 Scott Larson Modified comments, */
+/* fixed message swap logic, */
+/* resulting in version 6.2.0 */
/* */
/**************************************************************************/
-ULONG posix_arrange_msg( TX_QUEUE *Queue, ULONG *pMsgPrio )
+ULONG posix_arrange_msg(TX_QUEUE *Queue, ULONG *pMsgPrio)
{
-
- ULONG* Qread; /* to store read ptr of the queue */
- ULONG* temp_q = TX_NULL; /* temp storage for the message pointer */
- ULONG numMsgs; /* no of messages queued */
- ULONG msg; /* temp variable for thr for loop */
- ULONG priority; /* priority of the message */
- ULONG maxPrio; /* max. priority of the messages in queue*/
- ULONG number2; /* messages */
- ULONG minNo; /* oldest message in the same priority */
- ULONG swap; /* temp.variable for the swapping of the */
- /* messages */
+ ULONG* q_read; /* to store read ptr of the queue */
+ ULONG* temp_q = TX_NULL; /* temp storage for the message pointer */
+ ULONG numMsgs; /* no of messages queued */
+ ULONG msg; /* temp variable for thr for loop */
+ ULONG priority; /* priority of the message */
+ ULONG maxPrio; /* max. priority of the messages in queue*/
+ ULONG number2; /* messages */
+ ULONG minNo; /* oldest message in the same priority */
+ ULONG swap; /* temp.variable for the swapping of the */
+ /* messages */
/* initialize the priority to the lowest priority. */
maxPrio = 0;
- minNo=0;
+ minNo = 0;
/* Copy read pointer to the temporary variable. */
- Qread = Queue -> tx_queue_read;
+ q_read = Queue -> tx_queue_read;
/* Copy no. of messages in the queue to the temporary variable. */
numMsgs = Queue -> tx_queue_enqueued;
- if( numMsgs == 0 )
+ /* If there is 0 or 1 message, no rearranging is needed. */
+ if (numMsgs < 2)
+ {
return(OK);
+ }
- for( msg = 0; msg < numMsgs; msg ++)
+ for (msg = 0; msg < numMsgs; msg++)
{
-
- /* Advance Qread by two pointers to read the priority of the message. */
- Qread = Qread + 2 ;
+ /* Advance q_read to read the priority of the message. */
+ q_read = q_read + TX_POSIX_QUEUE_PRIORITY_OFFSET;
/* Priority of the message queued. */
- priority = *Qread;
+ priority = *q_read;
/* check with maxpriority. */
- if( priority > maxPrio )
- {
- /* copy read pointer to temporary buffer. */
- temp_q = Qread-2;
+ if (priority > maxPrio)
+ {
+ /* copy read pointer to temporary pointer. */
+ temp_q = q_read-TX_POSIX_QUEUE_PRIORITY_OFFSET;
+
/* increment read pointer to point to order. */
- Qread++;
+ q_read++;
/* copy FIFO order to the message */
- minNo = *Qread;
+ minNo = *q_read;
+
/* Found higher priority message. */
maxPrio = priority;
- Qread++;
+ q_read++;
}
-
- /* if more than one same priority messages are in the queue
- then check if this the oldest one. */
- else if ( priority == maxPrio )
+
+ /* if more than one message of the same priority is in the queue
+ then check if this the oldest message. */
+ else if (priority == maxPrio)
{
/* increment read pointer to point to read FIFO order */
- Qread++;
+ q_read++;
- /* copy number to the local varialble. */
- number2 = *Qread;
- Qread++;
- /* find the oldest of the messages in this priority level. */
- if( number2 < minNo )
- {
- /* founder older one */
- minNo = number2;
- /* copy read pointer to temporary buffer. */
- temp_q = Qread - 4;
- }
-
+ /* copy number to the local variable. */
+ number2 = *q_read;
+
+ /* Go to next message. */
+ q_read++;
+
+ /* find the oldest of the messages in this priority level. */
+ if( number2 < minNo )
+ {
+ /* founder older one */
+ minNo = number2;
+ /* copy read pointer to temporary buffer. */
+ temp_q = q_read - (TX_POSIX_MESSAGE_SIZE);
+ }
}
- else
- Qread = Qread + 2;
-
- /* Determine if we are at the end. */
- if ( Qread >= Queue ->tx_queue_end)
+ else
+ {
+ /* Not highest priority, go to next message. */
+ q_read = q_read + (TX_POSIX_MESSAGE_SIZE - TX_POSIX_QUEUE_PRIORITY_OFFSET);
+ }
- /* Yes, wrap around to the beginning. */
- Qread = Queue -> tx_queue_start;
+ /* Determine if we are at the end. */
+ if (q_read >= Queue -> tx_queue_end)
+ {
+ /* Yes, wrap around to the beginning. */
+ q_read = Queue -> tx_queue_start;
+ }
}
- /* All messages checked temp holds address of highest priority message and
- maxPrio holds the highest priority*/
-
- if( pMsgPrio != NULL )
+ /* Output priority if non-null */
+ if (pMsgPrio != NULL)
{
/* copy message priority. */
*pMsgPrio = maxPrio;
}
+ /* All messages checked, temp_q holds address of oldest highest priority message
+ and maxPrio holds the highest priority. */
/* Get the current queue read pointer */
- Qread = Queue -> tx_queue_read;
-
- /* if(*pMsgPrio != *(Qread + 2) || minNo < *(Qread + 3))*/
+ q_read = Queue -> tx_queue_read;
+
+ if((temp_q != TX_NULL) && (temp_q != q_read))
{
/* Swap the messages. */
- for ( msg = 0; msg < 4; msg++)
+ for (msg = 0; msg < TX_POSIX_MESSAGE_SIZE; msg++)
{
-
swap = *temp_q;
- *temp_q = *Qread;
- *Qread = swap;
+ *temp_q = *q_read;
+ *q_read = swap;
temp_q++;
- Qread++;
+ q_read++;
}
}
-
+
return(OK);
}