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 | } |
47 | |
48 | /*! |
49 | \class QRunnable |
50 | \inmodule QtCore |
51 | \since 4.4 |
52 | \brief The QRunnable class is the base class for all runnable objects. |
53 | |
54 | \ingroup thread |
55 | |
56 | The QRunnable class is an interface for representing a task or |
57 | piece of code that needs to be executed, represented by your |
58 | reimplementation of the run() function. |
59 | |
60 | You can use QThreadPool to execute your code in a separate |
61 | thread. QThreadPool deletes the QRunnable automatically if |
62 | autoDelete() returns \c true (the default). Use setAutoDelete() to |
63 | change the auto-deletion flag. |
64 | |
65 | QThreadPool supports executing the same QRunnable more than once |
66 | by calling QThreadPool::tryStart(this) from within the run() function. |
67 | If autoDelete is enabled the QRunnable will be deleted when |
68 | the last thread exits the run function. Calling QThreadPool::start() |
69 | multiple times with the same QRunnable when autoDelete is enabled |
70 | creates a race condition and is not recommended. |
71 | |
72 | \sa QThreadPool |
73 | */ |
74 | |
75 | /*! \fn QRunnable::run() |
76 | Implement this pure virtual function in your subclass. |
77 | */ |
78 | |
79 | /*! \fn QRunnable::QRunnable() |
80 | Constructs a QRunnable. Auto-deletion is enabled by default. |
81 | |
82 | \sa autoDelete(), setAutoDelete() |
83 | */ |
84 | |
85 | /*! \fn QRunnable::~QRunnable() |
86 | QRunnable virtual destructor. |
87 | */ |
88 | |
89 | /*! \fn bool QRunnable::autoDelete() const |
90 | |
91 | Returns \c true is auto-deletion is enabled; false otherwise. |
92 | |
93 | If auto-deletion is enabled, QThreadPool will automatically delete |
94 | this runnable after calling run(); otherwise, ownership remains |
95 | with the application programmer. |
96 | |
97 | \sa setAutoDelete(), QThreadPool |
98 | */ |
99 | |
100 | /*! \fn bool QRunnable::setAutoDelete(bool autoDelete) |
101 | |
102 | Enables auto-deletion if \a autoDelete is true; otherwise |
103 | auto-deletion is disabled. |
104 | |
105 | If auto-deletion is enabled, QThreadPool will automatically delete |
106 | this runnable after calling run(); otherwise, ownership remains |
107 | with the application programmer. |
108 | |
109 | Note that this flag must be set before calling |
110 | QThreadPool::start(). Calling this function after |
111 | QThreadPool::start() results in undefined behavior. |
112 | |
113 | \sa autoDelete(), QThreadPool |
114 | */ |
115 | |
116 | class FunctionRunnable : public QRunnable |
117 | { |
118 | std::function<void()> m_functionToRun; |
119 | public: |
120 | FunctionRunnable(std::function<void()> functionToRun) : m_functionToRun(std::move(functionToRun)) |
121 | { |
122 | } |
123 | void run() override |
124 | { |
125 | m_functionToRun(); |
126 | } |
127 | }; |
128 | |
129 | /*! |
130 | \since 5.15 |
131 | |
132 | Creates a QRunnable that calls \a functionToRun in run(). |
133 | |
134 | Auto-deletion is enabled by default. |
135 | |
136 | \sa run(), autoDelete() |
137 | */ |
138 | QRunnable *QRunnable::create(std::function<void()> functionToRun) |
139 | { |
140 | return new FunctionRunnable(std::move(functionToRun)); |
141 | } |
142 | |
143 | QT_END_NAMESPACE |
144 | |