From 533d396761b630e372166f6f0522ba1c2d128d70 Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Thu, 24 Apr 2014 04:09:18 -0400 Subject: Add multi-reader, single-writer locks to kernel. Any number of readers may be in the critical section at a time and writers are mutually exclusive to all other threads. They are a better choice when data is rarely modified but often read and multiple threads can safely access it for reading. Priority inheritance is fully implemented along with other changes to the kernel to fully support it on multiowner objects. This also cleans up priority code in the kernel and updates some associated structures in existing objects to the cleaner form. Currently doesn't add the mrsw_lock.[ch] files since they're not yet needed by anything but the supporting improvements are still useful. This includes a typed bitarray API (bitarray.h) which is pretty basic for now. Change-Id: Idbe43dcd9170358e06d48d00f1c69728ff45b0e3 Reviewed-on: http://gerrit.rockbox.org/801 Reviewed-by: Michael Sevakis Tested: Michael Sevakis --- firmware/kernel/mutex.c | 65 ++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 44 deletions(-) (limited to 'firmware/kernel/mutex.c') diff --git a/firmware/kernel/mutex.c b/firmware/kernel/mutex.c index f1e4b3c722..2e90b0f4b1 100644 --- a/firmware/kernel/mutex.c +++ b/firmware/kernel/mutex.c @@ -27,31 +27,10 @@ #include #include "config.h" #include "system.h" -#include "mutex.h" -#include "corelock.h" +#include "kernel.h" #include "thread-internal.h" #include "kernel-internal.h" -static inline void __attribute__((always_inline)) -mutex_set_thread(struct mutex *mtx, struct thread_entry *td) -{ -#ifdef HAVE_PRIORITY_SCHEDULING - mtx->blocker.thread = td; -#else - mtx->thread = td; -#endif -} - -static inline struct thread_entry * __attribute__((always_inline)) -mutex_get_thread(volatile struct mutex *mtx) -{ -#ifdef HAVE_PRIORITY_SCHEDULING - return mtx->blocker.thread; -#else - return mtx->thread; -#endif -} - /* Initialize a mutex object - call before any use and do not call again once * the object is available to other threads */ void mutex_init(struct mutex *m) @@ -59,10 +38,9 @@ void mutex_init(struct mutex *m) corelock_init(&m->cl); m->queue = NULL; m->recursion = 0; - mutex_set_thread(m, NULL); + m->blocker.thread = NULL; #ifdef HAVE_PRIORITY_SCHEDULING m->blocker.priority = PRIORITY_IDLE; - m->blocker.wakeup_protocol = wakeup_priority_protocol_transfer; m->no_preempt = false; #endif } @@ -72,7 +50,7 @@ void mutex_lock(struct mutex *m) { struct thread_entry *current = thread_self_entry(); - if(current == mutex_get_thread(m)) + if(current == m->blocker.thread) { /* current thread already owns this mutex */ m->recursion++; @@ -83,10 +61,10 @@ void mutex_lock(struct mutex *m) corelock_lock(&m->cl); /* must read thread again inside cs (a multiprocessor concern really) */ - if(LIKELY(mutex_get_thread(m) == NULL)) + if(LIKELY(m->blocker.thread == NULL)) { /* lock is open */ - mutex_set_thread(m, current); + m->blocker.thread = current; corelock_unlock(&m->cl); return; } @@ -97,7 +75,7 @@ void mutex_lock(struct mutex *m) current->bqp = &m->queue; disable_irq(); - block_thread(current); + block_thread(current, TIMEOUT_BLOCK); corelock_unlock(&m->cl); @@ -109,9 +87,9 @@ void mutex_lock(struct mutex *m) void mutex_unlock(struct mutex *m) { /* unlocker not being the owner is an unlocking violation */ - KERNEL_ASSERT(mutex_get_thread(m) == thread_self_entry(), + KERNEL_ASSERT(m->blocker.thread == thread_self_entry(), "mutex_unlock->wrong thread (%s != %s)\n", - mutex_get_thread(m)->name, + m->blocker.thread->name, thread_self_entry()->name); if(m->recursion > 0) @@ -128,25 +106,24 @@ void mutex_unlock(struct mutex *m) if(LIKELY(m->queue == NULL)) { /* no threads waiting - open the lock */ - mutex_set_thread(m, NULL); + m->blocker.thread = NULL; corelock_unlock(&m->cl); return; } - else - { - const int oldlevel = disable_irq_save(); - /* Tranfer of owning thread is handled in the wakeup protocol - * if priorities are enabled otherwise just set it from the - * queue head. */ - IFN_PRIO( mutex_set_thread(m, m->queue); ) - IF_PRIO( unsigned int result = ) wakeup_thread(&m->queue); - restore_irq(oldlevel); - corelock_unlock(&m->cl); + const int oldlevel = disable_irq_save(); + /* Tranfer of owning thread is handled in the wakeup protocol + * if priorities are enabled otherwise just set it from the + * queue head. */ + IFN_PRIO( m->blocker.thread = m->queue; ) + unsigned int result = wakeup_thread(&m->queue, WAKEUP_TRANSFER); + restore_irq(oldlevel); + + corelock_unlock(&m->cl); #ifdef HAVE_PRIORITY_SCHEDULING - if((result & THREAD_SWITCH) && !m->no_preempt) - switch_thread(); + if((result & THREAD_SWITCH) && !m->no_preempt) + switch_thread(); #endif - } + (void)result; } -- cgit v1.2.3