Qx v0.5.7
Qt Extensions Library
Loading...
Searching...
No Matches
qx-processbider_p.h
1#ifndef QX_PROCCESSBIDER_P_H
2#define QX_PROCCESSBIDER_P_H
3
4// Qt Includes
5#include <QObject>
6#include <QThread>
7#include <QBasicTimer>
8#include <QMutex>
9
10// Inter-component Includes
11#include <qx/core/qx-exclusiveaccess.h>
12
13// Project Includes
14/* The split headers like this are a touch messy as they rely on the public method's of all implementations being the same
15 * without a true base class, but this way avoids unnecessary virtual dispatch overhead that would be caused by
16 * using polymorphism when it's not needed (since the derived is known at compile time), and somewhat equally kludgey
17 * templates that would still rely on a fare amount of #ifdefs anyway
18 */
19#ifdef _WIN32
20#include "__private/qx-processwaiter_win.h"
21#endif
22#ifdef __linux__
23#include "__private/qx-processwaiter_linux.h"
24#endif
25
26namespace Qx
27{
30class ProcessBiderWorker : public QObject
31{
32 Q_OBJECT
33//-Class Types----------------------------------------------------------------------------------------------
34public:
35 enum Outcome { HookFail, GraceExpired, Abandoned };
36
37//-Instance Members------------------------------------------------------------------------------------------
38private:
39 // Data
40 QString mName;
41#ifdef __linux__
42 std::chrono::milliseconds mPollRate;
43#endif
44 std::chrono::milliseconds mGrace;
45 bool mStartWithGrace;
46
47 // Function
48 QBasicTimer mGraceTimer;
49 ProcessWaiter mWaiter;
50 bool mComplete;
51
52//-Constructor----------------------------------------------------------------------------------------------
53public:
54 explicit ProcessBiderWorker();
55
56//-Instance Functions----------------------------------------------------------------------------------------------
57private:
58 void startWait(quint32 pid);
59 void startGrace();
60 void timerEvent(QTimerEvent* event) override;
61 void handleGraceEnd(bool retry);
62 void finish(Outcome outcome);
63
64public:
65 void setProcessName(const QString& name);
66#ifdef __linux__
67 void setPollRate(std::chrono::milliseconds rate);
68#endif
69 void setGrace(std::chrono::milliseconds grace);
70 void setStartWithGrace(bool graceFirst);
71
72//-Slots------------------------------------------------------------------------------------------------------------
73private slots:
74 void handleProcesStop();
75
76public slots:
77 void handleAbort();
78 void handleClosure(std::chrono::milliseconds timeout, bool force);
79 void bide();
80
81//-Signals------------------------------------------------------------------------------------------------------------
82signals:
83 void processHooked();
84 void processStopped();
85 void processCloseFailed();
86 void graceStarted();
87 void complete(Outcome outcome);
88};
89
90class ProcessBider;
91
92class ProcessBiderManager
93{
94//-Class Members---------------------------------------------------------------------------------------------
95private:
96 // Needs to be static so it can be locked before the the singleton is created, or else a race in instance() could occur.
97 static inline constinit QMutex smMutex;
98
99//-Instance Members------------------------------------------------------------------------------------------
100private:
101 QThread* mThread;
102 int mWorkerCount;
103
104//-Constructor----------------------------------------------------------------------------------------------
105private:
106 explicit ProcessBiderManager();
107
108//-Destructor----------------------------------------------------------------------------------------------
109public:
110 ~ProcessBiderManager();
111
112//-Class Functions----------------------------------------------------------------------------------------------
113public:
115
116//-Instance Functions----------------------------------------------------------------------------------------------
117private:
118 void startThreadIfStopped();
119 void stopThreadIfStarted(bool wait = false);
120
121public:
122 void registerBider(ProcessBider* bider);
123 void notifyWorkerFinished();
124
126};
127
128}
129
130#endif // QX_PROCCESSBIDER_P_H
The ExclusiveAccess template class is a convenience class that simplifies access to resources secured...
Definition qx-exclusiveaccess.h:16
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-processwaiter.cpp:5