Skip to content

Commit

Permalink
samples/userspace/shared_mem: Fix SMP race
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Andy Ross authored and andrewboie committed Jan 21, 2020
1 parent 4c2fc2a commit 86ac7dc
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions samples/userspace/shared_mem/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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);

/*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 86ac7dc

Please sign in to comment.