Skip to content

Commit 01a01de

Browse files
barnas-michalcfriedt
authored andcommitted
shell: fix polling TXDONE signal by multiple threads
This patch fixes the issue that can cause a deadlock in shell. When two threads simultaneously poll the TXDONE signal, only one of them will receive it, leaving second one stalled. The problem was that shell's context contains k_poll_event objects that were polled by multiple threads. Polling it overwrites the poller field that was set by previous thread. Instead, the k_poll_event object must be created on the stack by every thread that wants to poll the TXDONE signal. This makes sure that no thread will be left waiting for this signal forever. Signed-off-by: Michał Barnaś <[email protected]>
1 parent b30ae46 commit 01a01de

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

include/shell/shell.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,10 @@ struct shell_ctx {
698698
volatile union shell_internal internal; /*!< Internal shell data.*/
699699

700700
struct k_poll_signal signals[SHELL_SIGNALS];
701+
702+
/*!< Events that should be used only internally by shell thread.
703+
* Event for SHELL_SIGNAL_TXDONE is initialized but unused.
704+
*/
701705
struct k_poll_event events[SHELL_SIGNALS];
702706

703707
struct k_mutex wr_mtx;

subsys/shell/shell_ops.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,13 @@ static void shell_pend_on_txdone(const struct shell *shell)
369369
{
370370
if (IS_ENABLED(CONFIG_MULTITHREADING) &&
371371
(shell->ctx->state < SHELL_STATE_PANIC_MODE_ACTIVE)) {
372-
k_poll(&shell->ctx->events[SHELL_SIGNAL_TXDONE], 1, K_FOREVER);
372+
struct k_poll_event event;
373+
374+
k_poll_event_init(&event,
375+
K_POLL_TYPE_SIGNAL,
376+
K_POLL_MODE_NOTIFY_ONLY,
377+
&shell->ctx->signals[SHELL_SIGNAL_TXDONE]);
378+
k_poll(&event, 1, K_FOREVER);
373379
k_poll_signal_reset(&shell->ctx->signals[SHELL_SIGNAL_TXDONE]);
374380
} else {
375381
/* Blocking wait in case of bare metal. */

0 commit comments

Comments
 (0)