1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2012-01-13
 * Description : Multithreaded worker object
 *
 * SPDX-FileCopyrightText: 2010-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "parallelworkers.h"

// Qt includes

#include <QCoreApplication>
#include <QEvent>
#include <QMetaMethod>
#include <QMutex>
#include <QThread>
#include <QMetaType>
#include <QWaitCondition>

// Local includes

#include "digikam_debug.h"
#include "threadmanager.h"

namespace Digikam
{

ParallelWorkers::~ParallelWorkers()
{
    Q_FOREACH (WorkerObject* const object, m_workers)
    {
        delete object;
    }

    delete m_replacementMetaObject;
}

int ParallelWorkers::optimalWorkerCount()
{
    return (qMax(1, QThread::idealThreadCount()));
}

bool ParallelWorkers::optimalWorkerCountReached() const
{
    return (m_workers.size() >= optimalWorkerCount());
}

void ParallelWorkers::schedule()
{
    Q_FOREACH (WorkerObject* const object, m_workers)
    {
        object->schedule();
    }
}

void ParallelWorkers::deactivate(WorkerObject::DeactivatingMode mode)
{
    Q_FOREACH (WorkerObject* const object, m_workers)
    {
        object->deactivate(mode);
    }
}

void ParallelWorkers::wait()
{
    Q_FOREACH (WorkerObject* const object, m_workers)
    {
        object->wait();
    }
}

void ParallelWorkers::setPriority(QThread::Priority priority)
{
    Q_FOREACH (WorkerObject* const object, m_workers)
    {
        object->setPriority(priority);
    }
}

void ParallelWorkers::add(WorkerObject* const worker)<--- Parameter 'worker' can be declared as pointer to const
{
/*
    if (!asQObject()->inherits(worker->metaObject()->className()))
    {
        qCDebug(DIGIKAM_GENERAL_LOG) << "You need to derive the ParallelWorkers class from the WorkerObject you want to use";
        return;
    }

    QMetaObject* const meta = asQObject()->metaObject();

    for (int i = 0 ; i < meta->methodCount() ; ++i)
    {
        QMetaMethod method = meta->method(index);
        if (!method->methodType() == QMetaMethod::
    }
*/

    m_workers << worker;
}

/*
bool ParallelWorkers::connect(const QObject* sender, const char* signal,
                              const char* method,
                              Qt::ConnectionType type) const
{
    Q_FOREACH (WorkerObject* object, m_workers)
    {
        if (!WorkerObject::connect(sender, signal, object, method, type))
        {
            return false;
        }
    }

    return true;
}
*/

bool ParallelWorkers::connect(const char* const signal,
                              const QObject* const receiver,
                              const char* const method,
                              Qt::ConnectionType type) const
{
    Q_FOREACH (WorkerObject* const object, m_workers)
    {
        if (!QObject::connect(object, signal, receiver, method, type))
        {
            return false;
        }
    }

    return true;
}

int ParallelWorkers::replacementStaticQtMetacall(QMetaObject::Call _c, int _id, void **_a)
{
    if (_c == QMetaObject::InvokeMetaMethod)
    {
        // This is the common ancestor's meta object, below WorkerObject

        const QMetaObject* const mobj = mocMetaObject();
        const int properMethods       = mobj->methodCount() - mobj->methodOffset();

        if (_id >= properMethods)
        {
            return (_id - properMethods);
        }

        // Get the relevant meta method. I'm not quite sure if this is rock solid.

        QMetaMethod method            = mobj->method(_id + mobj->methodOffset());

        // Copy the argument data - _a is going to be deleted in our current thread

        QList<QByteArray> types       = method.parameterTypes();
        QVector<QGenericArgument> args(10);                         // clazy:exclude=missing-typeinfo

        for (int i = 0 ; i < types.size() ; ++i)
        {

#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))

            int typeId = QMetaType::fromName(QByteArrayView(types[i].constData())).id();

#else

            int typeId = QMetaType::type(types[i].constData());

#endif

            if (!typeId && _a[i+1])
            {
                qCWarning(DIGIKAM_GENERAL_LOG) << "Unable to handle unregistered datatype" << types[i] << "Dropping signal.";
                return (_id - properMethods);
            }

            // We use QMetaType to copy the data. _a[0] is reserved for a return parameter.

#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))

            void* const data = QMetaType(typeId).create(_a[i+1]);

#else

            void* const data = QMetaType::create(typeId, _a[i+1]);

#endif

            args[i]          = QGenericArgument(types[i].constData(), data);
        }

        // Find the object to be invoked

        WorkerObject* const obj = m_workers.at(m_currentIndex);

        if (++m_currentIndex == m_workers.size())
        {
            m_currentIndex = 0;
        }

        obj->schedule();

        // Invoke across-thread

        method.invoke(obj, Qt::QueuedConnection,
                      args[0],
                      args[1],
                      args[2],
                      args[3],
                      args[4],
                      args[5],
                      args[6],
                      args[7],
                      args[8],
                      args[9]);

        return (_id - properMethods); // this return is used by replacementQtMetacall
    }
    else
    {
        m_originalStaticMetacall(asQObject(), _c, _id, _a);
    }

    return _id; // this return will be ignored (qt_static_metacall is void)
}

int ParallelWorkers::replacementQtMetacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = WorkerObjectQtMetacall(_c, _id, _a);

    if (_id < 0)
    {
        return _id;
    }

    if (_c == QMetaObject::InvokeMetaMethod)
    {
        return replacementStaticQtMetacall(_c, _id, _a);
    }

    return _id;
}

const QMetaObject* ParallelWorkers::replacementMetaObject() const
{
    if (!m_replacementMetaObject)
    {
        QMetaObject* const rmo                 = new QMetaObject(*mocMetaObject());
        ParallelWorkers* const nonConstThis    = const_cast<ParallelWorkers*>(this);
        nonConstThis->m_originalStaticMetacall = rmo->d.static_metacall;
        rmo->d.static_metacall                 = nonConstThis->staticMetacallPointer();
        nonConstThis->m_replacementMetaObject  = rmo;
    }

    return m_replacementMetaObject;
}

} // namespace Digikam