From 86ac7dc10af252a05fbbe622a6b27d5eaf83003f Mon Sep 17 00:00:00 2001 From: Andy Ross Date: Fri, 17 Jan 2020 11:55:26 -0800 Subject: [PATCH] samples/userspace/shared_mem: Fix SMP race There was some unlocked initialization code in the "enc" thread that would race with the "pt" and "ct" threads if another CPU was available to run them (it was safe on UP because "enc" entered the queue first and was cooperative, the others wouldn't run until it blocked). Move it to main() and remove the enc_state guard variable which is no longer doing anything. Signed-off-by: Andy Ross --- samples/userspace/shared_mem/src/main.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/samples/userspace/shared_mem/src/main.c b/samples/userspace/shared_mem/src/main.c index 43ff7c1c341..45748526790 100644 --- a/samples/userspace/shared_mem/src/main.c +++ b/samples/userspace/shared_mem/src/main.c @@ -78,7 +78,6 @@ struct k_thread ct_thread; K_THREAD_STACK_DEFINE(ct_stack, STACKSIZE); _app_enc_d char encMSG[] = "ENC!\n"; -_app_enc_d int enc_state = 1; _app_enc_b char enc_pt[50]; /* Copy form shared pt */ _app_enc_b char enc_ct[50]; /* Copy to shared ct */ @@ -104,6 +103,15 @@ void main(void) struct k_mem_partition *dom0_parts[] = {&part0, &part1}; k_tid_t tPT, tENC, tCT; + fBUFIN = 0; /* clear flags */ + fBUFOUT = 0; + calc_rev_wheel((BYTE *) &W1, (BYTE *)&W1R); + calc_rev_wheel((BYTE *) &W2, (BYTE *)&W2R); + calc_rev_wheel((BYTE *) &W3, (BYTE *)&W3R); + IW1 = 0; + IW2 = 0; + IW3 = 0; + k_thread_access_grant(k_current_get(), &allforone); /* @@ -169,17 +177,6 @@ void enc(void) { int index, index_out; - if (enc_state == 1) { - fBUFIN = 0; /* clear flags */ - fBUFOUT = 0; - calc_rev_wheel((BYTE *) &W1, (BYTE *)&W1R); - calc_rev_wheel((BYTE *) &W2, (BYTE *)&W2R); - calc_rev_wheel((BYTE *) &W3, (BYTE *)&W3R); - IW1 = 0; - IW2 = 0; - IW3 = 0; - enc_state = 0; - } while (1) { k_sem_take(&allforone, K_FOREVER);