| 1 | /* -*- C++ -*- |
| 2 | This file is part of ThreadWeaver. It declares the Thread class. |
| 3 | |
| 4 | SPDX-FileCopyrightText: 2004-2013 Mirko Boehm <mirko@kde.org> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef THREADWEAVER_THREAD_H |
| 10 | #define THREADWEAVER_THREAD_H |
| 11 | |
| 12 | #include <QMutex> |
| 13 | #include <QThread> |
| 14 | |
| 15 | #include "jobpointer.h" |
| 16 | #include "threadweaver_export.h" |
| 17 | |
| 18 | namespace ThreadWeaver |
| 19 | { |
| 20 | class Job; |
| 21 | class Weaver; |
| 22 | |
| 23 | /*! |
| 24 | * \class ThreadWeaver::Thread |
| 25 | * \inheaderfile ThreadWeaver/Thread |
| 26 | * \inmodule ThreadWeaver |
| 27 | * |
| 28 | * \brief Thread represents a worker thread in a Queue's inventory. |
| 29 | * |
| 30 | * Threads are created and managed by queues on demand. A Thread will try to retrieve and process |
| 31 | * jobs from the queue until it is told to exit. |
| 32 | */ |
| 33 | class THREADWEAVER_EXPORT Thread : public QThread |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | public: |
| 37 | /*! \brief Create a thread. |
| 38 | * |
| 39 | * \a parent the parent Weaver |
| 40 | */ |
| 41 | explicit Thread(Weaver *parent = nullptr); |
| 42 | |
| 43 | /*! The destructor. */ |
| 44 | ~Thread() override; |
| 45 | |
| 46 | /*! \brief The run method is reimplemented to execute jobs from the queue. |
| 47 | * |
| 48 | * 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 |
| 49 | * pointer. When a null pointer is returned, it tells the thread to exit. |
| 50 | */ |
| 51 | void run() override; |
| 52 | |
| 53 | /*! \brief Returns the thread id. |
| 54 | * |
| 55 | * This id marks the respective Thread object, and must therefore not be confused with, e.g., the pthread thread ID. |
| 56 | * The way threads are implemented and identified is platform specific. id() is the only way to uniquely identify a thread |
| 57 | * within ThreadWeaver. |
| 58 | */ |
| 59 | unsigned int id() const; |
| 60 | |
| 61 | /*! \brief Request the abortion of the job that is processed currently. |
| 62 | * |
| 63 | * If there is no current job, this method will do nothing, but can safely be called. It forwards the request to the |
| 64 | * current Job. |
| 65 | */ |
| 66 | void requestAbort(); |
| 67 | |
| 68 | private: |
| 69 | class Private; |
| 70 | Private *const d; |
| 71 | }; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | #endif |
| 76 | |