2017-04-18 07:35:04 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_SPIN_H
|
|
|
|
#define JEMALLOC_INTERNAL_SPIN_H
|
|
|
|
|
|
|
|
#define SPIN_INITIALIZER {0U}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned iteration;
|
|
|
|
} spin_t;
|
|
|
|
|
2017-10-04 09:03:02 +08:00
|
|
|
static inline void
|
2023-07-06 04:33:34 +08:00
|
|
|
spin_cpu_spinwait(void) {
|
2017-10-04 09:03:02 +08:00
|
|
|
# if HAVE_CPU_SPINWAIT
|
|
|
|
CPU_SPINWAIT;
|
|
|
|
# else
|
|
|
|
volatile int x = 0;
|
|
|
|
x = x;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2017-08-08 13:00:22 +08:00
|
|
|
static inline void
|
2017-04-18 07:35:04 +08:00
|
|
|
spin_adaptive(spin_t *spin) {
|
|
|
|
volatile uint32_t i;
|
|
|
|
|
|
|
|
if (spin->iteration < 5) {
|
|
|
|
for (i = 0; i < (1U << spin->iteration); i++) {
|
2017-10-04 09:03:02 +08:00
|
|
|
spin_cpu_spinwait();
|
2017-04-18 07:35:04 +08:00
|
|
|
}
|
|
|
|
spin->iteration++;
|
|
|
|
} else {
|
|
|
|
#ifdef _WIN32
|
|
|
|
SwitchToThread();
|
|
|
|
#else
|
|
|
|
sched_yield();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SPIN_INLINE
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_INTERNAL_SPIN_H */
|