Skip to content

Commit

Permalink
samples: smp: pi: keep main thread sleeping until ready
Browse files Browse the repository at this point in the history
Use `k_sleep(K_FOREVER)` in the main thread to keep it sleeping
while worker threads are running. The last worker thread to finish
calculation wakes up the main thread.

Signed-off-by: William Tambe <[email protected]>
  • Loading branch information
williamtcdns committed Jan 17, 2025
1 parent cdb9166 commit 6211d28
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions samples/arch/smp/pi/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ static struct k_thread tthread[THREADS_NUM];
static char th_buffer[THREADS_NUM][DIGITS_NUM + 1];
static atomic_t th_counter = THREADS_NUM;

struct k_thread *main_tid = 0;

Check failure on line 33 in samples/arch/smp/pi/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

GLOBAL_INITIALISERS

samples/arch/smp/pi/src/main.c:33 do not initialise globals to 0

void test_thread(void *arg1, void *arg2, void *arg3)
{
atomic_t *counter = (atomic_t *)arg1;
Expand Down Expand Up @@ -74,7 +76,8 @@ void test_thread(void *arg1, void *arg2, void *arg3)
buffer += 4;
}

atomic_dec(counter);
if (atomic_dec(counter) == 1)

Check warning on line 79 in samples/arch/smp/pi/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

BRACES

samples/arch/smp/pi/src/main.c:79 braces {} are required around if/while/for/else
k_wakeup(main_tid);
}

int main(void)
Expand All @@ -85,6 +88,8 @@ int main(void)
printk("Calculate first %d digits of Pi independently by %d threads.\n",
DIGITS_NUM, THREADS_NUM);

main_tid = k_current_get();

/* Capture initial time stamp */
start_time = k_cycle_get_32();

Expand All @@ -96,9 +101,7 @@ int main(void)
}

/* Wait for all workers to finish their calculations */
while (th_counter) {
k_sleep(K_MSEC(1));
}
k_sleep(K_FOREVER);

/* Capture final time stamp */
stop_time = k_cycle_get_32();
Expand Down

0 comments on commit 6211d28

Please sign in to comment.