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

0001 /* -*- C++ -*-
0002     This file is part of ThreadWeaver. It declares the Thread class.
0003 
0004     SPDX-FileCopyrightText: 2004-2013 Mirko Boehm <mirko@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef THREADWEAVER_THREAD_H
0010 #define THREADWEAVER_THREAD_H
0011 
0012 #include <QMutex>
0013 #include <QThread>
0014 
0015 #include "jobpointer.h"
0016 #include "threadweaver_export.h"
0017 
0018 namespace ThreadWeaver
0019 {
0020 class Job;
0021 class Weaver;
0022 
0023 /** @brief Thread represents a worker thread in a Queue's inventory.
0024  *
0025  * Threads are created and managed by queues on demand. A Thread will try to retrieve and process
0026  * jobs from the queue until it is told to exit. */
0027 class THREADWEAVER_EXPORT Thread : public QThread
0028 {
0029     Q_OBJECT
0030 public:
0031     /** @brief Create a thread.
0032      *
0033      *  @param parent the parent Weaver
0034      */
0035     explicit Thread(Weaver *parent = nullptr);
0036 
0037     /** The destructor. */
0038     ~Thread() override;
0039 
0040     /** @brief The run method is reimplemented to execute jobs from the queue.
0041      *
0042      * Whenever the thread is idle, it will ask its Weaver parent for a Job to do. The Weaver will either return a Job or a null
0043      * pointer. When a null pointer is returned, it tells the thread to exit.
0044      */
0045     void run() override;
0046 
0047     /** @brief Returns the thread id.
0048      *
0049      * This id marks the respective Thread object, and must therefore not be confused with, e.g., the pthread thread ID.
0050      * The way threads are implemented and identified is platform specific. id() is the only way to uniquely identify a thread
0051      * within ThreadWeaver.
0052      */
0053     unsigned int id() const;
0054 
0055     /** @brief Request the abortion of the job that is processed currently.
0056      *
0057      * If there is no current job, this method will do nothing, but can safely be called. It forwards the request to the
0058      * current Job.
0059      */
0060     void requestAbort();
0061 
0062 private:
0063     class Private;
0064     Private *const d;
0065 };
0066 
0067 }
0068 
0069 #endif