Added custom mutex spin.

A fixed max spin count is used -- with benchmark results showing it
solves almost all problems. As the benchmark used was rather intense,
the upper bound could be a little bit high. However it should offer a
good tradeoff between spinning and blocking.
This commit is contained in:
Qi Wang
2017-03-15 15:31:37 -07:00
committed by Qi Wang
parent 20b8c70e9f
commit 74f78cafda
3 changed files with 27 additions and 17 deletions

View File

@@ -69,14 +69,26 @@ void
malloc_mutex_lock_slow(malloc_mutex_t *mutex) {
mutex_prof_data_t *data = &mutex->prof_data;
{//TODO: a smart spin policy
if (ncpus == 1) {
goto label_spin_done;
}
int cnt = 0, max_cnt = MALLOC_MUTEX_MAX_SPIN;
do {
CPU_SPINWAIT;
if (!malloc_mutex_trylock(mutex)) {
data->n_spin_acquired++;
return;
}
}
} while (cnt++ < max_cnt);
if (!config_stats) {
/* Only spin is useful when stats is off. */
malloc_mutex_lock_final(mutex);
return;
}
nstime_t now, before;
label_spin_done:
nstime_init(&now, 0);
nstime_update(&now);
nstime_copy(&before, &now);