diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 51314c957..1aca890b3 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -64,6 +64,9 @@ target_sources(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_delete.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_flush.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_front_send.c + ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_peek.c + ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_peek_release.c + ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_peek_resume.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_info_get.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_initialize.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_performance_info_get.c @@ -164,6 +167,7 @@ target_sources(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_delete.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_flush.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_front_send.c + ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_peek.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_info_get.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_prioritize.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_receive.c diff --git a/common/inc/tx_api.h b/common/inc/tx_api.h index cffb0eb00..13ff4bd13 100644 --- a/common/inc/tx_api.h +++ b/common/inc/tx_api.h @@ -1127,6 +1127,7 @@ UINT _tx_trace_interrupt_control(UINT new_posture); #define tx_queue_send _tx_queue_send #define tx_queue_send_notify _tx_queue_send_notify #define tx_queue_front_send _tx_queue_front_send +#define tx_queue_peek _tx_queue_peek #define tx_queue_prioritize _tx_queue_prioritize #define tx_semaphore_ceiling_put _tx_semaphore_ceiling_put @@ -1250,6 +1251,7 @@ UINT _tx_trace_interrupt_control(UINT new_posture); #define tx_queue_send _txr_queue_send #define tx_queue_send_notify _txr_queue_send_notify #define tx_queue_front_send _txr_queue_front_send +#define tx_queue_peek _txr_queue_peek #define tx_queue_prioritize _txr_queue_prioritize #define tx_semaphore_ceiling_put _txr_semaphore_ceiling_put @@ -1361,6 +1363,7 @@ UINT _tx_el_interrupt_control(UINT new_posture); #define tx_queue_send _txe_queue_send #define tx_queue_send_notify _txe_queue_send_notify #define tx_queue_front_send _txe_queue_front_send +#define tx_queue_peek _txe_queue_peek #define tx_queue_prioritize _txe_queue_prioritize #define tx_semaphore_ceiling_put _txe_semaphore_ceiling_put @@ -1620,6 +1623,7 @@ UINT _tx_queue_receive(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG UINT _tx_queue_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); UINT _tx_queue_send_notify(TX_QUEUE *queue_ptr, VOID (*queue_send_notify)(TX_QUEUE *notify_queue_ptr)); UINT _tx_queue_front_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); +UINT _tx_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option); /* Define error checking shells for API services. These are only referenced by the @@ -1636,6 +1640,7 @@ UINT _txe_queue_receive(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG UINT _txe_queue_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); UINT _txe_queue_send_notify(TX_QUEUE *queue_ptr, VOID (*queue_send_notify)(TX_QUEUE *notify_queue_ptr)); UINT _txe_queue_front_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); +UINT _txe_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option); #ifdef TX_ENABLE_MULTI_ERROR_CHECKING UINT _txr_queue_create(TX_QUEUE *queue_ptr, TX_NAME_CONST CHAR *name_ptr, UINT message_size, VOID *queue_start, ULONG queue_size, UINT queue_control_block_size); @@ -1648,6 +1653,7 @@ UINT _txr_queue_receive(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG UINT _txr_queue_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); UINT _txr_queue_send_notify(TX_QUEUE *queue_ptr, VOID (*queue_send_notify)(TX_QUEUE *notify_queue_ptr)); UINT _txr_queue_front_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); +UINT _txr_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option); #endif diff --git a/common/inc/tx_queue.h b/common/inc/tx_queue.h index c8b4122c5..3128fca90 100644 --- a/common/inc/tx_queue.h +++ b/common/inc/tx_queue.h @@ -48,6 +48,11 @@ #define TX_QUEUE_ID ((ULONG) 0x51554555) +/* Value of tx_thread_suspend_option for a thread suspended in tx_queue_peek. */ + +#define TX_QUEUE_PEEK_SUSPEND ((UINT) 2) + + /* Determine if in-line component initialization is supported by the caller. */ #ifdef TX_INVOKE_INLINE_INITIALIZATION @@ -96,6 +101,8 @@ VOID _tx_queue_initialize(VOID); /* Define internal queue management function prototypes. */ VOID _tx_queue_cleanup(TX_THREAD *thread_ptr, ULONG suspension_sequence); +TX_THREAD *_tx_queue_peek_release(TX_QUEUE *queue_ptr, VOID *source_ptr); +VOID _tx_queue_peek_resume(TX_THREAD *thread_list); /* Queue management component data declarations follow. */ diff --git a/common/inc/tx_trace.h b/common/inc/tx_trace.h index 8f2667cac..b0a200403 100644 --- a/common/inc/tx_trace.h +++ b/common/inc/tx_trace.h @@ -304,6 +304,7 @@ typedef struct TX_TRACE_OBJECT_ENTRY_STRUCT #define TX_TRACE_QUEUE_RECEIVE 68 /* I1 = queue ptr, I2 = destination ptr, I3 = wait option, I4 = enqueued */ #define TX_TRACE_QUEUE_SEND 69 /* I1 = queue ptr, I2 = source ptr, I3 = wait option, I4 = enqueued */ #define TX_TRACE_QUEUE_SEND_NOTIFY 70 /* I1 = queue ptr */ +#define TX_TRACE_QUEUE_PEEK 71 /* I1 = queue ptr, I2 = destination ptr, I3 = wait option, I4 = enqueued */ #define TX_TRACE_SEMAPHORE_CEILING_PUT 80 /* I1 = semaphore ptr, I2 = current count, I3 = suspended count,I4 =ceiling */ #define TX_TRACE_SEMAPHORE_CREATE 81 /* I1 = semaphore ptr, I2 = initial count, I3 = stack ptr */ #define TX_TRACE_SEMAPHORE_DELETE 82 /* I1 = semaphore ptr, I2 = stack ptr */ diff --git a/common/src/tx_queue_front_send.c b/common/src/tx_queue_front_send.c index bb39b970b..dae59fd17 100644 --- a/common/src/tx_queue_front_send.c +++ b/common/src/tx_queue_front_send.c @@ -82,6 +82,7 @@ UINT suspended_count; TX_THREAD *next_thread; TX_THREAD *previous_thread; UINT status; +TX_THREAD *peek_list; #ifndef TX_DISABLE_NOTIFY_CALLBACKS VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); #endif @@ -115,6 +116,15 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); if (queue_ptr -> tx_queue_available_storage != ((UINT) 0)) { + /* Peeking threads get a copy; the message is still delivered below. */ + peek_list = TX_NULL; + if (suspended_count != TX_NO_SUSPENSIONS) + { + + peek_list = _tx_queue_peek_release(queue_ptr, source_ptr); + suspended_count = queue_ptr -> tx_queue_suspended_count; + } + /* Yes there is room in the queue. Now determine if there is a thread waiting for a message. */ if (suspended_count == TX_NO_SUSPENSIONS) @@ -167,6 +177,12 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); /* Restore interrupts. */ TX_RESTORE + if (peek_list != TX_NULL) + { + + _tx_queue_peek_resume(peek_list); + } + #ifndef TX_DISABLE_NOTIFY_CALLBACKS /* Determine if a notify callback is required. */ @@ -259,6 +275,12 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); _tx_thread_system_resume(thread_ptr); #endif + if (peek_list != TX_NULL) + { + + _tx_queue_peek_resume(peek_list); + } + #ifndef TX_DISABLE_NOTIFY_CALLBACKS /* Determine if a notify callback is required. */ diff --git a/common/src/tx_queue_peek.c b/common/src/tx_queue_peek.c new file mode 100644 index 000000000..f043b7891 --- /dev/null +++ b/common/src/tx_queue_peek.c @@ -0,0 +1,237 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Queue */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE + + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_trace.h" +#include "tx_thread.h" +#include "tx_queue.h" + + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_queue_peek PORTABLE C */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function copies the message at the front of the specified */ +/* queue without removing it. The message is the one the next */ +/* tx_queue_receive would return. If the queue is empty, this */ +/* function waits according to the option specified; a waiting thread */ +/* is handed a copy of the next message sent to the queue, and that */ +/* message stays in the queue. */ +/* */ +/* INPUT */ +/* */ +/* queue_ptr Pointer to queue control block */ +/* destination_ptr Pointer to message destination */ +/* **** MUST BE LARGE ENOUGH TO */ +/* HOLD MESSAGE **** */ +/* wait_option Suspension option */ +/* */ +/* OUTPUT */ +/* */ +/* status Completion status */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_system_suspend Suspend thread routine */ +/* _tx_thread_system_ni_suspend Non-interruptable suspend thread */ +/* */ +/* CALLED BY */ +/* */ +/* Application Code */ +/* */ +/**************************************************************************/ +UINT _tx_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option) +{ + +TX_INTERRUPT_SAVE_AREA + +TX_THREAD *thread_ptr; +ULONG *source; +ULONG *destination; +UINT size; +UINT suspended_count; +TX_THREAD *next_thread; +TX_THREAD *previous_thread; +UINT status; + + + /* Default the status to TX_SUCCESS. */ + status = TX_SUCCESS; + + /* Disable interrupts to peek at the queue. */ + TX_DISABLE + + /* If trace is enabled, insert this event into the trace buffer. */ + TX_TRACE_IN_LINE_INSERT(TX_TRACE_QUEUE_PEEK, queue_ptr, TX_POINTER_TO_ULONG_CONVERT(destination_ptr), wait_option, queue_ptr -> tx_queue_enqueued, TX_TRACE_QUEUE_EVENTS) + + /* Pickup the thread suspension count. */ + suspended_count = queue_ptr -> tx_queue_suspended_count; + + /* Determine if there is anything in the queue. */ + if (queue_ptr -> tx_queue_enqueued != TX_NO_MESSAGES) + { + + /* Default to the message at the read pointer. */ + source = queue_ptr -> tx_queue_read; + + /* Threads suspended on a non-empty queue are senders waiting on a full queue. + A front send suspension at the head is what tx_queue_receive returns next. */ + if (suspended_count != TX_NO_SUSPENSIONS) + { + + thread_ptr = queue_ptr -> tx_queue_suspension_list; + if (thread_ptr -> tx_thread_suspend_option == TX_TRUE) + { + + source = TX_VOID_TO_ULONG_POINTER_CONVERT(thread_ptr -> tx_thread_additional_suspend_info); + } + } + + /* Setup destination pointer and size. */ + destination = TX_VOID_TO_ULONG_POINTER_CONVERT(destination_ptr); + size = queue_ptr -> tx_queue_message_size; + + /* Copy message. Note that the source and destination pointers are + incremented by the macro. */ + TX_QUEUE_MESSAGE_COPY(source, destination, size) + + /* Restore interrupts. */ + TX_RESTORE + } + + /* Determine if the request specifies suspension. */ + else if (wait_option != TX_NO_WAIT) + { + + /* Determine if the preempt disable flag is non-zero. */ + if (_tx_thread_preempt_disable != ((UINT) 0)) + { + + /* Restore interrupts. */ + TX_RESTORE + + /* Suspension is not allowed if the preempt disable flag is non-zero at this point - return error completion. */ + status = TX_QUEUE_EMPTY; + } + else + { + + /* Prepare for suspension of this thread. */ + + /* Pickup thread pointer. */ + TX_THREAD_GET_CURRENT(thread_ptr) + + /* Setup cleanup routine pointer. */ + thread_ptr -> tx_thread_suspend_cleanup = &(_tx_queue_cleanup); + + /* Setup cleanup information, i.e. this queue control + block and the destination pointer. */ + thread_ptr -> tx_thread_suspend_control_block = (VOID *) queue_ptr; + thread_ptr -> tx_thread_additional_suspend_info = (VOID *) destination_ptr; + thread_ptr -> tx_thread_suspend_option = TX_QUEUE_PEEK_SUSPEND; + +#ifndef TX_NOT_INTERRUPTABLE + + /* Increment the suspension sequence number, which is used to identify + this suspension event. */ + thread_ptr -> tx_thread_suspension_sequence++; +#endif + + /* Setup suspension list. */ + if (suspended_count == TX_NO_SUSPENSIONS) + { + + /* No other threads are suspended. Setup the head pointer and + just setup this threads pointers to itself. */ + queue_ptr -> tx_queue_suspension_list = thread_ptr; + thread_ptr -> tx_thread_suspended_next = thread_ptr; + thread_ptr -> tx_thread_suspended_previous = thread_ptr; + } + else + { + + /* This list is not NULL, add current thread to the end. */ + next_thread = queue_ptr -> tx_queue_suspension_list; + thread_ptr -> tx_thread_suspended_next = next_thread; + previous_thread = next_thread -> tx_thread_suspended_previous; + thread_ptr -> tx_thread_suspended_previous = previous_thread; + previous_thread -> tx_thread_suspended_next = thread_ptr; + next_thread -> tx_thread_suspended_previous = thread_ptr; + } + + /* Increment the suspended thread count. */ + queue_ptr -> tx_queue_suspended_count = suspended_count + ((UINT) 1); + + /* Set the state to suspended. */ + thread_ptr -> tx_thread_state = TX_QUEUE_SUSP; + +#ifdef TX_NOT_INTERRUPTABLE + + /* Call actual non-interruptable thread suspension routine. */ + _tx_thread_system_ni_suspend(thread_ptr, wait_option); + + /* Restore interrupts. */ + TX_RESTORE +#else + + /* Set the suspending flag. */ + thread_ptr -> tx_thread_suspending = TX_TRUE; + + /* Setup the timeout period. */ + thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks = wait_option; + + /* Temporarily disable preemption. */ + _tx_thread_preempt_disable++; + + /* Restore interrupts. */ + TX_RESTORE + + /* Call actual thread suspension routine. */ + _tx_thread_system_suspend(thread_ptr); +#endif + + /* Return the completion status. */ + status = thread_ptr -> tx_thread_suspend_status; + } + } + else + { + + /* Restore interrupts. */ + TX_RESTORE + + /* Immediate return, return error completion. */ + status = TX_QUEUE_EMPTY; + } + + /* Return completion status. */ + return(status); +} + diff --git a/common/src/tx_queue_peek_release.c b/common/src/tx_queue_peek_release.c new file mode 100644 index 000000000..95ed931ac --- /dev/null +++ b/common/src/tx_queue_peek_release.c @@ -0,0 +1,155 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Queue */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE + + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_thread.h" +#include "tx_queue.h" + + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_queue_peek_release PORTABLE C */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function copies the message being sent to an empty queue into */ +/* every thread suspended in tx_queue_peek at the head of the queue's */ +/* suspension list, and removes those threads from the list. The */ +/* removed threads are returned as a list linked through */ +/* tx_thread_suspended_next, ready for _tx_queue_peek_resume. */ +/* */ +/* Interrupts must be disabled by the caller. If any thread is */ +/* released, preemption is disabled until _tx_queue_peek_resume. */ +/* */ +/* INPUT */ +/* */ +/* queue_ptr Pointer to queue control block */ +/* source_ptr Pointer to message being sent */ +/* */ +/* OUTPUT */ +/* */ +/* thread_list Released threads, or TX_NULL */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_queue_send Queue send function */ +/* _tx_queue_front_send Queue send to front function */ +/* */ +/**************************************************************************/ +TX_THREAD *_tx_queue_peek_release(TX_QUEUE *queue_ptr, VOID *source_ptr) +{ + +TX_THREAD *thread_ptr; +TX_THREAD *next_thread; +TX_THREAD *previous_thread; +TX_THREAD *released_list; +TX_THREAD *last_released; +ULONG *source; +ULONG *destination; +UINT size; +UINT suspended_count; + + + released_list = TX_NULL; + last_released = TX_NULL; + suspended_count = queue_ptr -> tx_queue_suspended_count; + + while (suspended_count != TX_NO_SUSPENSIONS) + { + + /* Stop at the first thread that is not peeking. */ + thread_ptr = queue_ptr -> tx_queue_suspension_list; + if (thread_ptr -> tx_thread_suspend_option != TX_QUEUE_PEEK_SUSPEND) + { + break; + } + + /* Remove the thread from the head of the suspension list. */ + suspended_count--; + if (suspended_count == TX_NO_SUSPENSIONS) + { + + queue_ptr -> tx_queue_suspension_list = TX_NULL; + } + else + { + + next_thread = thread_ptr -> tx_thread_suspended_next; + queue_ptr -> tx_queue_suspension_list = next_thread; + previous_thread = thread_ptr -> tx_thread_suspended_previous; + next_thread -> tx_thread_suspended_previous = previous_thread; + previous_thread -> tx_thread_suspended_next = next_thread; + } + + /* Clear cleanup routine to avoid timeout. */ + thread_ptr -> tx_thread_suspend_cleanup = TX_NULL; + + /* Setup source and destination pointers. */ + source = TX_VOID_TO_ULONG_POINTER_CONVERT(source_ptr); + destination = TX_VOID_TO_ULONG_POINTER_CONVERT(thread_ptr -> tx_thread_additional_suspend_info); + size = queue_ptr -> tx_queue_message_size; + + /* Copy message. Note that the source and destination pointers are + incremented by the macro. */ + TX_QUEUE_MESSAGE_COPY(source, destination, size) + + /* Put return status into the thread control block. */ + thread_ptr -> tx_thread_suspend_status = TX_SUCCESS; + + /* Append the thread to the released list. */ + thread_ptr -> tx_thread_suspended_next = TX_NULL; + if (released_list == TX_NULL) + { + + released_list = thread_ptr; + } + else + { + + last_released -> tx_thread_suspended_next = thread_ptr; + } + last_released = thread_ptr; + } + + /* Update the suspension count. */ + queue_ptr -> tx_queue_suspended_count = suspended_count; + + /* Keep released threads from being preempted by each other until all are resumed. */ + if (released_list != TX_NULL) + { + + _tx_thread_preempt_disable++; + } + + return(released_list); +} + diff --git a/common/src/tx_queue_peek_resume.c b/common/src/tx_queue_peek_resume.c new file mode 100644 index 000000000..810166d27 --- /dev/null +++ b/common/src/tx_queue_peek_resume.c @@ -0,0 +1,113 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Queue */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE + + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_thread.h" +#include "tx_queue.h" + + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_queue_peek_resume PORTABLE C */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function resumes the threads returned by */ +/* _tx_queue_peek_release, releases the preemption disable it took, */ +/* and checks for preemption. Interrupts must be enabled by the caller.*/ +/* */ +/* INPUT */ +/* */ +/* thread_list Released threads (not TX_NULL) */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_system_resume Resume thread routine */ +/* _tx_thread_system_ni_resume Non-interruptable resume thread */ +/* _tx_thread_system_preempt_check Check for preemption */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_queue_send Queue send function */ +/* _tx_queue_front_send Queue send to front function */ +/* */ +/**************************************************************************/ +VOID _tx_queue_peek_resume(TX_THREAD *thread_list) +{ + +TX_INTERRUPT_SAVE_AREA + +TX_THREAD *thread_ptr; +TX_THREAD *next_thread; + + + thread_ptr = thread_list; + while (thread_ptr != TX_NULL) + { + + /* Get next pointer first, resume reuses the link. */ + next_thread = thread_ptr -> tx_thread_suspended_next; + + /* Disable interrupts. */ + TX_DISABLE + +#ifdef TX_NOT_INTERRUPTABLE + + /* Resume the thread! */ + _tx_thread_system_ni_resume(thread_ptr); + + /* Restore interrupts. */ + TX_RESTORE +#else + + /* Temporarily disable preemption. */ + _tx_thread_preempt_disable++; + + /* Restore interrupts. */ + TX_RESTORE + + /* Resume thread. */ + _tx_thread_system_resume(thread_ptr); +#endif + + thread_ptr = next_thread; + } + + /* Release the preemption disable taken by _tx_queue_peek_release. */ + TX_DISABLE + _tx_thread_preempt_disable--; + TX_RESTORE + + /* Check for preemption. */ + _tx_thread_system_preempt_check(); +} + diff --git a/common/src/tx_queue_send.c b/common/src/tx_queue_send.c index 7d22f5b9c..be1b759f0 100644 --- a/common/src/tx_queue_send.c +++ b/common/src/tx_queue_send.c @@ -82,6 +82,7 @@ UINT suspended_count; TX_THREAD *next_thread; TX_THREAD *previous_thread; UINT status; +TX_THREAD *peek_list; #ifndef TX_DISABLE_NOTIFY_CALLBACKS VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); #endif @@ -117,6 +118,15 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); /* There is room for the message in the queue. */ + /* Peeking threads get a copy; the message is still delivered below. */ + peek_list = TX_NULL; + if (suspended_count != TX_NO_SUSPENSIONS) + { + + peek_list = _tx_queue_peek_release(queue_ptr, source_ptr); + suspended_count = queue_ptr -> tx_queue_suspended_count; + } + /* Determine if there are suspended on this queue. */ if (suspended_count == TX_NO_SUSPENSIONS) { @@ -160,6 +170,12 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); /* Restore interrupts. */ TX_RESTORE + if (peek_list != TX_NULL) + { + + _tx_queue_peek_resume(peek_list); + } + #ifndef TX_DISABLE_NOTIFY_CALLBACKS /* Determine if a notify callback is required. */ @@ -254,6 +270,12 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); _tx_thread_system_resume(thread_ptr); #endif + if (peek_list != TX_NULL) + { + + _tx_queue_peek_resume(peek_list); + } + #ifndef TX_DISABLE_NOTIFY_CALLBACKS /* Determine if a notify callback is required. */ diff --git a/common/src/txe_queue_peek.c b/common/src/txe_queue_peek.c new file mode 100644 index 000000000..9d17f60b7 --- /dev/null +++ b/common/src/txe_queue_peek.c @@ -0,0 +1,149 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Queue */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE + + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_timer.h" +#include "tx_thread.h" +#include "tx_queue.h" + + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _txe_queue_peek PORTABLE C */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function checks for errors in the queue peek function call. */ +/* */ +/* INPUT */ +/* */ +/* queue_ptr Pointer to queue control block */ +/* destination_ptr Pointer to message destination */ +/* **** MUST BE LARGE ENOUGH TO */ +/* HOLD MESSAGE **** */ +/* wait_option Suspension option */ +/* */ +/* OUTPUT */ +/* */ +/* TX_QUEUE_ERROR Invalid queue pointer */ +/* TX_PTR_ERROR Invalid destination pointer (NULL)*/ +/* TX_WAIT_ERROR Invalid wait option */ +/* status Actual completion status */ +/* */ +/* CALLS */ +/* */ +/* _tx_queue_peek Actual queue peek function */ +/* */ +/* CALLED BY */ +/* */ +/* Application Code */ +/* */ +/**************************************************************************/ +UINT _txe_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option) +{ + +UINT status; + +#ifndef TX_TIMER_PROCESS_IN_ISR +TX_THREAD *current_thread; +#endif + + + /* Default status to success. */ + status = TX_SUCCESS; + + /* Check for an invalid queue pointer. */ + if (queue_ptr == TX_NULL) + { + + /* Queue pointer is invalid, return appropriate error code. */ + status = TX_QUEUE_ERROR; + } + + /* Now check for invalid queue ID. */ + else if (queue_ptr -> tx_queue_id != TX_QUEUE_ID) + { + + /* Queue pointer is invalid, return appropriate error code. */ + status = TX_QUEUE_ERROR; + } + + /* Check for an invalid destination for message. */ + else if (destination_ptr == TX_NULL) + { + + /* Null destination pointer, return appropriate error. */ + status = TX_PTR_ERROR; + } + else + { + + /* Check for a wait option error. Only threads are allowed any form of + suspension. */ + if (wait_option != TX_NO_WAIT) + { + + /* Is the call from an ISR or Initialization? */ + if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0)) + { + + /* A non-thread is trying to suspend, return appropriate error code. */ + status = TX_WAIT_ERROR; + } + +#ifndef TX_TIMER_PROCESS_IN_ISR + else + { + + /* Pickup thread pointer. */ + TX_THREAD_GET_CURRENT(current_thread) + + /* Is the current thread the timer thread? */ + if (current_thread == &_tx_timer_thread) + { + + /* A non-thread is trying to suspend, return appropriate error code. */ + status = TX_WAIT_ERROR; + } + } +#endif + } + } + + /* Determine if everything is okay. */ + if (status == TX_SUCCESS) + { + + /* Call actual queue peek function. */ + status = _tx_queue_peek(queue_ptr, destination_ptr, wait_option); + } + + /* Return completion status. */ + return(status); +} + diff --git a/common_modules/inc/txm_module.h b/common_modules/inc/txm_module.h index 2a4764842..021069232 100644 --- a/common_modules/inc/txm_module.h +++ b/common_modules/inc/txm_module.h @@ -324,6 +324,7 @@ extern "C" { #define TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL 94 #define TXM_MODULE_OBJECT_ALLOCATE_CALL 95 #define TXM_MODULE_OBJECT_DEALLOCATE_CALL 96 +#define TXM_QUEUE_PEEK_CALL 97 #define TXM_MODULE_PORT_EXTENSION_API_ID_START 500 #define TXM_MODULE_PORT_EXTENSION_API_ID_END 999 diff --git a/common_modules/inc/txm_module_user_sample.h b/common_modules/inc/txm_module_user_sample.h index 889e0b91c..d8ca90937 100644 --- a/common_modules/inc/txm_module_user_sample.h +++ b/common_modules/inc/txm_module_user_sample.h @@ -91,6 +91,7 @@ /* #define TXM_QUEUE_FLUSH_CALL_NOT_USED */ /* #define TXM_QUEUE_FRONT_SEND_CALL_NOT_USED */ /* #define TXM_QUEUE_INFO_GET_CALL_NOT_USED */ +/* #define TXM_QUEUE_PEEK_CALL_NOT_USED */ /* #define TXM_QUEUE_PERFORMANCE_INFO_GET_CALL_NOT_USED */ /* #define TXM_QUEUE_PERFORMANCE_SYSTEM_INFO_GET_CALL_NOT_USED */ /* #define TXM_QUEUE_PRIORITIZE_CALL_NOT_USED */ diff --git a/common_modules/module_lib/src/txm_queue_peek.c b/common_modules/module_lib/src/txm_queue_peek.c new file mode 100644 index 000000000..dd8e6bdb6 --- /dev/null +++ b/common_modules/module_lib/src/txm_queue_peek.c @@ -0,0 +1,70 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Module */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TXM_MODULE +#include "txm_module.h" +#ifndef TXM_QUEUE_PEEK_CALL_NOT_USED +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _txe_queue_peek PORTABLE C */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function checks for errors in the queue peek function call. */ +/* */ +/* INPUT */ +/* */ +/* queue_ptr Pointer to queue control block */ +/* destination_ptr Pointer to message destination */ +/* **** MUST BE LARGE ENOUGH TO */ +/* HOLD MESSAGE **** */ +/* wait_option Suspension option */ +/* */ +/* OUTPUT */ +/* */ +/* TX_QUEUE_ERROR Invalid queue pointer */ +/* TX_PTR_ERROR Invalid destination pointer (NULL)*/ +/* TX_WAIT_ERROR Invalid wait option */ +/* status Actual completion status */ +/* */ +/* CALLS */ +/* */ +/* _txm_module_kernel_call_dispatcher */ +/* */ +/* CALLED BY */ +/* */ +/* Module application code */ +/* */ +/**************************************************************************/ +UINT _txe_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option) +{ + +UINT return_value; + + /* Call module manager dispatcher. */ + return_value = (UINT) (_txm_module_kernel_call_dispatcher)(TXM_QUEUE_PEEK_CALL, (ALIGN_TYPE) queue_ptr, (ALIGN_TYPE) destination_ptr, (ALIGN_TYPE) wait_option); + + /* Return value to the caller. */ + return(return_value); +} +#endif diff --git a/common_modules/module_manager/inc/txm_module_manager_dispatch.h b/common_modules/module_manager/inc/txm_module_manager_dispatch.h index cc9dac2f2..a409a77ea 100644 --- a/common_modules/module_manager/inc/txm_module_manager_dispatch.h +++ b/common_modules/module_manager/inc/txm_module_manager_dispatch.h @@ -1626,6 +1626,45 @@ TX_QUEUE *queue_ptr; } #endif +#ifndef TXM_QUEUE_PEEK_CALL_NOT_USED +/* UINT _txe_queue_peek( + TX_QUEUE *queue_ptr, -> param_0 + VOID *destination_ptr, -> param_1 + ULONG wait_option -> param_2 + ); */ +static ALIGN_TYPE _txm_module_manager_tx_queue_peek_dispatch(TXM_MODULE_INSTANCE *module_instance, ALIGN_TYPE param_0, ALIGN_TYPE param_1, ALIGN_TYPE param_2) +{ + +ALIGN_TYPE return_value; +TX_QUEUE *queue_ptr; + + if (module_instance -> txm_module_instance_property_flags & TXM_MODULE_MEMORY_PROTECTION) + { + if (!TXM_MODULE_MANAGER_PARAM_CHECK_TYPED_OBJECT_FOR_USE(module_instance, param_0, TXM_QUEUE_OBJECT)) + return(TXM_MODULE_INVALID_MEMORY); + + /* A module peeks only at its own callback request queue. */ + if ((TXM_MODULE_MANAGER_CALLBACK_REQUEST_QUEUE(param_0)) && + (((TX_QUEUE *) param_0) != &(module_instance -> txm_module_instance_callback_request_queue))) + { + return(TXM_MODULE_INVALID_MEMORY); + } + + /* We need to get the max size of the buffer from the queue. */ + queue_ptr = (TX_QUEUE *) param_0; + if (!TXM_MODULE_MANAGER_PARAM_CHECK_BUFFER_WRITE(module_instance, param_1, sizeof(ULONG)*queue_ptr -> tx_queue_message_size)) + return(TXM_MODULE_INVALID_MEMORY); + } + + return_value = (ALIGN_TYPE) _txe_queue_peek( + (TX_QUEUE *) param_0, + (VOID *) param_1, + (ULONG) param_2 + ); + return(return_value); +} +#endif + #ifndef TXM_QUEUE_SEND_CALL_NOT_USED /* UINT _txe_queue_send( TX_QUEUE *queue_ptr, -> param_0 diff --git a/common_modules/module_manager/src/txm_module_manager_kernel_dispatch.c b/common_modules/module_manager/src/txm_module_manager_kernel_dispatch.c index aa377430d..f6c36879c 100644 --- a/common_modules/module_manager/src/txm_module_manager_kernel_dispatch.c +++ b/common_modules/module_manager/src/txm_module_manager_kernel_dispatch.c @@ -425,6 +425,14 @@ TXM_MODULE_INSTANCE *module_instance; } #endif + #ifndef TXM_QUEUE_PEEK_CALL_NOT_USED + case TXM_QUEUE_PEEK_CALL: + { + return_value = _txm_module_manager_tx_queue_peek_dispatch(module_instance, param_0, param_1, param_2); + break; + } + #endif + #ifndef TXM_QUEUE_SEND_CALL_NOT_USED case TXM_QUEUE_SEND_CALL: { diff --git a/common_smp/CMakeLists.txt b/common_smp/CMakeLists.txt index 80b9d3154..b4ad722be 100644 --- a/common_smp/CMakeLists.txt +++ b/common_smp/CMakeLists.txt @@ -64,6 +64,9 @@ target_sources(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_delete.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_flush.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_front_send.c + ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_peek.c + ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_peek_release.c + ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_peek_resume.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_info_get.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_initialize.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_queue_performance_info_get.c @@ -175,6 +178,7 @@ target_sources(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_delete.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_flush.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_front_send.c + ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_peek.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_info_get.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_prioritize.c ${CMAKE_CURRENT_LIST_DIR}/src/txe_queue_receive.c diff --git a/common_smp/inc/tx_api.h b/common_smp/inc/tx_api.h index 77bd6218b..97bfa7332 100644 --- a/common_smp/inc/tx_api.h +++ b/common_smp/inc/tx_api.h @@ -1223,6 +1223,7 @@ UINT _tx_el_interrupt_control(UINT new_posture); #define tx_queue_send _tx_queue_send #define tx_queue_send_notify _tx_queue_send_notify #define tx_queue_front_send _tx_queue_front_send +#define tx_queue_peek _tx_queue_peek #define tx_queue_prioritize _tx_queue_prioritize #define tx_semaphore_ceiling_put _tx_semaphore_ceiling_put @@ -1351,6 +1352,7 @@ UINT _tx_el_interrupt_control(UINT new_posture); #define tx_queue_send _txr_queue_send #define tx_queue_send_notify _txr_queue_send_notify #define tx_queue_front_send _txr_queue_front_send +#define tx_queue_peek _txr_queue_peek #define tx_queue_prioritize _txr_queue_prioritize #define tx_semaphore_ceiling_put _txr_semaphore_ceiling_put @@ -1468,6 +1470,7 @@ UINT _tx_el_interrupt_control(UINT new_posture); #define tx_queue_send _txe_queue_send #define tx_queue_send_notify _txe_queue_send_notify #define tx_queue_front_send _txe_queue_front_send +#define tx_queue_peek _txe_queue_peek #define tx_queue_prioritize _txe_queue_prioritize #define tx_semaphore_ceiling_put _txe_semaphore_ceiling_put @@ -1688,6 +1691,7 @@ UINT _tx_queue_receive(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG UINT _tx_queue_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); UINT _tx_queue_send_notify(TX_QUEUE *queue_ptr, VOID (*queue_send_notify)(TX_QUEUE *notify_queue_ptr)); UINT _tx_queue_front_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); +UINT _tx_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option); /* Define error checking shells for API services. These are only referenced by the @@ -1704,6 +1708,7 @@ UINT _txe_queue_receive(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG UINT _txe_queue_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); UINT _txe_queue_send_notify(TX_QUEUE *queue_ptr, VOID (*queue_send_notify)(TX_QUEUE *notify_queue_ptr)); UINT _txe_queue_front_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); +UINT _txe_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option); /* Define semaphore management function prototypes. */ diff --git a/common_smp/inc/tx_queue.h b/common_smp/inc/tx_queue.h index c8b4122c5..3128fca90 100644 --- a/common_smp/inc/tx_queue.h +++ b/common_smp/inc/tx_queue.h @@ -48,6 +48,11 @@ #define TX_QUEUE_ID ((ULONG) 0x51554555) +/* Value of tx_thread_suspend_option for a thread suspended in tx_queue_peek. */ + +#define TX_QUEUE_PEEK_SUSPEND ((UINT) 2) + + /* Determine if in-line component initialization is supported by the caller. */ #ifdef TX_INVOKE_INLINE_INITIALIZATION @@ -96,6 +101,8 @@ VOID _tx_queue_initialize(VOID); /* Define internal queue management function prototypes. */ VOID _tx_queue_cleanup(TX_THREAD *thread_ptr, ULONG suspension_sequence); +TX_THREAD *_tx_queue_peek_release(TX_QUEUE *queue_ptr, VOID *source_ptr); +VOID _tx_queue_peek_resume(TX_THREAD *thread_list); /* Queue management component data declarations follow. */ diff --git a/common_smp/inc/tx_trace.h b/common_smp/inc/tx_trace.h index 2f4447d40..5202e89a4 100644 --- a/common_smp/inc/tx_trace.h +++ b/common_smp/inc/tx_trace.h @@ -305,6 +305,7 @@ typedef struct TX_TRACE_OBJECT_ENTRY_STRUCT #define TX_TRACE_QUEUE_RECEIVE 68 /* I1 = queue ptr, I2 = destination ptr, I3 = wait option, I4 = enqueued */ #define TX_TRACE_QUEUE_SEND 69 /* I1 = queue ptr, I2 = source ptr, I3 = wait option, I4 = enqueued */ #define TX_TRACE_QUEUE_SEND_NOTIFY 70 /* I1 = queue ptr */ +#define TX_TRACE_QUEUE_PEEK 71 /* I1 = queue ptr, I2 = destination ptr, I3 = wait option, I4 = enqueued */ #define TX_TRACE_SEMAPHORE_CEILING_PUT 80 /* I1 = semaphore ptr, I2 = current count, I3 = suspended count,I4 =ceiling */ #define TX_TRACE_SEMAPHORE_CREATE 81 /* I1 = semaphore ptr, I2 = initial count, I3 = stack ptr */ #define TX_TRACE_SEMAPHORE_DELETE 82 /* I1 = semaphore ptr, I2 = stack ptr */ diff --git a/common_smp/src/tx_queue_front_send.c b/common_smp/src/tx_queue_front_send.c index bb39b970b..dae59fd17 100644 --- a/common_smp/src/tx_queue_front_send.c +++ b/common_smp/src/tx_queue_front_send.c @@ -82,6 +82,7 @@ UINT suspended_count; TX_THREAD *next_thread; TX_THREAD *previous_thread; UINT status; +TX_THREAD *peek_list; #ifndef TX_DISABLE_NOTIFY_CALLBACKS VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); #endif @@ -115,6 +116,15 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); if (queue_ptr -> tx_queue_available_storage != ((UINT) 0)) { + /* Peeking threads get a copy; the message is still delivered below. */ + peek_list = TX_NULL; + if (suspended_count != TX_NO_SUSPENSIONS) + { + + peek_list = _tx_queue_peek_release(queue_ptr, source_ptr); + suspended_count = queue_ptr -> tx_queue_suspended_count; + } + /* Yes there is room in the queue. Now determine if there is a thread waiting for a message. */ if (suspended_count == TX_NO_SUSPENSIONS) @@ -167,6 +177,12 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); /* Restore interrupts. */ TX_RESTORE + if (peek_list != TX_NULL) + { + + _tx_queue_peek_resume(peek_list); + } + #ifndef TX_DISABLE_NOTIFY_CALLBACKS /* Determine if a notify callback is required. */ @@ -259,6 +275,12 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); _tx_thread_system_resume(thread_ptr); #endif + if (peek_list != TX_NULL) + { + + _tx_queue_peek_resume(peek_list); + } + #ifndef TX_DISABLE_NOTIFY_CALLBACKS /* Determine if a notify callback is required. */ diff --git a/common_smp/src/tx_queue_peek.c b/common_smp/src/tx_queue_peek.c new file mode 100644 index 000000000..f043b7891 --- /dev/null +++ b/common_smp/src/tx_queue_peek.c @@ -0,0 +1,237 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Queue */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE + + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_trace.h" +#include "tx_thread.h" +#include "tx_queue.h" + + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_queue_peek PORTABLE C */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function copies the message at the front of the specified */ +/* queue without removing it. The message is the one the next */ +/* tx_queue_receive would return. If the queue is empty, this */ +/* function waits according to the option specified; a waiting thread */ +/* is handed a copy of the next message sent to the queue, and that */ +/* message stays in the queue. */ +/* */ +/* INPUT */ +/* */ +/* queue_ptr Pointer to queue control block */ +/* destination_ptr Pointer to message destination */ +/* **** MUST BE LARGE ENOUGH TO */ +/* HOLD MESSAGE **** */ +/* wait_option Suspension option */ +/* */ +/* OUTPUT */ +/* */ +/* status Completion status */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_system_suspend Suspend thread routine */ +/* _tx_thread_system_ni_suspend Non-interruptable suspend thread */ +/* */ +/* CALLED BY */ +/* */ +/* Application Code */ +/* */ +/**************************************************************************/ +UINT _tx_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option) +{ + +TX_INTERRUPT_SAVE_AREA + +TX_THREAD *thread_ptr; +ULONG *source; +ULONG *destination; +UINT size; +UINT suspended_count; +TX_THREAD *next_thread; +TX_THREAD *previous_thread; +UINT status; + + + /* Default the status to TX_SUCCESS. */ + status = TX_SUCCESS; + + /* Disable interrupts to peek at the queue. */ + TX_DISABLE + + /* If trace is enabled, insert this event into the trace buffer. */ + TX_TRACE_IN_LINE_INSERT(TX_TRACE_QUEUE_PEEK, queue_ptr, TX_POINTER_TO_ULONG_CONVERT(destination_ptr), wait_option, queue_ptr -> tx_queue_enqueued, TX_TRACE_QUEUE_EVENTS) + + /* Pickup the thread suspension count. */ + suspended_count = queue_ptr -> tx_queue_suspended_count; + + /* Determine if there is anything in the queue. */ + if (queue_ptr -> tx_queue_enqueued != TX_NO_MESSAGES) + { + + /* Default to the message at the read pointer. */ + source = queue_ptr -> tx_queue_read; + + /* Threads suspended on a non-empty queue are senders waiting on a full queue. + A front send suspension at the head is what tx_queue_receive returns next. */ + if (suspended_count != TX_NO_SUSPENSIONS) + { + + thread_ptr = queue_ptr -> tx_queue_suspension_list; + if (thread_ptr -> tx_thread_suspend_option == TX_TRUE) + { + + source = TX_VOID_TO_ULONG_POINTER_CONVERT(thread_ptr -> tx_thread_additional_suspend_info); + } + } + + /* Setup destination pointer and size. */ + destination = TX_VOID_TO_ULONG_POINTER_CONVERT(destination_ptr); + size = queue_ptr -> tx_queue_message_size; + + /* Copy message. Note that the source and destination pointers are + incremented by the macro. */ + TX_QUEUE_MESSAGE_COPY(source, destination, size) + + /* Restore interrupts. */ + TX_RESTORE + } + + /* Determine if the request specifies suspension. */ + else if (wait_option != TX_NO_WAIT) + { + + /* Determine if the preempt disable flag is non-zero. */ + if (_tx_thread_preempt_disable != ((UINT) 0)) + { + + /* Restore interrupts. */ + TX_RESTORE + + /* Suspension is not allowed if the preempt disable flag is non-zero at this point - return error completion. */ + status = TX_QUEUE_EMPTY; + } + else + { + + /* Prepare for suspension of this thread. */ + + /* Pickup thread pointer. */ + TX_THREAD_GET_CURRENT(thread_ptr) + + /* Setup cleanup routine pointer. */ + thread_ptr -> tx_thread_suspend_cleanup = &(_tx_queue_cleanup); + + /* Setup cleanup information, i.e. this queue control + block and the destination pointer. */ + thread_ptr -> tx_thread_suspend_control_block = (VOID *) queue_ptr; + thread_ptr -> tx_thread_additional_suspend_info = (VOID *) destination_ptr; + thread_ptr -> tx_thread_suspend_option = TX_QUEUE_PEEK_SUSPEND; + +#ifndef TX_NOT_INTERRUPTABLE + + /* Increment the suspension sequence number, which is used to identify + this suspension event. */ + thread_ptr -> tx_thread_suspension_sequence++; +#endif + + /* Setup suspension list. */ + if (suspended_count == TX_NO_SUSPENSIONS) + { + + /* No other threads are suspended. Setup the head pointer and + just setup this threads pointers to itself. */ + queue_ptr -> tx_queue_suspension_list = thread_ptr; + thread_ptr -> tx_thread_suspended_next = thread_ptr; + thread_ptr -> tx_thread_suspended_previous = thread_ptr; + } + else + { + + /* This list is not NULL, add current thread to the end. */ + next_thread = queue_ptr -> tx_queue_suspension_list; + thread_ptr -> tx_thread_suspended_next = next_thread; + previous_thread = next_thread -> tx_thread_suspended_previous; + thread_ptr -> tx_thread_suspended_previous = previous_thread; + previous_thread -> tx_thread_suspended_next = thread_ptr; + next_thread -> tx_thread_suspended_previous = thread_ptr; + } + + /* Increment the suspended thread count. */ + queue_ptr -> tx_queue_suspended_count = suspended_count + ((UINT) 1); + + /* Set the state to suspended. */ + thread_ptr -> tx_thread_state = TX_QUEUE_SUSP; + +#ifdef TX_NOT_INTERRUPTABLE + + /* Call actual non-interruptable thread suspension routine. */ + _tx_thread_system_ni_suspend(thread_ptr, wait_option); + + /* Restore interrupts. */ + TX_RESTORE +#else + + /* Set the suspending flag. */ + thread_ptr -> tx_thread_suspending = TX_TRUE; + + /* Setup the timeout period. */ + thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks = wait_option; + + /* Temporarily disable preemption. */ + _tx_thread_preempt_disable++; + + /* Restore interrupts. */ + TX_RESTORE + + /* Call actual thread suspension routine. */ + _tx_thread_system_suspend(thread_ptr); +#endif + + /* Return the completion status. */ + status = thread_ptr -> tx_thread_suspend_status; + } + } + else + { + + /* Restore interrupts. */ + TX_RESTORE + + /* Immediate return, return error completion. */ + status = TX_QUEUE_EMPTY; + } + + /* Return completion status. */ + return(status); +} + diff --git a/common_smp/src/tx_queue_peek_release.c b/common_smp/src/tx_queue_peek_release.c new file mode 100644 index 000000000..95ed931ac --- /dev/null +++ b/common_smp/src/tx_queue_peek_release.c @@ -0,0 +1,155 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Queue */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE + + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_thread.h" +#include "tx_queue.h" + + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_queue_peek_release PORTABLE C */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function copies the message being sent to an empty queue into */ +/* every thread suspended in tx_queue_peek at the head of the queue's */ +/* suspension list, and removes those threads from the list. The */ +/* removed threads are returned as a list linked through */ +/* tx_thread_suspended_next, ready for _tx_queue_peek_resume. */ +/* */ +/* Interrupts must be disabled by the caller. If any thread is */ +/* released, preemption is disabled until _tx_queue_peek_resume. */ +/* */ +/* INPUT */ +/* */ +/* queue_ptr Pointer to queue control block */ +/* source_ptr Pointer to message being sent */ +/* */ +/* OUTPUT */ +/* */ +/* thread_list Released threads, or TX_NULL */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_queue_send Queue send function */ +/* _tx_queue_front_send Queue send to front function */ +/* */ +/**************************************************************************/ +TX_THREAD *_tx_queue_peek_release(TX_QUEUE *queue_ptr, VOID *source_ptr) +{ + +TX_THREAD *thread_ptr; +TX_THREAD *next_thread; +TX_THREAD *previous_thread; +TX_THREAD *released_list; +TX_THREAD *last_released; +ULONG *source; +ULONG *destination; +UINT size; +UINT suspended_count; + + + released_list = TX_NULL; + last_released = TX_NULL; + suspended_count = queue_ptr -> tx_queue_suspended_count; + + while (suspended_count != TX_NO_SUSPENSIONS) + { + + /* Stop at the first thread that is not peeking. */ + thread_ptr = queue_ptr -> tx_queue_suspension_list; + if (thread_ptr -> tx_thread_suspend_option != TX_QUEUE_PEEK_SUSPEND) + { + break; + } + + /* Remove the thread from the head of the suspension list. */ + suspended_count--; + if (suspended_count == TX_NO_SUSPENSIONS) + { + + queue_ptr -> tx_queue_suspension_list = TX_NULL; + } + else + { + + next_thread = thread_ptr -> tx_thread_suspended_next; + queue_ptr -> tx_queue_suspension_list = next_thread; + previous_thread = thread_ptr -> tx_thread_suspended_previous; + next_thread -> tx_thread_suspended_previous = previous_thread; + previous_thread -> tx_thread_suspended_next = next_thread; + } + + /* Clear cleanup routine to avoid timeout. */ + thread_ptr -> tx_thread_suspend_cleanup = TX_NULL; + + /* Setup source and destination pointers. */ + source = TX_VOID_TO_ULONG_POINTER_CONVERT(source_ptr); + destination = TX_VOID_TO_ULONG_POINTER_CONVERT(thread_ptr -> tx_thread_additional_suspend_info); + size = queue_ptr -> tx_queue_message_size; + + /* Copy message. Note that the source and destination pointers are + incremented by the macro. */ + TX_QUEUE_MESSAGE_COPY(source, destination, size) + + /* Put return status into the thread control block. */ + thread_ptr -> tx_thread_suspend_status = TX_SUCCESS; + + /* Append the thread to the released list. */ + thread_ptr -> tx_thread_suspended_next = TX_NULL; + if (released_list == TX_NULL) + { + + released_list = thread_ptr; + } + else + { + + last_released -> tx_thread_suspended_next = thread_ptr; + } + last_released = thread_ptr; + } + + /* Update the suspension count. */ + queue_ptr -> tx_queue_suspended_count = suspended_count; + + /* Keep released threads from being preempted by each other until all are resumed. */ + if (released_list != TX_NULL) + { + + _tx_thread_preempt_disable++; + } + + return(released_list); +} + diff --git a/common_smp/src/tx_queue_peek_resume.c b/common_smp/src/tx_queue_peek_resume.c new file mode 100644 index 000000000..810166d27 --- /dev/null +++ b/common_smp/src/tx_queue_peek_resume.c @@ -0,0 +1,113 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Queue */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE + + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_thread.h" +#include "tx_queue.h" + + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_queue_peek_resume PORTABLE C */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function resumes the threads returned by */ +/* _tx_queue_peek_release, releases the preemption disable it took, */ +/* and checks for preemption. Interrupts must be enabled by the caller.*/ +/* */ +/* INPUT */ +/* */ +/* thread_list Released threads (not TX_NULL) */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_system_resume Resume thread routine */ +/* _tx_thread_system_ni_resume Non-interruptable resume thread */ +/* _tx_thread_system_preempt_check Check for preemption */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_queue_send Queue send function */ +/* _tx_queue_front_send Queue send to front function */ +/* */ +/**************************************************************************/ +VOID _tx_queue_peek_resume(TX_THREAD *thread_list) +{ + +TX_INTERRUPT_SAVE_AREA + +TX_THREAD *thread_ptr; +TX_THREAD *next_thread; + + + thread_ptr = thread_list; + while (thread_ptr != TX_NULL) + { + + /* Get next pointer first, resume reuses the link. */ + next_thread = thread_ptr -> tx_thread_suspended_next; + + /* Disable interrupts. */ + TX_DISABLE + +#ifdef TX_NOT_INTERRUPTABLE + + /* Resume the thread! */ + _tx_thread_system_ni_resume(thread_ptr); + + /* Restore interrupts. */ + TX_RESTORE +#else + + /* Temporarily disable preemption. */ + _tx_thread_preempt_disable++; + + /* Restore interrupts. */ + TX_RESTORE + + /* Resume thread. */ + _tx_thread_system_resume(thread_ptr); +#endif + + thread_ptr = next_thread; + } + + /* Release the preemption disable taken by _tx_queue_peek_release. */ + TX_DISABLE + _tx_thread_preempt_disable--; + TX_RESTORE + + /* Check for preemption. */ + _tx_thread_system_preempt_check(); +} + diff --git a/common_smp/src/tx_queue_send.c b/common_smp/src/tx_queue_send.c index 7d22f5b9c..be1b759f0 100644 --- a/common_smp/src/tx_queue_send.c +++ b/common_smp/src/tx_queue_send.c @@ -82,6 +82,7 @@ UINT suspended_count; TX_THREAD *next_thread; TX_THREAD *previous_thread; UINT status; +TX_THREAD *peek_list; #ifndef TX_DISABLE_NOTIFY_CALLBACKS VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); #endif @@ -117,6 +118,15 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); /* There is room for the message in the queue. */ + /* Peeking threads get a copy; the message is still delivered below. */ + peek_list = TX_NULL; + if (suspended_count != TX_NO_SUSPENSIONS) + { + + peek_list = _tx_queue_peek_release(queue_ptr, source_ptr); + suspended_count = queue_ptr -> tx_queue_suspended_count; + } + /* Determine if there are suspended on this queue. */ if (suspended_count == TX_NO_SUSPENSIONS) { @@ -160,6 +170,12 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); /* Restore interrupts. */ TX_RESTORE + if (peek_list != TX_NULL) + { + + _tx_queue_peek_resume(peek_list); + } + #ifndef TX_DISABLE_NOTIFY_CALLBACKS /* Determine if a notify callback is required. */ @@ -254,6 +270,12 @@ VOID (*queue_send_notify)(struct TX_QUEUE_STRUCT *notify_queue_ptr); _tx_thread_system_resume(thread_ptr); #endif + if (peek_list != TX_NULL) + { + + _tx_queue_peek_resume(peek_list); + } + #ifndef TX_DISABLE_NOTIFY_CALLBACKS /* Determine if a notify callback is required. */ diff --git a/common_smp/src/txe_queue_peek.c b/common_smp/src/txe_queue_peek.c new file mode 100644 index 000000000..9d17f60b7 --- /dev/null +++ b/common_smp/src/txe_queue_peek.c @@ -0,0 +1,149 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Queue */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE + + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_timer.h" +#include "tx_thread.h" +#include "tx_queue.h" + + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _txe_queue_peek PORTABLE C */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function checks for errors in the queue peek function call. */ +/* */ +/* INPUT */ +/* */ +/* queue_ptr Pointer to queue control block */ +/* destination_ptr Pointer to message destination */ +/* **** MUST BE LARGE ENOUGH TO */ +/* HOLD MESSAGE **** */ +/* wait_option Suspension option */ +/* */ +/* OUTPUT */ +/* */ +/* TX_QUEUE_ERROR Invalid queue pointer */ +/* TX_PTR_ERROR Invalid destination pointer (NULL)*/ +/* TX_WAIT_ERROR Invalid wait option */ +/* status Actual completion status */ +/* */ +/* CALLS */ +/* */ +/* _tx_queue_peek Actual queue peek function */ +/* */ +/* CALLED BY */ +/* */ +/* Application Code */ +/* */ +/**************************************************************************/ +UINT _txe_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option) +{ + +UINT status; + +#ifndef TX_TIMER_PROCESS_IN_ISR +TX_THREAD *current_thread; +#endif + + + /* Default status to success. */ + status = TX_SUCCESS; + + /* Check for an invalid queue pointer. */ + if (queue_ptr == TX_NULL) + { + + /* Queue pointer is invalid, return appropriate error code. */ + status = TX_QUEUE_ERROR; + } + + /* Now check for invalid queue ID. */ + else if (queue_ptr -> tx_queue_id != TX_QUEUE_ID) + { + + /* Queue pointer is invalid, return appropriate error code. */ + status = TX_QUEUE_ERROR; + } + + /* Check for an invalid destination for message. */ + else if (destination_ptr == TX_NULL) + { + + /* Null destination pointer, return appropriate error. */ + status = TX_PTR_ERROR; + } + else + { + + /* Check for a wait option error. Only threads are allowed any form of + suspension. */ + if (wait_option != TX_NO_WAIT) + { + + /* Is the call from an ISR or Initialization? */ + if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0)) + { + + /* A non-thread is trying to suspend, return appropriate error code. */ + status = TX_WAIT_ERROR; + } + +#ifndef TX_TIMER_PROCESS_IN_ISR + else + { + + /* Pickup thread pointer. */ + TX_THREAD_GET_CURRENT(current_thread) + + /* Is the current thread the timer thread? */ + if (current_thread == &_tx_timer_thread) + { + + /* A non-thread is trying to suspend, return appropriate error code. */ + status = TX_WAIT_ERROR; + } + } +#endif + } + } + + /* Determine if everything is okay. */ + if (status == TX_SUCCESS) + { + + /* Call actual queue peek function. */ + status = _tx_queue_peek(queue_ptr, destination_ptr, wait_option); + } + + /* Return completion status. */ + return(status); +} + diff --git a/test/smp/cmake/regression/CMakeLists.txt b/test/smp/cmake/regression/CMakeLists.txt index cdf0c5859..c6abb1f4d 100644 --- a/test/smp/cmake/regression/CMakeLists.txt +++ b/test/smp/cmake/regression/CMakeLists.txt @@ -66,6 +66,7 @@ set(regression_test_cases ${SOURCE_DIR}/threadx_queue_front_send_test.c ${SOURCE_DIR}/threadx_queue_full_suspension_test.c ${SOURCE_DIR}/threadx_queue_information_test.c + ${SOURCE_DIR}/threadx_queue_peek_test.c ${SOURCE_DIR}/threadx_queue_prioritize.c ${SOURCE_DIR}/threadx_queue_suspension_timeout_test.c ${SOURCE_DIR}/threadx_queue_thread_terminate_test.c diff --git a/test/smp/cmake/threadx_smp/common_smp/CMakeLists.txt b/test/smp/cmake/threadx_smp/common_smp/CMakeLists.txt index 770379af0..fa758d2ad 100644 --- a/test/smp/cmake/threadx_smp/common_smp/CMakeLists.txt +++ b/test/smp/cmake/threadx_smp/common_smp/CMakeLists.txt @@ -65,6 +65,7 @@ target_sources(${PROJECT_NAME} ${CURRENT_DIR}/src/txe_queue_delete.c ${CURRENT_DIR}/src/txe_queue_flush.c ${CURRENT_DIR}/src/txe_queue_front_send.c + ${CURRENT_DIR}/src/txe_queue_peek.c ${CURRENT_DIR}/src/txe_queue_info_get.c ${CURRENT_DIR}/src/txe_queue_prioritize.c ${CURRENT_DIR}/src/txe_queue_receive.c @@ -127,6 +128,9 @@ target_sources(${PROJECT_NAME} ${CURRENT_DIR}/src/tx_queue_delete.c ${CURRENT_DIR}/src/tx_queue_flush.c ${CURRENT_DIR}/src/tx_queue_front_send.c + ${CURRENT_DIR}/src/tx_queue_peek.c + ${CURRENT_DIR}/src/tx_queue_peek_release.c + ${CURRENT_DIR}/src/tx_queue_peek_resume.c ${CURRENT_DIR}/src/tx_queue_info_get.c ${CURRENT_DIR}/src/tx_queue_initialize.c ${CURRENT_DIR}/src/tx_queue_performance_info_get.c diff --git a/test/smp/regression/testcontrol.c b/test/smp/regression/testcontrol.c index c964dc03a..01bbbe463 100644 --- a/test/smp/regression/testcontrol.c +++ b/test/smp/regression/testcontrol.c @@ -221,6 +221,7 @@ void threadx_queue_flush_no_suspension_application_define(void *); void threadx_queue_front_send_application_define(void *); void threadx_queue_prioritize_application_define(void *); void threadx_queue_information_application_define(void *); +void threadx_queue_peek_application_define(void *); void threadx_semaphore_basic_application_define(void *); void threadx_semaphore_delete_application_define(void *); @@ -368,6 +369,7 @@ TEST_ENTRY test_control_tests[] = threadx_queue_front_send_application_define, threadx_queue_prioritize_application_define, threadx_queue_information_application_define, + threadx_queue_peek_application_define, threadx_semaphore_basic_application_define, threadx_semaphore_delete_application_define, diff --git a/test/smp/regression/threadx_queue_peek_test.c b/test/smp/regression/threadx_queue_peek_test.c new file mode 100644 index 000000000..a6e9c47b7 --- /dev/null +++ b/test/smp/regression/threadx_queue_peek_test.c @@ -0,0 +1,341 @@ +/***************************************************************************/ +/* Copyright (c) 2026 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 */ +/***************************************************************************/ + +/* This test is designed to test the queue peek service, both immediate and + with suspension, and its interaction with send, front send and receive. */ + +#include +#include "tx_api.h" + +static TX_THREAD thread_0; +static TX_THREAD peek_thread_1; +static TX_THREAD peek_thread_2; +static TX_THREAD receive_thread; +static TX_THREAD front_send_thread; + +static TX_QUEUE queue_0; + +static volatile ULONG peek_1_count; +static volatile UINT peek_1_status; +static ULONG peek_1_message[2]; + +static volatile ULONG peek_2_count; +static volatile UINT peek_2_status; +static ULONG peek_2_message[2]; + +static volatile ULONG receive_count; +static ULONG receive_message[2]; + +static volatile ULONG front_send_count; +static ULONG front_send_message[2] = {0xF0F0F0F0, 0x0F0F0F0F}; + + +/* Define thread prototypes. */ + +static void thread_0_entry(ULONG thread_input); +static void peek_thread_1_entry(ULONG thread_input); +static void peek_thread_2_entry(ULONG thread_input); +static void receive_thread_entry(ULONG thread_input); +static void front_send_thread_entry(ULONG thread_input); + + +/* Prototype for test control return. */ + +void test_control_return(UINT status); + + +static ULONG queue_enqueued(void) +{ + +ULONG enqueued; + + + tx_queue_info_get(&queue_0, TX_NULL, &enqueued, TX_NULL, TX_NULL, TX_NULL, TX_NULL); + return(enqueued); +} + + +static void test_error(UINT error_number) +{ + + printf("ERROR #%u\n", error_number); + test_control_return(1); +} + + +/* Define what the initial system looks like. */ + +#ifdef CTEST +void test_application_define(void *first_unused_memory) +#else +void threadx_queue_peek_application_define(void *first_unused_memory) +#endif +{ + +UINT status; +CHAR *pointer; + + + /* Put first available memory address into a character pointer. */ + pointer = (CHAR *) first_unused_memory; + + peek_1_count = 0; + peek_2_count = 0; + receive_count = 0; + front_send_count = 0; + + status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 16, 16, 100, TX_AUTO_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + status += tx_thread_create(&peek_thread_1, "peek thread 1", peek_thread_1_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 15, 15, 100, TX_DONT_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + status += tx_thread_create(&peek_thread_2, "peek thread 2", peek_thread_2_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 15, 15, 100, TX_DONT_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + status += tx_thread_create(&receive_thread, "receive thread", receive_thread_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 15, 15, 100, TX_DONT_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + status += tx_thread_create(&front_send_thread, "front send thread", front_send_thread_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 15, 15, 100, TX_DONT_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + /* Two messages of two words each. */ + status += tx_queue_create(&queue_0, "queue 0", TX_2_ULONG, pointer, 2*2*sizeof(ULONG)); + pointer = pointer + 2*2*sizeof(ULONG); + + if (status != TX_SUCCESS) + { + + printf("Running Queue Peek Test............................................. ERROR #1\n"); + test_control_return(1); + } +} + + +/* Define the test threads. */ + +static void thread_0_entry(ULONG thread_input) +{ + +UINT status; +ULONG message_a[2] = {0x11111111, 0xAAAAAAAA}; +ULONG message_b[2] = {0x22222222, 0xBBBBBBBB}; +ULONG message_c[2] = {0x33333333, 0xCCCCCCCC}; +ULONG message[2]; +#ifndef TX_DISABLE_ERROR_CHECKING +TX_QUEUE not_a_queue = {0}; +#endif + + + /* Inform user. */ + printf("Running Queue Peek Test............................................. "); + +#ifndef TX_DISABLE_ERROR_CHECKING + + if (tx_queue_peek(TX_NULL, message, TX_NO_WAIT) != TX_QUEUE_ERROR) + test_error(2); + + if (tx_queue_peek(¬_a_queue, message, TX_NO_WAIT) != TX_QUEUE_ERROR) + test_error(3); + + if (tx_queue_peek(&queue_0, TX_NULL, TX_NO_WAIT) != TX_PTR_ERROR) + test_error(4); +#endif + + /* Empty queue without wait. */ + if (tx_queue_peek(&queue_0, message, TX_NO_WAIT) != TX_QUEUE_EMPTY) + test_error(5); + + /* Empty queue with timeout. */ + if (tx_queue_peek(&queue_0, message, 2) != TX_QUEUE_EMPTY) + test_error(6); + + /* Peek returns the front message and leaves it in the queue. */ + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + tx_queue_send(&queue_0, message_b, TX_NO_WAIT); + message[0] = 0; + message[1] = 0; + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0]) || (message[1] != message_a[1]) || (queue_enqueued() != 2)) + test_error(7); + + /* Peeking twice returns the same message. */ + status = tx_queue_peek(&queue_0, message, TX_WAIT_FOREVER); + if ((status != TX_SUCCESS) || (message[0] != message_a[0]) || (queue_enqueued() != 2)) + test_error(8); + + /* Receive gets the peeked message, and peek then sees the next one. */ + status = tx_queue_receive(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0])) + test_error(9); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_b[0]) || (message[1] != message_b[1])) + test_error(10); + + /* Peek follows the read pointer across the wrap of the queue area. */ + tx_queue_send(&queue_0, message_c, TX_NO_WAIT); + tx_queue_receive(&queue_0, message, TX_NO_WAIT); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_c[0]) || (message[1] != message_c[1])) + test_error(11); + tx_queue_flush(&queue_0); + + /* A front send suspended on a full queue is what receive returns next, so peek returns it too. */ + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + tx_queue_send(&queue_0, message_b, TX_NO_WAIT); + tx_thread_resume(&front_send_thread); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != front_send_message[0]) || (message[1] != front_send_message[1])) + test_error(12); + status = tx_queue_receive(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != front_send_message[0]) || (front_send_count != 1)) + test_error(13); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0])) + test_error(14); + tx_queue_flush(&queue_0); + + /* A suspended peeker is handed a copy of the next message, which stays queued. */ + tx_thread_resume(&peek_thread_1); + if (peek_1_count != 0) + test_error(15); + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + if ((peek_1_count != 1) || (peek_1_status != TX_SUCCESS) || (peek_1_message[0] != message_a[0]) || + (peek_1_message[1] != message_a[1]) || (queue_enqueued() != 1)) + test_error(16); + tx_queue_flush(&queue_0); + + /* Every peeker at the head of the suspension list gets the message. */ + tx_thread_resume(&peek_thread_1); + tx_thread_resume(&peek_thread_2); + tx_queue_send(&queue_0, message_b, TX_NO_WAIT); + if ((peek_1_count != 2) || (peek_1_message[0] != message_b[0]) || + (peek_2_count != 1) || (peek_2_status != TX_SUCCESS) || (peek_2_message[0] != message_b[0]) || + (queue_enqueued() != 1)) + test_error(17); + tx_queue_flush(&queue_0); + + /* Peeker ahead of a receiver: both get the message and the receiver consumes it. */ + tx_thread_resume(&peek_thread_1); + tx_thread_resume(&receive_thread); + tx_queue_send(&queue_0, message_c, TX_NO_WAIT); + if ((peek_1_count != 3) || (peek_1_message[0] != message_c[0]) || + (receive_count != 1) || (receive_message[0] != message_c[0]) || (receive_message[1] != message_c[1]) || + (queue_enqueued() != 0)) + test_error(18); + + /* Receiver ahead of a peeker: the receiver consumes the message and the peeker keeps waiting. */ + tx_thread_resume(&receive_thread); + tx_thread_resume(&peek_thread_1); + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + if ((receive_count != 2) || (receive_message[0] != message_a[0]) || (peek_1_count != 3) || (queue_enqueued() != 0)) + test_error(19); + tx_queue_send(&queue_0, message_b, TX_NO_WAIT); + if ((peek_1_count != 4) || (peek_1_message[0] != message_b[0]) || (queue_enqueued() != 1)) + test_error(20); + tx_queue_flush(&queue_0); + + /* Front send also hands the message to a suspended peeker. */ + tx_thread_resume(&peek_thread_1); + status = tx_queue_front_send(&queue_0, message_c, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (peek_1_count != 5) || (peek_1_message[0] != message_c[0]) || (queue_enqueued() != 1)) + test_error(21); + status = tx_queue_receive(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_c[0])) + test_error(22); + + /* An aborted peek is removed from the queue's suspension list. */ + tx_thread_resume(&peek_thread_1); + tx_thread_wait_abort(&peek_thread_1); + if ((peek_1_count != 6) || (peek_1_status != TX_WAIT_ABORTED)) + test_error(23); + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + if ((peek_1_count != 6) || (queue_enqueued() != 1)) + test_error(24); + tx_queue_flush(&queue_0); + + /* Deleting the queue wakes a suspended peeker. */ + tx_thread_resume(&peek_thread_1); + tx_queue_delete(&queue_0); + if ((peek_1_count != 7) || (peek_1_status != TX_DELETED)) + test_error(25); + + /* Successful test. */ + printf("SUCCESS!\n"); + test_control_return(0); +} + + +static void peek_thread_1_entry(ULONG thread_input) +{ + + while (1) + { + + peek_1_status = tx_queue_peek(&queue_0, peek_1_message, TX_WAIT_FOREVER); + peek_1_count++; + tx_thread_suspend(&peek_thread_1); + } +} + + +static void peek_thread_2_entry(ULONG thread_input) +{ + + while (1) + { + + peek_2_status = tx_queue_peek(&queue_0, peek_2_message, TX_WAIT_FOREVER); + peek_2_count++; + tx_thread_suspend(&peek_thread_2); + } +} + + +static void receive_thread_entry(ULONG thread_input) +{ + + while (1) + { + + if (tx_queue_receive(&queue_0, receive_message, TX_WAIT_FOREVER) == TX_SUCCESS) + { + + receive_count++; + } + tx_thread_suspend(&receive_thread); + } +} + + +static void front_send_thread_entry(ULONG thread_input) +{ + + while (1) + { + + if (tx_queue_front_send(&queue_0, front_send_message, TX_WAIT_FOREVER) == TX_SUCCESS) + { + + front_send_count++; + } + tx_thread_suspend(&front_send_thread); + } +} diff --git a/test/tx/cmake/regression/CMakeLists.txt b/test/tx/cmake/regression/CMakeLists.txt index 6b94a8576..00ea3c17a 100644 --- a/test/tx/cmake/regression/CMakeLists.txt +++ b/test/tx/cmake/regression/CMakeLists.txt @@ -62,6 +62,7 @@ set(regression_test_cases ${SOURCE_DIR}/threadx_queue_front_send_test.c ${SOURCE_DIR}/threadx_queue_full_suspension_test.c ${SOURCE_DIR}/threadx_queue_information_test.c + ${SOURCE_DIR}/threadx_queue_peek_test.c ${SOURCE_DIR}/threadx_queue_prioritize.c ${SOURCE_DIR}/threadx_queue_suspension_timeout_test.c ${SOURCE_DIR}/threadx_queue_thread_terminate_test.c diff --git a/test/tx/module_manager/threadx_module_manager_block_pool_parameters_test.c b/test/tx/module_manager/threadx_module_manager_block_pool_parameters_test.c index 4fbd3e50b..06724014f 100644 --- a/test/tx/module_manager/threadx_module_manager_block_pool_parameters_test.c +++ b/test/tx/module_manager/threadx_module_manager_block_pool_parameters_test.c @@ -155,6 +155,7 @@ #define TXM_QUEUE_FLUSH_CALL_NOT_USED #define TXM_QUEUE_FRONT_SEND_CALL_NOT_USED #define TXM_QUEUE_INFO_GET_CALL_NOT_USED +#define TXM_QUEUE_PEEK_CALL_NOT_USED #define TXM_QUEUE_PERFORMANCE_INFO_GET_CALL_NOT_USED #define TXM_QUEUE_PERFORMANCE_SYSTEM_INFO_GET_CALL_NOT_USED #define TXM_QUEUE_PRIORITIZE_CALL_NOT_USED diff --git a/test/tx/module_manager/threadx_module_manager_object_name_range_test.c b/test/tx/module_manager/threadx_module_manager_object_name_range_test.c index 7e6ee5e5d..78672951f 100644 --- a/test/tx/module_manager/threadx_module_manager_object_name_range_test.c +++ b/test/tx/module_manager/threadx_module_manager_object_name_range_test.c @@ -140,6 +140,7 @@ #define TXM_QUEUE_FLUSH_CALL_NOT_USED #define TXM_QUEUE_FRONT_SEND_CALL_NOT_USED #define TXM_QUEUE_INFO_GET_CALL_NOT_USED +#define TXM_QUEUE_PEEK_CALL_NOT_USED #define TXM_QUEUE_PERFORMANCE_INFO_GET_CALL_NOT_USED #define TXM_QUEUE_PERFORMANCE_SYSTEM_INFO_GET_CALL_NOT_USED #define TXM_QUEUE_PRIORITIZE_CALL_NOT_USED diff --git a/test/tx/module_manager/threadx_module_manager_queue_message_range_test.c b/test/tx/module_manager/threadx_module_manager_queue_message_range_test.c index 5cbc4539e..4baf0e0c3 100644 --- a/test/tx/module_manager/threadx_module_manager_queue_message_range_test.c +++ b/test/tx/module_manager/threadx_module_manager_queue_message_range_test.c @@ -125,6 +125,7 @@ #define TXM_QUEUE_DELETE_CALL_NOT_USED #define TXM_QUEUE_FLUSH_CALL_NOT_USED #define TXM_QUEUE_INFO_GET_CALL_NOT_USED +#define TXM_QUEUE_PEEK_CALL_NOT_USED #define TXM_QUEUE_PERFORMANCE_INFO_GET_CALL_NOT_USED #define TXM_QUEUE_PERFORMANCE_SYSTEM_INFO_GET_CALL_NOT_USED #define TXM_QUEUE_PRIORITIZE_CALL_NOT_USED diff --git a/test/tx/regression/testcontrol.c b/test/tx/regression/testcontrol.c index bec50ee76..61633795e 100644 --- a/test/tx/regression/testcontrol.c +++ b/test/tx/regression/testcontrol.c @@ -193,6 +193,7 @@ void threadx_queue_flush_no_suspension_application_define(void *); void threadx_queue_front_send_application_define(void *); void threadx_queue_prioritize_application_define(void *); void threadx_queue_information_application_define(void *); +void threadx_queue_peek_application_define(void *); void threadx_semaphore_basic_application_define(void *); void threadx_semaphore_delete_application_define(void *); @@ -312,6 +313,7 @@ TEST_ENTRY test_control_tests[] = threadx_queue_front_send_application_define, threadx_queue_prioritize_application_define, threadx_queue_information_application_define, + threadx_queue_peek_application_define, threadx_semaphore_basic_application_define, threadx_semaphore_delete_application_define, diff --git a/test/tx/regression/threadx_queue_peek_test.c b/test/tx/regression/threadx_queue_peek_test.c new file mode 100644 index 000000000..a6e9c47b7 --- /dev/null +++ b/test/tx/regression/threadx_queue_peek_test.c @@ -0,0 +1,341 @@ +/***************************************************************************/ +/* Copyright (c) 2026 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 */ +/***************************************************************************/ + +/* This test is designed to test the queue peek service, both immediate and + with suspension, and its interaction with send, front send and receive. */ + +#include +#include "tx_api.h" + +static TX_THREAD thread_0; +static TX_THREAD peek_thread_1; +static TX_THREAD peek_thread_2; +static TX_THREAD receive_thread; +static TX_THREAD front_send_thread; + +static TX_QUEUE queue_0; + +static volatile ULONG peek_1_count; +static volatile UINT peek_1_status; +static ULONG peek_1_message[2]; + +static volatile ULONG peek_2_count; +static volatile UINT peek_2_status; +static ULONG peek_2_message[2]; + +static volatile ULONG receive_count; +static ULONG receive_message[2]; + +static volatile ULONG front_send_count; +static ULONG front_send_message[2] = {0xF0F0F0F0, 0x0F0F0F0F}; + + +/* Define thread prototypes. */ + +static void thread_0_entry(ULONG thread_input); +static void peek_thread_1_entry(ULONG thread_input); +static void peek_thread_2_entry(ULONG thread_input); +static void receive_thread_entry(ULONG thread_input); +static void front_send_thread_entry(ULONG thread_input); + + +/* Prototype for test control return. */ + +void test_control_return(UINT status); + + +static ULONG queue_enqueued(void) +{ + +ULONG enqueued; + + + tx_queue_info_get(&queue_0, TX_NULL, &enqueued, TX_NULL, TX_NULL, TX_NULL, TX_NULL); + return(enqueued); +} + + +static void test_error(UINT error_number) +{ + + printf("ERROR #%u\n", error_number); + test_control_return(1); +} + + +/* Define what the initial system looks like. */ + +#ifdef CTEST +void test_application_define(void *first_unused_memory) +#else +void threadx_queue_peek_application_define(void *first_unused_memory) +#endif +{ + +UINT status; +CHAR *pointer; + + + /* Put first available memory address into a character pointer. */ + pointer = (CHAR *) first_unused_memory; + + peek_1_count = 0; + peek_2_count = 0; + receive_count = 0; + front_send_count = 0; + + status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 16, 16, 100, TX_AUTO_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + status += tx_thread_create(&peek_thread_1, "peek thread 1", peek_thread_1_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 15, 15, 100, TX_DONT_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + status += tx_thread_create(&peek_thread_2, "peek thread 2", peek_thread_2_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 15, 15, 100, TX_DONT_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + status += tx_thread_create(&receive_thread, "receive thread", receive_thread_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 15, 15, 100, TX_DONT_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + status += tx_thread_create(&front_send_thread, "front send thread", front_send_thread_entry, 0, + pointer, TEST_STACK_SIZE_PRINTF, + 15, 15, 100, TX_DONT_START); + pointer = pointer + TEST_STACK_SIZE_PRINTF; + + /* Two messages of two words each. */ + status += tx_queue_create(&queue_0, "queue 0", TX_2_ULONG, pointer, 2*2*sizeof(ULONG)); + pointer = pointer + 2*2*sizeof(ULONG); + + if (status != TX_SUCCESS) + { + + printf("Running Queue Peek Test............................................. ERROR #1\n"); + test_control_return(1); + } +} + + +/* Define the test threads. */ + +static void thread_0_entry(ULONG thread_input) +{ + +UINT status; +ULONG message_a[2] = {0x11111111, 0xAAAAAAAA}; +ULONG message_b[2] = {0x22222222, 0xBBBBBBBB}; +ULONG message_c[2] = {0x33333333, 0xCCCCCCCC}; +ULONG message[2]; +#ifndef TX_DISABLE_ERROR_CHECKING +TX_QUEUE not_a_queue = {0}; +#endif + + + /* Inform user. */ + printf("Running Queue Peek Test............................................. "); + +#ifndef TX_DISABLE_ERROR_CHECKING + + if (tx_queue_peek(TX_NULL, message, TX_NO_WAIT) != TX_QUEUE_ERROR) + test_error(2); + + if (tx_queue_peek(¬_a_queue, message, TX_NO_WAIT) != TX_QUEUE_ERROR) + test_error(3); + + if (tx_queue_peek(&queue_0, TX_NULL, TX_NO_WAIT) != TX_PTR_ERROR) + test_error(4); +#endif + + /* Empty queue without wait. */ + if (tx_queue_peek(&queue_0, message, TX_NO_WAIT) != TX_QUEUE_EMPTY) + test_error(5); + + /* Empty queue with timeout. */ + if (tx_queue_peek(&queue_0, message, 2) != TX_QUEUE_EMPTY) + test_error(6); + + /* Peek returns the front message and leaves it in the queue. */ + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + tx_queue_send(&queue_0, message_b, TX_NO_WAIT); + message[0] = 0; + message[1] = 0; + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0]) || (message[1] != message_a[1]) || (queue_enqueued() != 2)) + test_error(7); + + /* Peeking twice returns the same message. */ + status = tx_queue_peek(&queue_0, message, TX_WAIT_FOREVER); + if ((status != TX_SUCCESS) || (message[0] != message_a[0]) || (queue_enqueued() != 2)) + test_error(8); + + /* Receive gets the peeked message, and peek then sees the next one. */ + status = tx_queue_receive(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0])) + test_error(9); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_b[0]) || (message[1] != message_b[1])) + test_error(10); + + /* Peek follows the read pointer across the wrap of the queue area. */ + tx_queue_send(&queue_0, message_c, TX_NO_WAIT); + tx_queue_receive(&queue_0, message, TX_NO_WAIT); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_c[0]) || (message[1] != message_c[1])) + test_error(11); + tx_queue_flush(&queue_0); + + /* A front send suspended on a full queue is what receive returns next, so peek returns it too. */ + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + tx_queue_send(&queue_0, message_b, TX_NO_WAIT); + tx_thread_resume(&front_send_thread); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != front_send_message[0]) || (message[1] != front_send_message[1])) + test_error(12); + status = tx_queue_receive(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != front_send_message[0]) || (front_send_count != 1)) + test_error(13); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0])) + test_error(14); + tx_queue_flush(&queue_0); + + /* A suspended peeker is handed a copy of the next message, which stays queued. */ + tx_thread_resume(&peek_thread_1); + if (peek_1_count != 0) + test_error(15); + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + if ((peek_1_count != 1) || (peek_1_status != TX_SUCCESS) || (peek_1_message[0] != message_a[0]) || + (peek_1_message[1] != message_a[1]) || (queue_enqueued() != 1)) + test_error(16); + tx_queue_flush(&queue_0); + + /* Every peeker at the head of the suspension list gets the message. */ + tx_thread_resume(&peek_thread_1); + tx_thread_resume(&peek_thread_2); + tx_queue_send(&queue_0, message_b, TX_NO_WAIT); + if ((peek_1_count != 2) || (peek_1_message[0] != message_b[0]) || + (peek_2_count != 1) || (peek_2_status != TX_SUCCESS) || (peek_2_message[0] != message_b[0]) || + (queue_enqueued() != 1)) + test_error(17); + tx_queue_flush(&queue_0); + + /* Peeker ahead of a receiver: both get the message and the receiver consumes it. */ + tx_thread_resume(&peek_thread_1); + tx_thread_resume(&receive_thread); + tx_queue_send(&queue_0, message_c, TX_NO_WAIT); + if ((peek_1_count != 3) || (peek_1_message[0] != message_c[0]) || + (receive_count != 1) || (receive_message[0] != message_c[0]) || (receive_message[1] != message_c[1]) || + (queue_enqueued() != 0)) + test_error(18); + + /* Receiver ahead of a peeker: the receiver consumes the message and the peeker keeps waiting. */ + tx_thread_resume(&receive_thread); + tx_thread_resume(&peek_thread_1); + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + if ((receive_count != 2) || (receive_message[0] != message_a[0]) || (peek_1_count != 3) || (queue_enqueued() != 0)) + test_error(19); + tx_queue_send(&queue_0, message_b, TX_NO_WAIT); + if ((peek_1_count != 4) || (peek_1_message[0] != message_b[0]) || (queue_enqueued() != 1)) + test_error(20); + tx_queue_flush(&queue_0); + + /* Front send also hands the message to a suspended peeker. */ + tx_thread_resume(&peek_thread_1); + status = tx_queue_front_send(&queue_0, message_c, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (peek_1_count != 5) || (peek_1_message[0] != message_c[0]) || (queue_enqueued() != 1)) + test_error(21); + status = tx_queue_receive(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_c[0])) + test_error(22); + + /* An aborted peek is removed from the queue's suspension list. */ + tx_thread_resume(&peek_thread_1); + tx_thread_wait_abort(&peek_thread_1); + if ((peek_1_count != 6) || (peek_1_status != TX_WAIT_ABORTED)) + test_error(23); + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + if ((peek_1_count != 6) || (queue_enqueued() != 1)) + test_error(24); + tx_queue_flush(&queue_0); + + /* Deleting the queue wakes a suspended peeker. */ + tx_thread_resume(&peek_thread_1); + tx_queue_delete(&queue_0); + if ((peek_1_count != 7) || (peek_1_status != TX_DELETED)) + test_error(25); + + /* Successful test. */ + printf("SUCCESS!\n"); + test_control_return(0); +} + + +static void peek_thread_1_entry(ULONG thread_input) +{ + + while (1) + { + + peek_1_status = tx_queue_peek(&queue_0, peek_1_message, TX_WAIT_FOREVER); + peek_1_count++; + tx_thread_suspend(&peek_thread_1); + } +} + + +static void peek_thread_2_entry(ULONG thread_input) +{ + + while (1) + { + + peek_2_status = tx_queue_peek(&queue_0, peek_2_message, TX_WAIT_FOREVER); + peek_2_count++; + tx_thread_suspend(&peek_thread_2); + } +} + + +static void receive_thread_entry(ULONG thread_input) +{ + + while (1) + { + + if (tx_queue_receive(&queue_0, receive_message, TX_WAIT_FOREVER) == TX_SUCCESS) + { + + receive_count++; + } + tx_thread_suspend(&receive_thread); + } +} + + +static void front_send_thread_entry(ULONG thread_input) +{ + + while (1) + { + + if (tx_queue_front_send(&queue_0, front_send_message, TX_WAIT_FOREVER) == TX_SUCCESS) + { + + front_send_count++; + } + tx_thread_suspend(&front_send_thread); + } +}