File indexing completed on 2024-04-21 04:01:27

0001 /* -*- C++ -*-
0002     Base class for job decorators in ThreadWeaver.
0003 
0004     SPDX-FileCopyrightText: 2005-2013 Mirko Boehm <mirko@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "collection.h"
0010 #include "managedjobpointer.h"
0011 #include "sequence.h"
0012 
0013 #include "iddecorator.h"
0014 
0015 namespace
0016 {
0017 const quintptr IdDecorator_AutoDelete = 1;
0018 
0019 }
0020 
0021 namespace ThreadWeaver
0022 {
0023 // Pssst: IdDecorator uses the d pointer to hold decoratee. It also uses d2 as a bitfield to store the
0024 // autoDelete setting. The goal is not to require a dynamic allocation on creation.
0025 IdDecorator::IdDecorator(JobInterface *decoratee, bool autoDelete)
0026     : d1(reinterpret_cast<Private1 *>(decoratee))
0027     , d2(nullptr)
0028 {
0029     setAutoDelete(autoDelete);
0030 }
0031 
0032 IdDecorator::~IdDecorator()
0033 {
0034     // Do not assert here. IdDecorator can decorate a null pointer. Only assert if a method is called on a decorated
0035     // null  pointer.
0036     if (autoDelete()) {
0037         delete job();
0038     }
0039 }
0040 
0041 QMutex *IdDecorator::mutex() const
0042 {
0043     Q_ASSERT(d1);
0044     return job()->mutex();
0045 }
0046 
0047 void IdDecorator::run(JobPointer self, Thread *thread)
0048 {
0049     Q_ASSERT(d1);
0050     job()->run(self, thread);
0051 }
0052 
0053 void IdDecorator::defaultBegin(const JobPointer &self, Thread *thread)
0054 {
0055     Q_ASSERT(d1);
0056     job()->defaultBegin(self, thread);
0057 }
0058 
0059 void IdDecorator::defaultEnd(const JobPointer &self, Thread *thread)
0060 {
0061     Q_ASSERT(d1);
0062     job()->defaultEnd(self, thread);
0063 }
0064 
0065 void IdDecorator::removeQueuePolicy(QueuePolicy *policy)
0066 {
0067     Q_ASSERT(d1);
0068     job()->removeQueuePolicy(policy);
0069 }
0070 
0071 QList<QueuePolicy *> IdDecorator::queuePolicies() const
0072 {
0073     Q_ASSERT(d1);
0074     return job()->queuePolicies();
0075 }
0076 
0077 void IdDecorator::assignQueuePolicy(QueuePolicy *policy)
0078 {
0079     Q_ASSERT(d1);
0080     job()->assignQueuePolicy(policy);
0081 }
0082 
0083 bool IdDecorator::isFinished() const
0084 {
0085     Q_ASSERT(d1);
0086     return job()->isFinished();
0087 }
0088 
0089 void IdDecorator::aboutToBeQueued(QueueAPI *api)
0090 {
0091     Q_ASSERT(d1);
0092     job()->aboutToBeQueued(api);
0093 }
0094 
0095 void IdDecorator::aboutToBeQueued_locked(QueueAPI *api)
0096 {
0097     Q_ASSERT(d1);
0098     job()->aboutToBeQueued_locked(api);
0099 }
0100 
0101 void IdDecorator::aboutToBeDequeued(QueueAPI *api)
0102 {
0103     Q_ASSERT(d1);
0104     job()->aboutToBeDequeued(api);
0105 }
0106 
0107 void IdDecorator::aboutToBeDequeued_locked(QueueAPI *api)
0108 {
0109     Q_ASSERT(d1);
0110     job()->aboutToBeDequeued_locked(api);
0111 }
0112 
0113 void IdDecorator::requestAbort()
0114 {
0115     Q_ASSERT(d1);
0116     job()->requestAbort();
0117 }
0118 
0119 bool IdDecorator::success() const
0120 {
0121     Q_ASSERT(d1);
0122     return job()->success();
0123 }
0124 
0125 int IdDecorator::priority() const
0126 {
0127     Q_ASSERT(d1);
0128     return job()->priority();
0129 }
0130 
0131 void IdDecorator::setStatus(JobInterface::Status status)
0132 {
0133     Q_ASSERT(d1);
0134     job()->setStatus(status);
0135 }
0136 
0137 JobInterface::Status IdDecorator::status() const
0138 {
0139     Q_ASSERT(d1);
0140     return job()->status();
0141 }
0142 
0143 Executor *IdDecorator::executor() const
0144 {
0145     Q_ASSERT(d1);
0146     return job()->executor();
0147 }
0148 
0149 Executor *IdDecorator::setExecutor(Executor *executor)
0150 {
0151     Q_ASSERT(d1);
0152     return job()->setExecutor(executor);
0153 }
0154 
0155 void IdDecorator::execute(const JobPointer &self, ThreadWeaver::Thread *thread)
0156 {
0157     Q_ASSERT(d1);
0158     job()->execute(self, thread);
0159 }
0160 
0161 void IdDecorator::blockingExecute()
0162 {
0163     Q_ASSERT(d1);
0164     job()->blockingExecute();
0165 }
0166 
0167 const ThreadWeaver::JobInterface *IdDecorator::job() const
0168 {
0169     return reinterpret_cast<JobInterface *>(d1);
0170 }
0171 
0172 JobInterface *IdDecorator::job()
0173 {
0174     return reinterpret_cast<JobInterface *>(d1);
0175 }
0176 
0177 void IdDecorator::setAutoDelete(bool onOff)
0178 {
0179     if (onOff) {
0180         d2 = reinterpret_cast<IdDecorator::Private2 *>(IdDecorator_AutoDelete);
0181     } else {
0182         d2 = nullptr;
0183     }
0184 }
0185 
0186 bool IdDecorator::autoDelete() const
0187 {
0188     return d2 == reinterpret_cast<IdDecorator::Private2 *>(IdDecorator_AutoDelete);
0189 }
0190 
0191 const ThreadWeaver::Collection *IdDecorator::collection() const
0192 {
0193     return dynamic_cast<const Collection *>(job());
0194 }
0195 
0196 Collection *IdDecorator::collection()
0197 {
0198     return dynamic_cast<Collection *>(job());
0199 }
0200 
0201 const Sequence *IdDecorator::sequence() const
0202 {
0203     return dynamic_cast<const Sequence *>(job());
0204 }
0205 
0206 Sequence *IdDecorator::sequence()
0207 {
0208     return dynamic_cast<Sequence *>(job());
0209 }
0210 
0211 }