1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qrunnable.h" |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | QRunnable::~QRunnable() |
45 | { |
46 | // Must be empty until ### Qt 6 |
47 | } |
48 | |
49 | /*! |
50 | \class QRunnable |
51 | \inmodule QtCore |
52 | \since 4.4 |
53 | \brief The QRunnable class is the base class for all runnable objects. |
54 | |
55 | \ingroup thread |
56 | |
57 | The QRunnable class is an interface for representing a task or |
58 | piece of code that needs to be executed, represented by your |
59 | reimplementation of the run() function. |
60 | |
61 | You can use QThreadPool to execute your code in a separate |
62 | thread. QThreadPool deletes the QRunnable automatically if |
63 | autoDelete() returns \c true (the default). Use setAutoDelete() to |
64 | change the auto-deletion flag. |
65 | |
66 | QThreadPool supports executing the same QRunnable more than once |
67 | by calling QThreadPool::tryStart(this) from within the run() function. |
68 | If autoDelete is enabled the QRunnable will be deleted when |
69 | the last thread exits the run function. Calling QThreadPool::start() |
70 | multiple times with the same QRunnable when autoDelete is enabled |
71 | creates a race condition and is not recommended. |
72 | |
73 | \sa QThreadPool |
74 | */ |
75 | |
76 | /*! \fn QRunnable::run() |
77 | Implement this pure virtual function in your subclass. |
78 | */ |
79 | |
80 | /*! \fn QRunnable::QRunnable() |
81 | Constructs a QRunnable. Auto-deletion is enabled by default. |
82 | |
83 | \sa autoDelete(), setAutoDelete() |
84 | */ |
85 | |
86 | /*! \fn QRunnable::~QRunnable() |
87 | QRunnable virtual destructor. |
88 | */ |
89 | |
90 | /*! \fn bool QRunnable::autoDelete() const |
91 | |
92 | Returns \c true is auto-deletion is enabled; false otherwise. |
93 | |
94 | If auto-deletion is enabled, QThreadPool will automatically delete |
95 | this runnable after calling run(); otherwise, ownership remains |
96 | with the application programmer. |
97 | |
98 | \sa setAutoDelete(), QThreadPool |
99 | */ |
100 | |
101 | /*! \fn bool QRunnable::setAutoDelete(bool autoDelete) |
102 | |
103 | Enables auto-deletion if \a autoDelete is true; otherwise |
104 | auto-deletion is disabled. |
105 | |
106 | If auto-deletion is enabled, QThreadPool will automatically delete |
107 | this runnable after calling run(); otherwise, ownership remains |
108 | with the application programmer. |
109 | |
110 | Note that this flag must be set before calling |
111 | QThreadPool::start(). Calling this function after |
112 | QThreadPool::start() results in undefined behavior. |
113 | |
114 | \sa autoDelete(), QThreadPool |
115 | */ |
116 | |
117 | class FunctionRunnable : public QRunnable |
118 | { |
119 | std::function<void()> m_functionToRun; |
120 | public: |
121 | FunctionRunnable(std::function<void()> functionToRun) : m_functionToRun(std::move(functionToRun)) |
122 | { |
123 | } |
124 | void run() override |
125 | { |
126 | m_functionToRun(); |
127 | } |
128 | }; |
129 | |
130 | /*! |
131 | \since 5.15 |
132 | |
133 | Creates a QRunnable that calls \a functionToRun in run(). |
134 | |
135 | Auto-deletion is enabled by default. |
136 | |
137 | \sa run(), autoDelete() |
138 | */ |
139 | QRunnable *QRunnable::create(std::function<void()> functionToRun) |
140 | { |
141 | return new FunctionRunnable(std::move(functionToRun)); |
142 | } |
143 | |
144 | QT_END_NAMESPACE |
145 | |