Skip to content

Commit

Permalink
core: arm: mutex: simplify code with xsave/xrestore spin lock
Browse files Browse the repository at this point in the history
cpu_spin_lock_xsave/cpu_spin_unlock_xrestore could be used
to simplify the code.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
  • Loading branch information
MrVan authored and jforissier committed Jul 17, 2017
1 parent afd74d5 commit 4c6699f
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions core/arch/arm/kernel/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ static void __mutex_lock(struct mutex *m, const char *fname, int lineno)
* all.
*/

old_itr_status = thread_mask_exceptions(THREAD_EXCP_ALL);
cpu_spin_lock(&m->spin_lock);
old_itr_status = cpu_spin_lock_xsave(&m->spin_lock);

old_value = m->value;
if (old_value == MUTEX_VALUE_LOCKED) {
Expand All @@ -69,8 +68,7 @@ static void __mutex_lock(struct mutex *m, const char *fname, int lineno)
thread_add_mutex(m);
}

cpu_spin_unlock(&m->spin_lock);
thread_unmask_exceptions(old_itr_status);
cpu_spin_unlock_xrestore(&m->spin_lock, old_itr_status);

if (old_value == MUTEX_VALUE_LOCKED) {
/*
Expand All @@ -90,17 +88,15 @@ static void __mutex_unlock(struct mutex *m, const char *fname, int lineno)
assert_have_no_spinlock();
assert(thread_get_id_may_fail() != -1);

old_itr_status = thread_mask_exceptions(THREAD_EXCP_ALL);
cpu_spin_lock(&m->spin_lock);
old_itr_status = cpu_spin_lock_xsave(&m->spin_lock);

if (m->value != MUTEX_VALUE_LOCKED)
panic();

thread_rem_mutex(m);
m->value = MUTEX_VALUE_UNLOCKED;

cpu_spin_unlock(&m->spin_lock);
thread_unmask_exceptions(old_itr_status);
cpu_spin_unlock_xrestore(&m->spin_lock, old_itr_status);

wq_wake_one(&m->wq, m, fname, lineno);
}
Expand All @@ -114,17 +110,15 @@ static bool __mutex_trylock(struct mutex *m, const char *fname __unused,
assert_have_no_spinlock();
assert(thread_get_id_may_fail() != -1);

old_itr_status = thread_mask_exceptions(THREAD_EXCP_ALL);
cpu_spin_lock(&m->spin_lock);
old_itr_status = cpu_spin_lock_xsave(&m->spin_lock);

old_value = m->value;
if (old_value == MUTEX_VALUE_UNLOCKED) {
m->value = MUTEX_VALUE_LOCKED;
thread_add_mutex(m);
}

cpu_spin_unlock(&m->spin_lock);
thread_unmask_exceptions(old_itr_status);
cpu_spin_unlock_xrestore(&m->spin_lock, old_itr_status);

return old_value == MUTEX_VALUE_UNLOCKED;
}
Expand Down Expand Up @@ -194,11 +188,9 @@ static void cv_signal(struct condvar *cv, bool only_one, const char *fname,
uint32_t old_itr_status;
struct mutex *m;

old_itr_status = thread_mask_exceptions(THREAD_EXCP_ALL);
cpu_spin_lock(&cv->spin_lock);
old_itr_status = cpu_spin_lock_xsave(&cv->spin_lock);
m = cv->m;
cpu_spin_unlock(&cv->spin_lock);
thread_unmask_exceptions(old_itr_status);
cpu_spin_unlock_xrestore(&cv->spin_lock, old_itr_status);

if (m)
wq_promote_condvar(&m->wq, cv, only_one, m, fname, lineno);
Expand Down Expand Up @@ -234,10 +226,8 @@ static void __condvar_wait(struct condvar *cv, struct mutex *m,
uint32_t old_itr_status;
struct wait_queue_elem wqe;

old_itr_status = thread_mask_exceptions(THREAD_EXCP_ALL);

/* Link this condvar to this mutex until reinitialized */
cpu_spin_lock(&cv->spin_lock);
old_itr_status = cpu_spin_lock_xsave(&cv->spin_lock);
if (cv->m && cv->m != m)
panic("invalid mutex");

Expand All @@ -256,9 +246,7 @@ static void __condvar_wait(struct condvar *cv, struct mutex *m,
thread_rem_mutex(m);
m->value = MUTEX_VALUE_UNLOCKED;

cpu_spin_unlock(&m->spin_lock);

thread_unmask_exceptions(old_itr_status);
cpu_spin_unlock_xrestore(&m->spin_lock, old_itr_status);

/* Wake eventual waiters */
wq_wake_one(&m->wq, m, fname, lineno);
Expand Down

0 comments on commit 4c6699f

Please sign in to comment.