Skip to content

Commit e682106

Browse files
committed
extmod/zephyr_kernel: Restore k_yield() in mutex unlock for fair scheduling.
Restores k_yield() call after k_sem_give() in mp_thread_mutex_unlock() to match ports/zephyr pattern (mpthreadport.c:272). Without k_yield(), when GIL is released, current thread continues execution and can immediately re-acquire GIL before waiting threads get scheduling opportunity, violating fair GIL access semantics. With same thread priority (previous commit), k_yield() ensures waiting threads get immediate opportunity to acquire GIL after release, implementing proper fair scheduling behavior. Test results: 27/34 passing (unchanged from priority fix alone). Remaining 7 failures have different root causes. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent a10626b commit e682106

File tree

2 files changed

+1
-4
lines changed

2 files changed

+1
-4
lines changed

extmod/zephyr_kernel/kernel/mpthread_zephyr.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,7 @@ int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) {
592592
// Unlock mutex
593593
void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
594594
k_sem_give(&mutex->handle);
595-
// NOTE: k_yield() removed - was causing deadlock during thread creation
596-
// When main thread creates new thread and releases thread_mutex, yielding
597-
// immediately causes new thread to run and block on GIL (held by main),
598-
// creating deadlock. Rely on timeslicing for context switches instead.
595+
k_yield(); // Yield to allow waiting threads fair access (matches ports/zephyr)
599596
}
600597

601598
// Recursive mutex functions (only compiled when GIL is disabled)
-1.39 KB
Binary file not shown.

0 commit comments

Comments
 (0)