Skip to content

Commit 36bd9df

Browse files
authored
Merge pull request #1291 from abdollar/set_stack_size_linux
provide an option to set the default stack size on linux
2 parents 8fbbfd7 + 25bfad1 commit 36bd9df

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ AC_CHECK_FUNCS([getcwd gettimeofday getwd memset munmap putenv realpath strcasec
455455
AC_CHECK_LIB(m,cbrt,[AC_DEFINE(HAVE_CBRT,1,[have cbrt() in libm.])])
456456
AC_CHECK_LIB(m,hypot,[AC_DEFINE(HAVE_HYPOT,1,[have hypot() in libm.])])
457457
AC_CHECK_LIB(m,atan2,[AC_DEFINE(HAVE_ATAN2,1,[have atan2() in libm.])])
458+
AC_CHECK_LIB([pthread], [pthread_setattr_default_np], [AC_DEFINE(HAVE_PTHREAD_DEFAULT_NP,1,[have pthread_setattr_default_np() in pthread.])])
458459

459460
# have to have these
460461
# need glib 2.6 for GOption

libvips/iofuncs/init.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
#include <vips/vector.h>
9191
#include <vips/vips7compat.h>
9292

93+
#ifdef HAVE_PTHREAD_DEFAULT_NP
94+
#include <pthread.h>
95+
#endif /*HAVE_PTHREAD_DEFAULT_NP*/
96+
9397
/* abort() on the first warning or error.
9498
*/
9599
int vips__fatal = 0;
@@ -309,6 +313,32 @@ vips_init( const char *argv0 )
309313
(void) _setmaxstdio( 2048 );
310314
#endif /*OS_WIN32*/
311315

316+
#ifdef HAVE_PTHREAD_DEFAULT_NP
317+
{
318+
const char *pstacksize_str;
319+
/* Set the default stack size especially if you use musl
320+
*/
321+
if( (pstacksize_str = g_getenv( "VIPS_MIN_STACK_SIZE" )) ) {
322+
guint64 default_min_stack_size = 1 << 21; // 2MB
323+
guint64 vips_min_stack_size;
324+
guint64 cur_stack_size;
325+
pthread_attr_t attr;
326+
vips_min_stack_size = vips__parse_size(pstacksize_str);
327+
if (vips_min_stack_size == 0) {
328+
vips_min_stack_size = default_min_stack_size;
329+
}
330+
if (pthread_attr_init(&attr) ||
331+
pthread_attr_getstacksize(&attr, &cur_stack_size) ||
332+
(cur_stack_size > vips_min_stack_size) ||
333+
pthread_attr_setstacksize(&attr, vips_min_stack_size) ||
334+
pthread_setattr_default_np(&attr)) {
335+
g_warning("Could not set minimum pthread stack size of %s, current size is %dk",
336+
pstacksize_str, (int) (cur_stack_size / 1024.0) );
337+
}
338+
}
339+
}
340+
#endif /*HAVE_PTHREAD_DEFAULT_NP*/
341+
312342
#ifdef HAVE_TYPE_INIT
313343
/* Before glib 2.36 you have to call this on startup.
314344
*/

0 commit comments

Comments
 (0)