1/* -*- C++ -*-
2 Base class for job decorators in ThreadWeaver.
3
4 SPDX-FileCopyrightText: 2005-2013 Mirko Boehm <mirko@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "collection.h"
10#include "managedjobpointer.h"
11#include "sequence.h"
12
13#include "iddecorator.h"
14
15namespace
16{
17const quintptr IdDecorator_AutoDelete = 1;
18
19}
20
21namespace ThreadWeaver
22{
23// Pssst: IdDecorator uses the d pointer to hold decoratee. It also uses d2 as a bitfield to store the
24// autoDelete setting. The goal is not to require a dynamic allocation on creation.
25IdDecorator::IdDecorator(JobInterface *decoratee, bool autoDelete)
26 : d1(reinterpret_cast<Private1 *>(decoratee))
27 , d2(nullptr)
28{
29 setAutoDelete(autoDelete);
30}
31
32IdDecorator::~IdDecorator()
33{
34 // Do not assert here. IdDecorator can decorate a null pointer. Only assert if a method is called on a decorated
35 // null pointer.
36 if (autoDelete()) {
37 delete job();
38 }
39}
40
41QMutex *IdDecorator::mutex() const
42{
43 Q_ASSERT(d1);
44 return job()->mutex();
45}
46
47void IdDecorator::run(JobPointer self, Thread *thread)
48{
49 Q_ASSERT(d1);
50 job()->run(self, thread);
51}
52
53void IdDecorator::defaultBegin(const JobPointer &self, Thread *thread)
54{
55 Q_ASSERT(d1);
56 job()->defaultBegin(job: self, thread);
57}
58
59void IdDecorator::defaultEnd(const JobPointer &self, Thread *thread)
60{
61 Q_ASSERT(d1);
62 job()->defaultEnd(job: self, thread);
63}
64
65void IdDecorator::removeQueuePolicy(QueuePolicy *policy)
66{
67 Q_ASSERT(d1);
68 job()->removeQueuePolicy(policy);
69}
70
71QList<QueuePolicy *> IdDecorator::queuePolicies() const
72{
73 Q_ASSERT(d1);
74 return job()->queuePolicies();
75}
76
77void IdDecorator::assignQueuePolicy(QueuePolicy *policy)
78{
79 Q_ASSERT(d1);
80 job()->assignQueuePolicy(policy);
81}
82
83bool IdDecorator::isFinished() const
84{
85 Q_ASSERT(d1);
86 return job()->isFinished();
87}
88
89void IdDecorator::aboutToBeQueued(QueueAPI *api)
90{
91 Q_ASSERT(d1);
92 job()->aboutToBeQueued(api);
93}
94
95void IdDecorator::aboutToBeQueued_locked(QueueAPI *api)
96{
97 Q_ASSERT(d1);
98 job()->aboutToBeQueued_locked(api);
99}
100
101void IdDecorator::aboutToBeDequeued(QueueAPI *api)
102{
103 Q_ASSERT(d1);
104 job()->aboutToBeDequeued(api);
105}
106
107void IdDecorator::aboutToBeDequeued_locked(QueueAPI *api)
108{
109 Q_ASSERT(d1);
110 job()->aboutToBeDequeued_locked(api);
111}
112
113void IdDecorator::requestAbort()
114{
115 Q_ASSERT(d1);
116 job()->requestAbort();
117}
118
119bool IdDecorator::success() const
120{
121 Q_ASSERT(d1);
122 return job()->success();
123}
124
125int IdDecorator::priority() const
126{
127 Q_ASSERT(d1);
128 return job()->priority();
129}
130
131void IdDecorator::setStatus(JobInterface::Status status)
132{
133 Q_ASSERT(d1);
134 job()->setStatus(status);
135}
136
137JobInterface::Status IdDecorator::status() const
138{
139 Q_ASSERT(d1);
140 return job()->status();
141}
142
143Executor *IdDecorator::executor() const
144{
145 Q_ASSERT(d1);
146 return job()->executor();
147}
148
149Executor *IdDecorator::setExecutor(Executor *executor)
150{
151 Q_ASSERT(d1);
152 return job()->setExecutor(executor);
153}
154
155void IdDecorator::execute(const JobPointer &self, ThreadWeaver::Thread *thread)
156{
157 Q_ASSERT(d1);
158 job()->execute(job: self, thread);
159}
160
161void IdDecorator::blockingExecute()
162{
163 Q_ASSERT(d1);
164 job()->blockingExecute();
165}
166
167const ThreadWeaver::JobInterface *IdDecorator::job() const
168{
169 return reinterpret_cast<JobInterface *>(d1);
170}
171
172JobInterface *IdDecorator::job()
173{
174 return reinterpret_cast<JobInterface *>(d1);
175}
176
177void IdDecorator::setAutoDelete(bool onOff)
178{
179 if (onOff) {
180 d2 = reinterpret_cast<IdDecorator::Private2 *>(IdDecorator_AutoDelete);
181 } else {
182 d2 = nullptr;
183 }
184}
185
186bool IdDecorator::autoDelete() const
187{
188 return d2 == reinterpret_cast<IdDecorator::Private2 *>(IdDecorator_AutoDelete);
189}
190
191const ThreadWeaver::Collection *IdDecorator::collection() const
192{
193 return dynamic_cast<const Collection *>(job());
194}
195
196Collection *IdDecorator::collection()
197{
198 return dynamic_cast<Collection *>(job());
199}
200
201const Sequence *IdDecorator::sequence() const
202{
203 return dynamic_cast<const Sequence *>(job());
204}
205
206Sequence *IdDecorator::sequence()
207{
208 return dynamic_cast<Sequence *>(job());
209}
210
211}
212

source code of threadweaver/src/iddecorator.cpp