You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add tx_queue_peek API to ThreadX Queue Component #838
Feature Request: Add tx_queue_peek API to ThreadX Queue Component
Is your feature request related to a problem? Please describe.
Currently, Eclipse ThreadX lacks a native tx_queue_peek function. In many real-time applications, a thread needs to inspect the message at the front of a queue without removing it (e.g., parsing a packet header to decide routing, or checking a command ID without consuming the data).
The current workarounds require either:
Calling tx_queue_receive followed immediately by tx_queue_front_send. This introduces overhead and requires explicit preemption locking (tx_thread_preemption_change) to prevent race conditions in multi-threaded environments.
Directly breaking encapsulation by inspecting internal TX_QUEUE structure pointers (tx_queue_read_ptr), which is unsafe and highly discouraged.
Describe the solution you'd like
I would like to propose a new native API: tx_queue_peek.
This function would behave identically to tx_queue_receive (including suspension logic if wait_option is used), except it copies the data from the head of the queue without advancing the internal read pointer (tx_queue_read_ptr) or decrementing the item count (tx_queue_enqueued). Other RTOS kernels, such as FreeRTOS (xQueuePeek) or embOS ('OS_MAILBOX_Peek') natively support this out of the box.
Describe alternatives you've considered
Receive + Front Send: Works but introduces unnecessary CPU cycles, memory copy overhead, and critical section complexity to ensure atomicity.
Additional context
I am willing to implement this feature and submit a Pull Request myself if the core team accepts this proposal.
While digging a little deeper into this I noticed, that supporting wait_option makes this more complicated and would like input on one design point: should tx_queue_peek support wait_option?
With wait_option (as proposed above)
Can block until a message arrives, like xQueuePeek.
But a peek doesn't consume the message, so one send may have to wake several waiting peeks and still deliver the message to a receiver or the queue. That requires changes to tx_queue_send and tx_queue_front_send (no cost when no peek is waiting), and send time grows with the number of waiting peeks.
Without wait_option: tx_queue_peek(queue_ptr, destination_ptr), like embOS OS_MAILBOX_Peek()
One small, self-contained function with constant run time that is safe to call from an ISR. No existing kernel code changes.
No blocking peek. Callers can use tx_queue_send_notify plus a semaphore or event flags if they need to wait.
Keep the signature, implement only TX_NO_WAIT
Same implementation as without wait_option. Any other wait option returns TX_WAIT_ERROR.
Blocking can be added later without breaking existing callers.
I'd suggest one of the non-blocking variants for now, and adding blocking later if there is demand. Which would you prefer?
Keeping wait_option in the signature but only supporting TX_NO_WAIT is probably the way to go here.
Adding blocking to a peek is tricky because peeking doesn't consume the message. When a thread sends to an empty queue, it would have to walk the suspension list, copy the message to all waiting peeker threads, and then still deliver to a real receiver or store it in the queue. That breaks the O(1) deterministic send path and stretches the interrupt disable window.
A non-blocking peek keeps everything self-contained in a new file and doesn't touch the existing queue send/receive code. It's constant time, ISR-safe, and returning TX_WAIT_ERROR for non-zero wait options keeps the API signature consistent if blocking is ever revisited. The only detail to handle is when a queue is full and has a front-suspended sender, where the message has to come from tx_thread_additional_suspend_info instead of tx_queue_read.
I have _tx_queue_peek, the _txe_ error checking wrapper, and tests ready on a branch. Let me know if you want me to put up a PR.
I think the non-blocking version is the safer approach, as it doesn't change existing code. The wait_option parameter is kept, so blocking could still be added later without breaking callers.
Activity
spir6s commentedon Nov 5, 2026
While digging a little deeper into this I noticed, that supporting
wait_optionmakes this more complicated and would like input on one design point: shouldtx_queue_peeksupportwait_option?With
wait_option(as proposed above)xQueuePeek.tx_queue_sendandtx_queue_front_send(no cost when no peek is waiting), and send time grows with the number of waiting peeks.Without
wait_option:tx_queue_peek(queue_ptr, destination_ptr), like embOSOS_MAILBOX_Peek()tx_queue_send_notifyplus a semaphore or event flags if they need to wait.Keep the signature, implement only
TX_NO_WAITwait_option. Any other wait option returnsTX_WAIT_ERROR.I'd suggest one of the non-blocking variants for now, and adding blocking later if there is demand. Which would you prefer?
Dev10-sys commentedon Nov 28, 2026
Keeping
wait_optionin the signature but only supportingTX_NO_WAITis probably the way to go here.Adding blocking to a peek is tricky because peeking doesn't consume the message. When a thread sends to an empty queue, it would have to walk the suspension list, copy the message to all waiting peeker threads, and then still deliver to a real receiver or store it in the queue. That breaks the O(1) deterministic send path and stretches the interrupt disable window.
A non-blocking peek keeps everything self-contained in a new file and doesn't touch the existing queue send/receive code. It's constant time, ISR-safe, and returning
TX_WAIT_ERRORfor non-zero wait options keeps the API signature consistent if blocking is ever revisited. The only detail to handle is when a queue is full and has a front-suspended sender, where the message has to come fromtx_thread_additional_suspend_infoinstead oftx_queue_read.I have
_tx_queue_peek, the_txe_error checking wrapper, and tests ready on a branch. Let me know if you want me to put up a PR.spir6s commentedon Nov 30, 2026
I've already implemented both versions using Claude Opus 5.5:
TX_NO_WAIT, other wait options returnTX_WAIT_ERROR): Add non-blocking tx_queue_peek #855wait_option: Add tx_queue_peek with wait_option (blocking) #856I think the non-blocking version is the safer approach, as it doesn't change existing code. The
wait_optionparameter is kept, so blocking could still be added later without breaking callers.