Skip to content

Commit dedfeec

Browse files
committed
Invoke dlsym() on demand.
If no lazy lock or background thread is enabled, avoid dlsym pthread_create on boot.
1 parent c95284d commit dedfeec

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/background_thread.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,26 @@ background_thread_stats_read(tsdn_t *tsdn, background_thread_stats_t *stats) {
804804
#undef BILLION
805805
#undef BACKGROUND_THREAD_MIN_INTERVAL_NS
806806

807+
static bool
808+
pthread_create_fptr_init(void) {
809+
if (pthread_create_fptr != NULL) {
810+
return false;
811+
}
812+
pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create");
813+
if (pthread_create_fptr == NULL) {
814+
can_enable_background_thread = false;
815+
if (config_lazy_lock || opt_background_thread) {
816+
malloc_write("<jemalloc>: Error in dlsym(RTLD_NEXT, "
817+
"\"pthread_create\")\n");
818+
abort();
819+
}
820+
} else {
821+
can_enable_background_thread = true;
822+
}
823+
824+
return false;
825+
}
826+
807827
/*
808828
* When lazy lock is enabled, we need to make sure setting isthreaded before
809829
* taking any background_thread locks. This is called early in ctl (instead of
@@ -814,6 +834,7 @@ void
814834
background_thread_ctl_init(tsdn_t *tsdn) {
815835
malloc_mutex_assert_not_owner(tsdn, &background_thread_lock);
816836
#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER
837+
pthread_create_fptr_init();
817838
pthread_create_wrapper_init();
818839
#endif
819840
}
@@ -827,18 +848,10 @@ background_thread_boot0(void) {
827848
"supports pthread only\n");
828849
return true;
829850
}
830-
831851
#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER
832-
pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create");
833-
if (pthread_create_fptr == NULL) {
834-
can_enable_background_thread = false;
835-
if (config_lazy_lock || opt_background_thread) {
836-
malloc_write("<jemalloc>: Error in dlsym(RTLD_NEXT, "
837-
"\"pthread_create\")\n");
838-
abort();
839-
}
840-
} else {
841-
can_enable_background_thread = true;
852+
if ((config_lazy_lock || opt_background_thread) &&
853+
pthread_create_fptr_init()) {
854+
return true;
842855
}
843856
#endif
844857
return false;

0 commit comments

Comments
 (0)