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

0001 /* -*- C++ -*-
0002     This file implements the state handling 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     $Id: State.h 32 2005-08-17 08:38:01Z mirko $
0009 */
0010 
0011 #ifndef THREADWEAVER_STATE_H
0012 #define THREADWEAVER_STATE_H
0013 
0014 #include <QString>
0015 
0016 #include "queueinterface.h"
0017 #include "queuesignals.h"
0018 #include "threadweaver_export.h"
0019 #include "weaverinterface.h"
0020 
0021 namespace ThreadWeaver
0022 {
0023 class Job;
0024 class Thread;
0025 
0026 /** All weaver objects maintain a state of operation which can be
0027  *  queried by the application. See the threadweaver documentation on
0028  *  how the different states are related.
0029  */
0030 
0031 enum StateId {
0032     /** The object is in the state of construction and has not yet
0033      *  started to process jobs. */
0034     InConstruction = 0,
0035     /** Jobs are being processed. */
0036     WorkingHard,
0037     /** Job processing is suspended, but some jobs which where already
0038      *  in progress are not finished yet. */
0039     Suspending,
0040     /** Job processing is suspended, and no jobs are being
0041      *  processed. */
0042     Suspended,
0043     /** The object is being destructed. Jobs might still be processed,
0044      *  the destructor will wait for all threads to exit and then
0045      *  end. */
0046     ShuttingDown,
0047     /** The object is being destructed, and all threads have
0048      *  exited. No jobs are handled anymore. */
0049     Destructed,
0050     /** Not a state, but a sentinel for the number of defined states. */
0051     NoOfStates,
0052 };
0053 
0054 /** We use a State pattern to handle the system state in ThreadWeaver. */
0055 class THREADWEAVER_EXPORT State : public QueueInterface, public WeaverInterface
0056 {
0057 public:
0058     /** Default constructor. */
0059     explicit State(QueueSignals *weaver);
0060 
0061     /** Destructor. */
0062     ~State() override;
0063 
0064     /** The ID of the current state.
0065      *  @see StateNames, StateID
0066      */
0067     QString stateName() const;
0068 
0069     /** The state Id. */
0070     virtual StateId stateId() const = 0;
0071 
0072     /** The state has been changed so that this object is responsible for
0073      *  state handling. */
0074     virtual void activated();
0075 
0076 protected:
0077     /** The Weaver interface this state handles. */
0078     virtual QueueInterface *weaver();
0079     virtual const QueueInterface *weaver() const;
0080 
0081 private:
0082     class Private;
0083     Private *const d;
0084 };
0085 
0086 }
0087 
0088 #endif // THREADWEAVER_STATE_H