Skip to content

Commit e5d91b3

Browse files
shell: exchange k_poll for k_event
The shell subsystem currently uses k_poll for signalling. However, k_poll is only used for simple event signals, results are not used. Replacing the k_poll with k_event greatly simplifies the code, and saves 4 struct k_poll_signal and 4 struct k_poll_event (one of which was entirely unused) while costing a single struct k_event, for every shell instance. It also allows us to not select POLL, as we are using the simpler EVENTS instead. A quick test build of the shell test suite on an nrf54l15 produces the following build info: using EVENTS: FLASH: 71592 B 1428 KB 4.90% RAM: 9872 B 188 KB 5.13% IDT_LIST: 0 GB 32 KB 0.00% using POLL FLASH: 75524 B 1428 KB 5.16% RAM: 11224 B 188 KB 5.83% IDT_LIST: 0 GB 32 KB 0.00% Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
1 parent f52d71c commit e5d91b3

File tree

5 files changed

+28
-73
lines changed

5 files changed

+28
-73
lines changed

include/zephyr/shell/shell.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,10 @@ union shell_backend_ctx {
900900
};
901901

902902
enum shell_signal {
903-
SHELL_SIGNAL_RXRDY,
904-
SHELL_SIGNAL_LOG_MSG,
905-
SHELL_SIGNAL_KILL,
906-
SHELL_SIGNAL_TXDONE, /* TXDONE must be last one before SHELL_SIGNALS */
907-
SHELL_SIGNALS
903+
SHELL_SIGNAL_RXRDY = BIT(0),
904+
SHELL_SIGNAL_LOG_MSG = BIT(1),
905+
SHELL_SIGNAL_KILL = BIT(2),
906+
SHELL_SIGNAL_TXDONE = BIT(3),
908907
};
909908

910909
/**
@@ -962,12 +961,7 @@ struct shell_ctx {
962961
volatile union shell_backend_cfg cfg;
963962
volatile union shell_backend_ctx ctx;
964963

965-
struct k_poll_signal signals[SHELL_SIGNALS];
966-
967-
/** Events that should be used only internally by shell thread.
968-
* Event for SHELL_SIGNAL_TXDONE is initialized but unused.
969-
*/
970-
struct k_poll_event events[SHELL_SIGNALS];
964+
struct k_event signal_event;
971965

972966
struct k_mutex wr_mtx;
973967
k_tid_t tid;

subsys/shell/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
menuconfig SHELL
99
bool "Shell"
1010
imply LOG_RUNTIME_FILTERING
11-
select POLL
11+
select EVENTS
1212

1313
if SHELL
1414

subsys/shell/shell.c

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,19 +1171,16 @@ static void state_collect(const struct shell *sh)
11711171
static void transport_evt_handler(enum shell_transport_evt evt_type, void *ctx)
11721172
{
11731173
struct shell *sh = (struct shell *)ctx;
1174-
struct k_poll_signal *signal;
1174+
enum shell_signal sig = evt_type == SHELL_TRANSPORT_EVT_RX_RDY
1175+
? SHELL_SIGNAL_RXRDY
1176+
: SHELL_SIGNAL_TXDONE;
11751177

1176-
signal = (evt_type == SHELL_TRANSPORT_EVT_RX_RDY) ?
1177-
&sh->ctx->signals[SHELL_SIGNAL_RXRDY] :
1178-
&sh->ctx->signals[SHELL_SIGNAL_TXDONE];
1179-
k_poll_signal_raise(signal, 0);
1178+
k_event_post(&sh->ctx->signal_event, sig);
11801179
}
11811180

11821181
static void shell_log_process(const struct shell *sh)
11831182
{
11841183
bool processed = false;
1185-
int signaled = 0;
1186-
int result;
11871184

11881185
do {
11891186
if (!IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
@@ -1193,9 +1190,6 @@ static void shell_log_process(const struct shell *sh)
11931190
sh->log_backend);
11941191
}
11951192

1196-
struct k_poll_signal *signal =
1197-
&sh->ctx->signals[SHELL_SIGNAL_RXRDY];
1198-
11991193
z_shell_print_prompt_and_cmd(sh);
12001194

12011195
/* Arbitrary delay added to ensure that prompt is
@@ -1205,9 +1199,7 @@ static void shell_log_process(const struct shell *sh)
12051199
k_sleep(K_MSEC(15));
12061200
}
12071201

1208-
k_poll_signal_check(signal, &signaled, &result);
1209-
1210-
} while (processed && !signaled);
1202+
} while (processed && !k_event_test(&sh->ctx->signal_event, SHELL_SIGNAL_RXRDY));
12111203
}
12121204

12131205
static int instance_init(const struct shell *sh,
@@ -1224,16 +1216,9 @@ static int instance_init(const struct shell *sh,
12241216

12251217
history_init(sh);
12261218

1219+
k_event_init(&sh->ctx->signal_event);
12271220
k_mutex_init(&sh->ctx->wr_mtx);
12281221

1229-
for (int i = 0; i < SHELL_SIGNALS; i++) {
1230-
k_poll_signal_init(&sh->ctx->signals[i]);
1231-
k_poll_event_init(&sh->ctx->events[i],
1232-
K_POLL_TYPE_SIGNAL,
1233-
K_POLL_MODE_NOTIFY_ONLY,
1234-
&sh->ctx->signals[i]);
1235-
}
1236-
12371222
if (IS_ENABLED(CONFIG_SHELL_STATS)) {
12381223
sh->stats->log_lost_cnt = 0;
12391224
}
@@ -1302,19 +1287,15 @@ static int instance_uninit(const struct shell *sh)
13021287
typedef void (*shell_signal_handler_t)(const struct shell *sh);
13031288

13041289
static void shell_signal_handle(const struct shell *sh,
1305-
enum shell_signal sig_idx,
1290+
enum shell_signal sig,
13061291
shell_signal_handler_t handler)
13071292
{
1308-
struct k_poll_signal *sig = &sh->ctx->signals[sig_idx];
1309-
int set;
1310-
int res;
1311-
1312-
k_poll_signal_check(sig, &set, &res);
1313-
1314-
if (set) {
1315-
k_poll_signal_reset(sig);
1316-
handler(sh);
1293+
if (!k_event_test(&sh->ctx->signal_event, sig)) {
1294+
return;
13171295
}
1296+
1297+
handler(sh);
1298+
k_event_clear(&sh->ctx->signal_event, sig);
13181299
}
13191300

13201301
static void kill_handler(const struct shell *sh)
@@ -1354,20 +1335,13 @@ void shell_thread(void *shell_handle, void *arg_log_backend,
13541335
}
13551336

13561337
while (true) {
1357-
/* waiting for all signals except SHELL_SIGNAL_TXDONE */
1358-
err = k_poll(sh->ctx->events, SHELL_SIGNAL_TXDONE,
1338+
k_event_wait(&sh->ctx->signal_event,
1339+
SHELL_SIGNAL_RXRDY |
1340+
SHELL_SIGNAL_LOG_MSG |
1341+
SHELL_SIGNAL_KILL,
1342+
false,
13591343
K_FOREVER);
13601344

1361-
if (err != 0) {
1362-
if (k_mutex_lock(&sh->ctx->wr_mtx, SHELL_TX_MTX_TIMEOUT) != 0) {
1363-
return;
1364-
}
1365-
z_shell_fprintf(sh, SHELL_ERROR,
1366-
"Shell thread error: %d", err);
1367-
k_mutex_unlock(&sh->ctx->wr_mtx);
1368-
return;
1369-
}
1370-
13711345
k_mutex_lock(&sh->ctx->wr_mtx, K_FOREVER);
13721346

13731347
shell_signal_handle(sh, SHELL_SIGNAL_KILL, kill_handler);
@@ -1419,13 +1393,7 @@ void shell_uninit(const struct shell *sh, shell_uninit_cb_t cb)
14191393
__ASSERT_NO_MSG(sh);
14201394

14211395
if (IS_ENABLED(CONFIG_MULTITHREADING)) {
1422-
struct k_poll_signal *signal =
1423-
&sh->ctx->signals[SHELL_SIGNAL_KILL];
1424-
1425-
sh->ctx->uninit_cb = cb;
1426-
/* signal kill message */
1427-
(void)k_poll_signal_raise(signal, 0);
1428-
1396+
k_event_post(&sh->ctx->signal_event, SHELL_SIGNAL_KILL);
14291397
return;
14301398
}
14311399

subsys/shell/shell_log_backend.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ static void process(const struct log_backend *const backend,
232232
const struct log_output *log_output = log_backend->log_output;
233233
bool colors = IS_ENABLED(CONFIG_SHELL_VT100_COLORS) &&
234234
z_flag_use_colors_get(sh);
235-
struct k_poll_signal *signal;
236235

237236
switch (sh->log_backend->control_block->state) {
238237
case SHELL_LOG_BACKEND_ENABLED:
@@ -241,8 +240,8 @@ static void process(const struct log_backend *const backend,
241240
} else {
242241
if (copy_to_pbuffer(mpsc_buffer, msg, log_backend->timeout)) {
243242
if (IS_ENABLED(CONFIG_MULTITHREADING)) {
244-
signal = &sh->ctx->signals[SHELL_SIGNAL_LOG_MSG];
245-
k_poll_signal_raise(signal, 0);
243+
k_event_post(&sh->ctx->signal_event,
244+
SHELL_SIGNAL_LOG_MSG);
246245
}
247246
} else {
248247
dropped(backend, 1);

subsys/shell/shell_ops.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,8 @@ static void shell_pend_on_txdone(const struct shell *sh)
432432
{
433433
if (IS_ENABLED(CONFIG_MULTITHREADING) &&
434434
(sh->ctx->state < SHELL_STATE_PANIC_MODE_ACTIVE)) {
435-
struct k_poll_event event;
436-
437-
k_poll_event_init(&event,
438-
K_POLL_TYPE_SIGNAL,
439-
K_POLL_MODE_NOTIFY_ONLY,
440-
&sh->ctx->signals[SHELL_SIGNAL_TXDONE]);
441-
k_poll(&event, 1, K_FOREVER);
442-
k_poll_signal_reset(&sh->ctx->signals[SHELL_SIGNAL_TXDONE]);
435+
k_event_wait(&sh->ctx->signal_event, SHELL_SIGNAL_TXDONE, false, K_FOREVER);
436+
k_event_clear(&sh->ctx->signal_event, SHELL_SIGNAL_TXDONE);
443437
} else {
444438
/* Blocking wait in case of bare metal. */
445439
while (!z_flag_tx_rdy_get(sh)) {

0 commit comments

Comments
 (0)