Skip to content

Add tx_queue_peek API to ThreadX Queue Component #838

Open

Description

@spir6s

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:

  1. 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.
  2. 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.

UINT tx_queue_peek(TX_QUEUE *queue_ptr, VOID *destination_ptr, ULONG wait_option);

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.

Activity

  1. added
    feature
    on Nov 5, 2026
  2. spir6s commented on Nov 5, 2026

    @spir6s
    Author

    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?

  3. Dev10-sys commented on Nov 28, 2026

    @Dev10-sys

    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.

  4. spir6s commented on Nov 30, 2026

    @spir6s
    Author

    I've already implemented both versions using Claude Opus 5.5:

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions