aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-09-07 11:33:22 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-09-07 13:18:30 +0200
commit6541627d63428f8e6315d992180ce8855c246668 (patch)
tree87a016a552a0cbb7c537b7b0b7d397974edec119
parentd3877ec7445bc7719aff233bae16243b563d5a04 (diff)
PySide6: Add QThreadPool().start(std::function)/tryStart(std::function)
[ChangeLog][PySide6] QThreadPool().start(std::function) and tryStart(std::function) have been added. Change-Id: I01fc3b743d97e11375c20391fb6e28b03d1ceac0 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r--sources/pyside6/PySide6/QtCore/typesystem_core_common.xml16
-rw-r--r--sources/pyside6/PySide6/glue/qtcore.cpp22
-rw-r--r--sources/pyside6/tests/QtCore/bug_927.py18
3 files changed, 55 insertions, 1 deletions
diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml
index 6d8d15401..b10138d41 100644
--- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml
+++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml
@@ -2863,11 +2863,27 @@
<parent index="this" action="add"/>
</modify-argument>
</modify-function>
+ <add-function signature="start(PyCallable,int @priority@=0)">
+ <inject-code class="target" position="beginning"
+ file="../glue/qtcore.cpp"
+ snippet="std-function-void-lambda"/>
+ <inject-code class="target" position="beginning"
+ file="../glue/qtcore.cpp"
+ snippet="qthreadpool-start"/>
+ </add-function>
<modify-function signature="tryStart(QRunnable*)" allow-thread="yes">
<modify-argument index="1">
<parent index="this" action="add"/>
</modify-argument>
</modify-function>
+ <add-function signature="tryStart(PyCallable)" return-type="bool">
+ <inject-code class="target" position="beginning"
+ file="../glue/qtcore.cpp"
+ snippet="std-function-void-lambda"/>
+ <inject-code class="target" position="beginning"
+ file="../glue/qtcore.cpp"
+ snippet="qthreadpool-trystart"/>
+ </add-function>
<modify-function signature="tryTake(QRunnable*)" allow-thread="yes"/>
<modify-function signature="globalInstance()" >
diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp
index 11fc59cf7..394e1eae1 100644
--- a/sources/pyside6/PySide6/glue/qtcore.cpp
+++ b/sources/pyside6/PySide6/glue/qtcore.cpp
@@ -1971,3 +1971,25 @@ Py_DECREF(suffix);
%PYARG_0 = Shiboken::Buffer::newObject(%CPPSELF.%FUNCTION_NAME(), %CPPSELF.size(),
Shiboken::Buffer::ReadWrite);
// @snippet qsharedmemory_data_readwrite
+
+// @snippet std-function-void-lambda
+auto *callable = %PYARG_1;
+auto cppCallback = [callable]()
+{
+ Shiboken::GilState state;
+ Shiboken::AutoDecRef arglist(PyTuple_New(0));
+ Shiboken::AutoDecRef ret(PyObject_CallObject(callable, arglist));
+ Py_DECREF(callable);
+};
+// @snippet std-function-void-lambda
+
+// @snippet qthreadpool-start
+Py_INCREF(callable);
+%CPPSELF.%FUNCTION_NAME(cppCallback, %2);
+// @snippet qthreadpool-start
+
+// @snippet qthreadpool-trystart
+Py_INCREF(callable);
+%RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(cppCallback);
+%PYARG_0 = %CONVERTTOPYTHON[int](cppResult);
+// @snippet qthreadpool-trystart
diff --git a/sources/pyside6/tests/QtCore/bug_927.py b/sources/pyside6/tests/QtCore/bug_927.py
index 5a513b83a..f7dd21841 100644
--- a/sources/pyside6/tests/QtCore/bug_927.py
+++ b/sources/pyside6/tests/QtCore/bug_927.py
@@ -39,14 +39,23 @@ init_test_paths(False)
from PySide6.QtCore import QRunnable, QThread, QThreadPool
+thread_function_called = False
+
+
+class thread_function():
+ global thread_function_called
+ thread_function_called = True
+
+
class Task(QRunnable):
def run(self):
QThread.sleep(2) # Sleep 2 seconds
class QThreadPoolTest(unittest.TestCase):
- '''This used to cause a segfault due the ownership control on globalInstance function '''
def testSlowJobs(self):
+ '''This used to cause a segfault due the ownership control on
+ globalInstance function'''
for i in range(3):
task = Task()
QThreadPool.globalInstance().start(task)
@@ -54,6 +63,13 @@ class QThreadPoolTest(unittest.TestCase):
QThreadPool.globalInstance().waitForDone()
+ def testCallable(self):
+ global thread_function_called
+ tp = QThreadPool.globalInstance()
+ tp.start(thread_function)
+ tp.waitForDone()
+ self.assertTrue(thread_function_called)
+
if __name__ == '__main__':
unittest.main()