xref: /netbsd-src/external/bsd/jemalloc.old/include/jemalloc/internal/background_thread_inlines.h (revision 8e33eff89e26cf71871ead62f0d5063e1313c33a)
1*8e33eff8Schristos #ifndef JEMALLOC_INTERNAL_BACKGROUND_THREAD_INLINES_H
2*8e33eff8Schristos #define JEMALLOC_INTERNAL_BACKGROUND_THREAD_INLINES_H
3*8e33eff8Schristos 
4*8e33eff8Schristos JEMALLOC_ALWAYS_INLINE bool
5*8e33eff8Schristos background_thread_enabled(void) {
6*8e33eff8Schristos 	return atomic_load_b(&background_thread_enabled_state, ATOMIC_RELAXED);
7*8e33eff8Schristos }
8*8e33eff8Schristos 
9*8e33eff8Schristos JEMALLOC_ALWAYS_INLINE void
10*8e33eff8Schristos background_thread_enabled_set(tsdn_t *tsdn, bool state) {
11*8e33eff8Schristos 	malloc_mutex_assert_owner(tsdn, &background_thread_lock);
12*8e33eff8Schristos 	atomic_store_b(&background_thread_enabled_state, state, ATOMIC_RELAXED);
13*8e33eff8Schristos }
14*8e33eff8Schristos 
15*8e33eff8Schristos JEMALLOC_ALWAYS_INLINE background_thread_info_t *
16*8e33eff8Schristos arena_background_thread_info_get(arena_t *arena) {
17*8e33eff8Schristos 	unsigned arena_ind = arena_ind_get(arena);
18*8e33eff8Schristos 	return &background_thread_info[arena_ind % ncpus];
19*8e33eff8Schristos }
20*8e33eff8Schristos 
21*8e33eff8Schristos JEMALLOC_ALWAYS_INLINE uint64_t
22*8e33eff8Schristos background_thread_wakeup_time_get(background_thread_info_t *info) {
23*8e33eff8Schristos 	uint64_t next_wakeup = nstime_ns(&info->next_wakeup);
24*8e33eff8Schristos 	assert(atomic_load_b(&info->indefinite_sleep, ATOMIC_ACQUIRE) ==
25*8e33eff8Schristos 	    (next_wakeup == BACKGROUND_THREAD_INDEFINITE_SLEEP));
26*8e33eff8Schristos 	return next_wakeup;
27*8e33eff8Schristos }
28*8e33eff8Schristos 
29*8e33eff8Schristos JEMALLOC_ALWAYS_INLINE void
30*8e33eff8Schristos background_thread_wakeup_time_set(tsdn_t *tsdn, background_thread_info_t *info,
31*8e33eff8Schristos     uint64_t wakeup_time) {
32*8e33eff8Schristos 	malloc_mutex_assert_owner(tsdn, &info->mtx);
33*8e33eff8Schristos 	atomic_store_b(&info->indefinite_sleep,
34*8e33eff8Schristos 	    wakeup_time == BACKGROUND_THREAD_INDEFINITE_SLEEP, ATOMIC_RELEASE);
35*8e33eff8Schristos 	nstime_init(&info->next_wakeup, wakeup_time);
36*8e33eff8Schristos }
37*8e33eff8Schristos 
38*8e33eff8Schristos JEMALLOC_ALWAYS_INLINE bool
39*8e33eff8Schristos background_thread_indefinite_sleep(background_thread_info_t *info) {
40*8e33eff8Schristos 	return atomic_load_b(&info->indefinite_sleep, ATOMIC_ACQUIRE);
41*8e33eff8Schristos }
42*8e33eff8Schristos 
43*8e33eff8Schristos JEMALLOC_ALWAYS_INLINE void
44*8e33eff8Schristos arena_background_thread_inactivity_check(tsdn_t *tsdn, arena_t *arena,
45*8e33eff8Schristos     bool is_background_thread) {
46*8e33eff8Schristos 	if (!background_thread_enabled() || is_background_thread) {
47*8e33eff8Schristos 		return;
48*8e33eff8Schristos 	}
49*8e33eff8Schristos 	background_thread_info_t *info =
50*8e33eff8Schristos 	    arena_background_thread_info_get(arena);
51*8e33eff8Schristos 	if (background_thread_indefinite_sleep(info)) {
52*8e33eff8Schristos 		background_thread_interval_check(tsdn, arena,
53*8e33eff8Schristos 		    &arena->decay_dirty, 0);
54*8e33eff8Schristos 	}
55*8e33eff8Schristos }
56*8e33eff8Schristos 
57*8e33eff8Schristos #endif /* JEMALLOC_INTERNAL_BACKGROUND_THREAD_INLINES_H */
58