1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-08 22:50:27 +02:00

Add helper for yielding in a spinlock

This commit is contained in:
Andy Wingo 2022-06-06 16:57:22 +02:00
parent 52166fe286
commit e4342f6c45
2 changed files with 21 additions and 11 deletions

View file

@ -9,6 +9,7 @@
#include "assert.h"
#include "debug.h"
#include "inline.h"
#include "spin.h"
// The Chase-Lev work-stealing deque, as initially described in "Dynamic
// Circular Work-Stealing Deque" (Chase and Lev, SPAA'05)
@ -530,8 +531,7 @@ trace_worker_check_termination(struct trace_worker *worker,
return 1;
}
size_t spin_count = 0;
while (1) {
for (size_t spin_count = 0;; spin_count++) {
if (trace_worker_can_steal_from_any(worker, tracer)) {
atomic_fetch_add_explicit(&tracer->active_tracers, 1,
memory_order_relaxed);
@ -544,15 +544,7 @@ trace_worker_check_termination(struct trace_worker *worker,
}
// spin
DEBUG("tracer #%zu: spinning #%zu\n", worker->id, spin_count);
if (spin_count < 10)
__builtin_ia32_pause();
else if (spin_count < 20)
sched_yield();
else if (spin_count < 40)
usleep(0);
else
usleep(1);
spin_count++;
yield_for_spin(spin_count);
}
}

18
spin.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef SPIN_H
#define SPIN_H
#include <sched.h>
#include <unistd.h>
static inline void yield_for_spin(size_t spin_count) {
if (spin_count < 10)
__builtin_ia32_pause();
else if (spin_count < 20)
sched_yield();
else if (spin_count < 40)
usleep(0);
else
usleep(1);
}
#endif // SPIN_H