diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 51314c957..26a1ba8f5 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -64,6 +64,7 @@ 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_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 +165,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_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_peek.c b/common/src/tx_queue_peek.c new file mode 100644 index 000000000..3255540ba --- /dev/null +++ b/common/src/tx_queue_peek.c @@ -0,0 +1,142 @@ +/*************************************************************************** + * 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. This function never suspends, so */ +/* it may be called from threads, timers and ISRs. Only TX_NO_WAIT is */ +/* supported as wait option; the parameter is reserved for a future */ +/* blocking peek. */ +/* */ +/* 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, must be */ +/* TX_NO_WAIT */ +/* */ +/* OUTPUT */ +/* */ +/* TX_SUCCESS Message copied */ +/* TX_QUEUE_EMPTY Queue is empty */ +/* TX_WAIT_ERROR Wait option is not TX_NO_WAIT */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* 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 status; + + + /* Blocking peek is not supported. */ + if (wait_option != TX_NO_WAIT) + { + + status = TX_WAIT_ERROR; + } + else + { + + /* 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) + + /* 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 (queue_ptr -> tx_queue_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) + + status = TX_SUCCESS; + } + else + { + + status = TX_QUEUE_EMPTY; + } + + /* Restore interrupts. */ + TX_RESTORE + } + + /* Return completion status. */ + return(status); +} + diff --git a/common/src/txe_queue_peek.c b/common/src/txe_queue_peek.c new file mode 100644 index 000000000..3e2dc80a3 --- /dev/null +++ b/common/src/txe_queue_peek.c @@ -0,0 +1,106 @@ +/*************************************************************************** + * 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_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, must be */ +/* TX_NO_WAIT */ +/* */ +/* OUTPUT */ +/* */ +/* TX_QUEUE_ERROR Invalid queue pointer */ +/* TX_PTR_ERROR Invalid destination pointer (NULL)*/ +/* TX_WAIT_ERROR Wait option is not TX_NO_WAIT */ +/* 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; + + + /* 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 + { + + /* Call actual queue peek function, which also rejects any wait option + other than TX_NO_WAIT. */ + 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..2c08ee649 --- /dev/null +++ b/common_modules/module_lib/src/txm_queue_peek.c @@ -0,0 +1,71 @@ +/*************************************************************************** + * 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, must be */ +/* TX_NO_WAIT */ +/* */ +/* 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..1bf995949 100644 --- a/common_smp/CMakeLists.txt +++ b/common_smp/CMakeLists.txt @@ -64,6 +64,7 @@ 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_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 +176,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_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_peek.c b/common_smp/src/tx_queue_peek.c new file mode 100644 index 000000000..3255540ba --- /dev/null +++ b/common_smp/src/tx_queue_peek.c @@ -0,0 +1,142 @@ +/*************************************************************************** + * 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. This function never suspends, so */ +/* it may be called from threads, timers and ISRs. Only TX_NO_WAIT is */ +/* supported as wait option; the parameter is reserved for a future */ +/* blocking peek. */ +/* */ +/* 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, must be */ +/* TX_NO_WAIT */ +/* */ +/* OUTPUT */ +/* */ +/* TX_SUCCESS Message copied */ +/* TX_QUEUE_EMPTY Queue is empty */ +/* TX_WAIT_ERROR Wait option is not TX_NO_WAIT */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* 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 status; + + + /* Blocking peek is not supported. */ + if (wait_option != TX_NO_WAIT) + { + + status = TX_WAIT_ERROR; + } + else + { + + /* 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) + + /* 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 (queue_ptr -> tx_queue_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) + + status = TX_SUCCESS; + } + else + { + + status = TX_QUEUE_EMPTY; + } + + /* Restore interrupts. */ + TX_RESTORE + } + + /* Return completion status. */ + return(status); +} + diff --git a/common_smp/src/txe_queue_peek.c b/common_smp/src/txe_queue_peek.c new file mode 100644 index 000000000..3e2dc80a3 --- /dev/null +++ b/common_smp/src/txe_queue_peek.c @@ -0,0 +1,106 @@ +/*************************************************************************** + * 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_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, must be */ +/* TX_NO_WAIT */ +/* */ +/* OUTPUT */ +/* */ +/* TX_QUEUE_ERROR Invalid queue pointer */ +/* TX_PTR_ERROR Invalid destination pointer (NULL)*/ +/* TX_WAIT_ERROR Wait option is not TX_NO_WAIT */ +/* 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; + + + /* 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 + { + + /* Call actual queue peek function, which also rejects any wait option + other than TX_NO_WAIT. */ + 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..a62fa4021 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,7 @@ 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_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..92aa07179 --- /dev/null +++ b/test/smp/regression/threadx_queue_peek_test.c @@ -0,0 +1,232 @@ +/***************************************************************************/ +/* 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 non-blocking queue peek service and its + interaction with send, front send and receive. */ + +#include +#include "tx_api.h" + +static TX_THREAD thread_0; +static TX_THREAD front_send_thread; + +static TX_QUEUE queue_0; +static TX_TIMER timer_0; + +static volatile ULONG front_send_count; +static ULONG front_send_message[2] = {0xF0F0F0F0, 0x0F0F0F0F}; + +static volatile ULONG timer_count; +static volatile UINT timer_status; +static volatile UINT timer_wait_status; +static ULONG timer_message[2]; + + +/* Define thread prototypes. */ + +static void thread_0_entry(ULONG thread_input); +static void front_send_thread_entry(ULONG thread_input); +static void timer_0_entry(ULONG timer_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; + + front_send_count = 0; + timer_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(&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; + + status += tx_timer_create(&timer_0, "timer 0", timer_0_entry, 0, 1, 0, TX_NO_ACTIVATE); + + /* 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. */ + if (tx_queue_peek(&queue_0, message, TX_NO_WAIT) != TX_QUEUE_EMPTY) + test_error(5); + + /* Only TX_NO_WAIT is supported, whether the queue is empty or not. */ + if (tx_queue_peek(&queue_0, message, 2) != TX_WAIT_ERROR) + test_error(6); + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + message[0] = 0; + if ((tx_queue_peek(&queue_0, message, TX_WAIT_FOREVER) != TX_WAIT_ERROR) || (message[0] != 0)) + test_error(7); + tx_queue_flush(&queue_0); + + /* 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(8); + + /* Peeking twice returns the same message. */ + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0]) || (queue_enqueued() != 2)) + test_error(9); + + /* 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(10); + 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(11); + + /* 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(12); + 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(13); + 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(14); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0])) + test_error(15); + + /* Peek works from a non-thread context (timer callback). */ + tx_timer_activate(&timer_0); + tx_thread_sleep(3); + if ((timer_count != 1) || (timer_status != TX_SUCCESS) || (timer_message[0] != message_a[0]) || + (timer_message[1] != message_a[1]) || (timer_wait_status != TX_WAIT_ERROR) || (queue_enqueued() != 2)) + test_error(16); + + /* Successful test. */ + printf("SUCCESS!\n"); + test_control_return(0); +} + + +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); + } +} + + +static void timer_0_entry(ULONG timer_input) +{ + +ULONG message[2]; + + + timer_status = tx_queue_peek(&queue_0, timer_message, TX_NO_WAIT); + timer_wait_status = tx_queue_peek(&queue_0, message, 1); + timer_count++; +} 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..92aa07179 --- /dev/null +++ b/test/tx/regression/threadx_queue_peek_test.c @@ -0,0 +1,232 @@ +/***************************************************************************/ +/* 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 non-blocking queue peek service and its + interaction with send, front send and receive. */ + +#include +#include "tx_api.h" + +static TX_THREAD thread_0; +static TX_THREAD front_send_thread; + +static TX_QUEUE queue_0; +static TX_TIMER timer_0; + +static volatile ULONG front_send_count; +static ULONG front_send_message[2] = {0xF0F0F0F0, 0x0F0F0F0F}; + +static volatile ULONG timer_count; +static volatile UINT timer_status; +static volatile UINT timer_wait_status; +static ULONG timer_message[2]; + + +/* Define thread prototypes. */ + +static void thread_0_entry(ULONG thread_input); +static void front_send_thread_entry(ULONG thread_input); +static void timer_0_entry(ULONG timer_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; + + front_send_count = 0; + timer_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(&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; + + status += tx_timer_create(&timer_0, "timer 0", timer_0_entry, 0, 1, 0, TX_NO_ACTIVATE); + + /* 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. */ + if (tx_queue_peek(&queue_0, message, TX_NO_WAIT) != TX_QUEUE_EMPTY) + test_error(5); + + /* Only TX_NO_WAIT is supported, whether the queue is empty or not. */ + if (tx_queue_peek(&queue_0, message, 2) != TX_WAIT_ERROR) + test_error(6); + tx_queue_send(&queue_0, message_a, TX_NO_WAIT); + message[0] = 0; + if ((tx_queue_peek(&queue_0, message, TX_WAIT_FOREVER) != TX_WAIT_ERROR) || (message[0] != 0)) + test_error(7); + tx_queue_flush(&queue_0); + + /* 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(8); + + /* Peeking twice returns the same message. */ + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0]) || (queue_enqueued() != 2)) + test_error(9); + + /* 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(10); + 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(11); + + /* 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(12); + 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(13); + 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(14); + status = tx_queue_peek(&queue_0, message, TX_NO_WAIT); + if ((status != TX_SUCCESS) || (message[0] != message_a[0])) + test_error(15); + + /* Peek works from a non-thread context (timer callback). */ + tx_timer_activate(&timer_0); + tx_thread_sleep(3); + if ((timer_count != 1) || (timer_status != TX_SUCCESS) || (timer_message[0] != message_a[0]) || + (timer_message[1] != message_a[1]) || (timer_wait_status != TX_WAIT_ERROR) || (queue_enqueued() != 2)) + test_error(16); + + /* Successful test. */ + printf("SUCCESS!\n"); + test_control_return(0); +} + + +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); + } +} + + +static void timer_0_entry(ULONG timer_input) +{ + +ULONG message[2]; + + + timer_status = tx_queue_peek(&queue_0, timer_message, TX_NO_WAIT); + timer_wait_status = tx_queue_peek(&queue_0, message, 1); + timer_count++; +}