Skip to content

k_sem/k_event: stress coverage for the handled races + two doc notes #43

@swoisz

Description

@swoisz

An independent design review of the notification-backed k_sem (#41) confirmed every code-level trap is handled (give-before-take latch, ISR-side give, timeout-vs-give consume protocol) but found the hard paths have little or no test coverage. The same gaps apply to k_event (#42), which shares the architecture.

  • Timeout-vs-give race stress — highest value: the consume path (k_sem.c woken+timed-out branch) has ZERO coverage. Every giver in the suite delays well inside the timeout. Shape: ~100+ iterations of k_sem_take(K_MSEC(20)) with a giver firing at ~20 ms ± jitter (pin the giver to the other core on S3 for true concurrency). Per-iteration invariant: ret == 0 → count == 0; ret == -EAGAIN → count == 1 (the give latched). After each iteration drain, then assert k_sem_take(K_NO_WAIT) == -EBUSY AND a short blocking take times out cleanly — proves no stranded notification on the reserved index poisons the task's next blocking call. Mirror for k_event_wait.
  • FromISR give/post branch never runs in the suite. Add a giver from real ISR context (esp_timer ISR dispatch or a GPTimer ISR) to a K_FOREVER waiter; assert prompt wake (verifies the yield-from-ISR). One k_sem_take(K_NO_WAIT) from the same ISR (fast path is FreeRTOS-call-free). Same for k_event_post from ISR (including the multi-waiter accumulate-yield-once path).
  • Give racing the park (give-before-block, not give-before-take): a zero-delay higher-priority giver looped many times covers the enqueue→notify-wait window and the relock-recheck window probabilistically.
  • Multi-waiter beyond two: (a) N≥3 waiters, M gives — each wakes exactly once, units conserved; (b) FIFO order among equal-priority waiters (the strict > in the pop is what preserves it — pin with a test); (c) k_sem_reset and a single k_event_post waking 3+ waiters.
  • Doc: k_sem_reset is task-context-only — it uses the task-only notify API, while upstream's reset is ISR-legal. Real divergence; needs the @note on the declaration.
  • Doc: waiter priority cached at enqueuek_thread_priority_set on a blocked waiter leaves wake order using the stale value (upstream re-sorts the pend queue). One-line @note on k_sem_take/k_event_wait.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions