Ticket #49793: patch-qthread-stacksize.diff

File patch-qthread-stacksize.diff, 2.7 KB (added by devernay (Frédéric Devernay), 8 years ago)

patch default qthread stack size to get same behavior as linux

  • src/corelib/thread/qthread_unix.cpp

    old new  
    7979#endif
    8080
    8181#if defined(Q_OS_MAC)
     82#include <sys/resource.h> // getrlimit/setrlimit
    8283# ifdef qDebug
    8384#   define old_qDebug qDebug
    8485#   undef qDebug
     
    649650#endif // QT_HAS_THREAD_PRIORITY_SCHEDULING
    650651
    651652
     653#if defined(Q_OS_MAC) && !defined(Q_OS_IOS)
     654    if (d->stackSize == 0) {
     655        // Fix the default (too small) stack size for threads on OS X,
     656        // which also affects the thread pool.
     657        // See also:
     658        // https://bugreports.qt.io/browse/QTBUG-2568
     659        // This fix can also be found in Chromium:
     660        // https://chromium.googlesource.com/chromium/src.git/+/master/base/threading/platform_thread_mac.mm#186
     661
     662        // The Mac OS X default for a pthread stack size is 512kB.
     663        // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses
     664        // DEFAULT_STACK_SIZE for this purpose.
     665        //
     666        // 512kB isn't quite generous enough for some deeply recursive threads that
     667        // otherwise request the default stack size by specifying 0. Here, adopt
     668        // glibc's behavior as on Linux, which is to use the current stack size
     669        // limit (ulimit -s) as the default stack size. See
     670        // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To
     671        // avoid setting the limit below the Mac OS X default or the minimum usable
     672        // stack size, these values are also considered. If any of these values
     673        // can't be determined, or if stack size is unlimited (ulimit -s unlimited),
     674        // stack_size is left at 0 to get the system default.
     675        //
     676        // Mac OS X normally only applies ulimit -s to the main thread stack. On
     677        // contemporary OS X and Linux systems alike, this value is generally 8MB
     678        // or in that neighborhood.
     679        size_t default_stack_size = 0;
     680        struct rlimit stack_rlimit;
     681        if (pthread_attr_getstacksize(&attributes, &default_stack_size) == 0 &&
     682            getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 &&
     683            stack_rlimit.rlim_cur != RLIM_INFINITY) {
     684            default_stack_size =
     685                    std::max(std::max(default_stack_size,
     686                                      static_cast<size_t>(PTHREAD_STACK_MIN)),
     687                             static_cast<size_t>(stack_rlimit.rlim_cur));
     688        }
     689        d->stackSize = default_stack_size;
     690    }
     691#endif
    652692    if (d->stackSize > 0) {
    653693#if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0 > 0)
    654694        int code = pthread_attr_setstacksize(&attr, d->stackSize);