From cb08b40976150b679d2978f82e3b7ba7c1732780 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sat, 16 Sep 2017 12:54:45 +0100 Subject: Move detection of to a configure test is needed by QThread::create. Instead of a fragile series of preprocessor tests, move its detection to a configure test. This dramatically simplifies the code, but on the other hand ties the availability of QThread::create() to the system used to compile Qt (rather the one used to compile an application). Change-Id: If1b06363379bf29126cfa68f2a0651cbb78a67f7 Reviewed-by: Thiago Macieira --- src/corelib/configure.json | 17 +++++++++++++++++ src/corelib/thread/qthread.cpp | 4 ++-- src/corelib/thread/qthread.h | 36 +++++++++++++++--------------------- 3 files changed, 34 insertions(+), 23 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/configure.json b/src/corelib/configure.json index 3feda2fffc..51e6d1d391 100644 --- a/src/corelib/configure.json +++ b/src/corelib/configure.json @@ -251,6 +251,18 @@ ] } }, + "cxx11_future": { + "label": "C++11 ", + "type": "compile", + "test": { + "include": "future", + "main": [ + "std::future f = std::async([]() { return 42; });", + "(void)f.get();" + ], + "qmake": "unix:LIBS += -lpthread" + } + }, "cxx11_random": { "label": "C++11 ", "type": "compile", @@ -442,6 +454,11 @@ "condition": "features.doubleconversion && libs.doubleconversion", "output": [ "privateFeature" ] }, + "cxx11_future": { + "label": "C++11 ", + "condition": "tests.cxx11_future", + "output": [ "publicFeature" ] + }, "cxx11_random": { "label": "C++11 ", "condition": "tests.cxx11_random", diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index e3ba1e4449..e92be64dfa 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -925,7 +925,7 @@ bool QThread::isInterruptionRequested() const \sa start() */ -#ifdef QTHREAD_HAS_CREATE +#if QT_CONFIG(cxx11_future) class QThreadCreateThread : public QThread { public: @@ -947,7 +947,7 @@ QThread *QThread::createThreadImpl(std::future &&future) { return new QThreadCreateThread(std::move(future)); } -#endif // QTHREAD_HAS_CREATE +#endif // QT_CONFIG(cxx11_future) /*! \class QDaemonThread diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h index 670197d375..03b5424bb6 100644 --- a/src/corelib/thread/qthread.h +++ b/src/corelib/thread/qthread.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -42,17 +43,10 @@ #include -// The implementation of QThread::create uses various C++14/C++17 facilities; -// we must check for their presence. Specifically for glibcxx bundled in MinGW -// with win32 threads, we check the condition found in its header -// since _GLIBCXX_HAS_GTHREADS might then not be defined. -// For std::async (used in all codepaths) -// there is no SG10 feature macro; just test for the header presence. -// For the C++17 codepath do some more throughout checks for std::invoke and -// C++14 lambdas availability. -#if QT_HAS_INCLUDE() \ - && (!defined(__GLIBCXX__) || (defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1))) -# define QTHREAD_HAS_CREATE +// For QThread::create. The configure-time test just checks for the availability +// of std::future and std::async; for the C++17 codepath we perform some extra +// checks here (for std::invoke and C++14 lambdas). +#if QT_CONFIG(cxx11_future) # include // for std::async # include // for std::invoke; no guard needed as it's a C++98 header @@ -125,16 +119,16 @@ public: template static QThread *create(Function &&f); #else -#ifdef QTHREAD_HAS_CREATE -#ifdef QTHREAD_HAS_VARIADIC_CREATE +# if QT_CONFIG(cxx11_future) +# ifdef QTHREAD_HAS_VARIADIC_CREATE template static QThread *create(Function &&f, Args &&... args); -#else +# else template static QThread *create(Function &&f); -#endif -#endif -#endif +# endif // QTHREAD_HAS_VARIADIC_CREATE +# endif // QT_CONFIG(cxx11_future) +#endif // Q_QDOC public Q_SLOTS: void start(Priority = InheritPriority); @@ -165,7 +159,7 @@ protected: private: Q_DECLARE_PRIVATE(QThread) -#ifdef QTHREAD_HAS_CREATE +#if QT_CONFIG(cxx11_future) static QThread *createThreadImpl(std::future &&future); #endif @@ -173,9 +167,9 @@ private: friend class QThreadData; }; -#ifdef QTHREAD_HAS_CREATE +#if QT_CONFIG(cxx11_future) -#ifdef QTHREAD_HAS_VARIADIC_CREATE +#if defined(QTHREAD_HAS_VARIADIC_CREATE) // C++17: std::thread's constructor complying call template QThread *QThread::create(Function &&f, Args &&... args) @@ -243,7 +237,7 @@ QThread *QThread::create(Function &&f) } #endif // QTHREAD_HAS_VARIADIC_CREATE -#endif // QTHREAD_HAS_CREATE +#endif // QT_CONFIG(cxx11_future) #else // QT_NO_THREAD -- cgit v1.2.3