diff --git a/configure.ac b/configure.ac index 5a5ba3ad..18b7a296 100644 --- a/configure.ac +++ b/configure.ac @@ -455,6 +455,7 @@ AC_CHECK_FUNCS([getcwd gettimeofday getwd memset munmap putenv realpath strcasec AC_CHECK_LIB(m,cbrt,[AC_DEFINE(HAVE_CBRT,1,[have cbrt() in libm.])]) AC_CHECK_LIB(m,hypot,[AC_DEFINE(HAVE_HYPOT,1,[have hypot() in libm.])]) AC_CHECK_LIB(m,atan2,[AC_DEFINE(HAVE_ATAN2,1,[have atan2() in libm.])]) +AC_CHECK_LIB([pthread], [pthread_setattr_default_np], [AC_DEFINE(HAVE_PTHREAD_DEFAULT_NP,1,[have pthread_setattr_default_np() in pthread.])]) # have to have these # need glib 2.6 for GOption diff --git a/libvips/iofuncs/init.c b/libvips/iofuncs/init.c index 6612eb35..64bf95f6 100644 --- a/libvips/iofuncs/init.c +++ b/libvips/iofuncs/init.c @@ -90,6 +90,10 @@ #include #include +#ifdef HAVE_PTHREAD_DEFAULT_NP +#include +#endif /*HAVE_PTHREAD_DEFAULT_NP*/ + /* abort() on the first warning or error. */ int vips__fatal = 0; @@ -309,6 +313,32 @@ vips_init( const char *argv0 ) (void) _setmaxstdio( 2048 ); #endif /*OS_WIN32*/ +#ifdef HAVE_PTHREAD_DEFAULT_NP +{ + const char *pstacksize_str; + /* Set the default stack size especially if you use musl + */ + if( (pstacksize_str = g_getenv( "VIPS_MIN_STACK_SIZE" )) ) { + guint64 default_min_stack_size = 1 << 21; // 2MB + guint64 vips_min_stack_size; + guint64 cur_stack_size; + pthread_attr_t attr; + vips_min_stack_size = vips__parse_size(pstacksize_str); + if (vips_min_stack_size == 0) { + vips_min_stack_size = default_min_stack_size; + } + if (pthread_attr_init(&attr) || + pthread_attr_getstacksize(&attr, &cur_stack_size) || + (cur_stack_size > vips_min_stack_size) || + pthread_attr_setstacksize(&attr, vips_min_stack_size) || + pthread_setattr_default_np(&attr)) { + g_warning("Could not set minimum pthread stack size of %s, current size is %dk", + pstacksize_str, (int) (cur_stack_size / 1024.0) ); + } + } +} +#endif /*HAVE_PTHREAD_DEFAULT_NP*/ + #ifdef HAVE_TYPE_INIT /* Before glib 2.36 you have to call this on startup. */