From 81cb80d923367baf7c229b9c828aedf410229718 Mon Sep 17 00:00:00 2001 From: Filip Kokosinski Date: Fri, 26 Jul 2024 12:21:51 +0200 Subject: [PATCH] samples/synchronization: fix thread b pinning This commit adds a brief thread b suspend while the sample sets its affinity mask. If the call to `k_thread_cpu_pin` is being made while the thread is actively running, then we get `-EINVAL` and the affinity mask is left unchanged. Signed-off-by: Filip Kokosinski --- samples/synchronization/src/main.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/samples/synchronization/src/main.c b/samples/synchronization/src/main.c index e72eea92f925..e57f35acd993 100644 --- a/samples/synchronization/src/main.c +++ b/samples/synchronization/src/main.c @@ -110,7 +110,16 @@ int main(void) #if PIN_THREADS if (arch_num_cpus() > 1) { k_thread_cpu_pin(&thread_a_data, 0); + + /* + * Thread b is a static thread that is spawned immediately. This means that the + * following `k_thread_cpu_pin` call can fail with `-EINVAL` if the thread is + * actively running. Let's suspend the thread and resume it after the affinity mask + * is set. + */ + k_thread_suspend(thread_b); k_thread_cpu_pin(thread_b, 1); + k_thread_resume(thread_b); } #endif