ArnLib  4.0.x
Active Registry Network
ArnScriptJobs.hpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #ifndef ARNSCRIPTJOBS_HPP
33 #define ARNSCRIPTJOBS_HPP
34 
35 #include "ArnLib_global.hpp"
36 #include "ArnScriptJob.hpp"
37 #include "MQFlags.hpp"
38 #include <QThread>
39 #include <QMutex>
40 #include <QObject>
41 
42 class ArnScriptWatchdogThread;
43 class ArnScriptWatchdogRun;
44 
45 
47 class ArnScriptJobThread : public QThread
48 {
49 Q_OBJECT
50 public:
51  ArnScriptJobThread( ArnScriptJobControl* jobConfig, ArnScriptJobFactory* jobFactory,
52  ArnScriptWatchdogThread* watchdogThread);
53  ~ArnScriptJobThread();
54 
55 protected:
56  void run();
57 
58 private:
59  ArnScriptJobControl* _jobConfig;
60  ArnScriptJobFactory* _jobFactory;
61  ArnScriptWatchdogThread* _watchdogThread;
62 };
63 
64 
65 class ARNLIBSHARED_EXPORT ArnScriptJobSingle : public QObject
66 {
67 Q_OBJECT
68 public:
69  ArnScriptJobSingle( ArnScriptJobControl* jobConfig, ArnScriptJobFactory* jobFactory,
70  ArnScriptWatchdogThread* watchdogThread);
71  ~ArnScriptJobSingle();
72 
73 signals:
74 
75 private slots:
76  void doScheduleRequest( int callerId);
77  void doQuitRequest( int callerId);
78  void doScriptChanged( int id);
79 
80 private:
81  void newScriptJob();
82 
83  ArnScriptJobControl* _jobConfig;
84  ArnScriptJobFactory* _jobFactory;
85  ArnScriptWatchdogThread* _watchdogThread;
86 
87  ArnScriptJob* _job;
88  int _runningId;
89  bool _scriptChanged;
90 };
91 
92 
93 #ifdef ARNUSE_SCRIPTJS
94 class ArnScriptWatchdogRun : public QObject
95 {
96 Q_OBJECT
97 public:
98  ArnScriptWatchdogRun( ArnScriptWatchdogThread& watchdogThread);
99  ~ArnScriptWatchdogRun();
100 
101 public slots:
102  void addWatchdog( ArnScriptWatchdog* watchdog);
103  void removeWatchdog( ArnScriptWatchdog* watchdog);
104 
105 private slots:
106 
107 private:
108  ArnScriptWatchdogThread* _watchdogThread = arnNullptr;
109 };
110 
111 
112 class ArnScriptWatchdogThread : public QThread
113 {
114 Q_OBJECT
115 public:
116  ArnScriptWatchdogThread( QObject* parent = arnNullptr);
117  ~ArnScriptWatchdogThread();
118 
119  void addWatchdog( ArnScriptWatchdog* watchdog);
120  void removeWatchdog( ArnScriptWatchdog* watchdog);
121 
122 signals:
123  void ready();
124 
125 protected:
126  void run();
127 
128 private:
129  void postInit();
130  void addWatchdogNow( ArnScriptWatchdog* watchdog);
131 
132  ArnScriptWatchdogRun* _watchdogRun = arnNullptr;
133  QList<ArnScriptWatchdog*> _wdTab;
134  bool _isReady = false;
135  QMutex _mutex;
136 };
137 
138 
139 #else
140 class ArnScriptWatchdogThread : public QObject
142 {
143 Q_OBJECT
144 public:
145  ArnScriptWatchdogThread( QObject* parent = arnNullptr);
146 
147  static void start();
148  static void addWatchdog( ArnScriptWatchdog* watchdog);
149  static void removeWatchdog( ArnScriptWatchdog* watchdog);
150 
151 signals:
152  void ready();
153 };
154 #endif
155 
157 
160 class ARNLIBSHARED_EXPORT ArnScriptJobs : public QObject
161 {
162  Q_OBJECT
163 public:
164  struct Type {
165  enum E {
169  };
171  };
172  explicit ArnScriptJobs( QObject* parent = arnNullptr);
173  void addJob( ArnScriptJobControl* jobConfig, int prio = 1);
174  void setFactory( ArnScriptJobFactory* jobFactory);
175  void start( Type type = Type::Cooperative);
176 
177 signals:
178 
179 private slots:
180  void doScheduleRequest( int callerId);
181  void setScriptChanged( int id);
182  void doPreemtiveStartNow();
183 
184 private:
185  struct JobSlot {
186  ArnScriptJobThread* thread;
187  ArnScriptJobControl* jobConfig;
188  ArnScriptJob* job;
189  int startPrio;
190  int curPrio;
191  bool scriptChanged;
192 
193  JobSlot()
194  {
195  startPrio = 1;
196  curPrio = 1;
197  thread = arnNullptr;
198  jobConfig = arnNullptr;
199  job = arnNullptr;
200  scriptChanged = false;
201  }
202  };
203  QList<JobSlot> _jobSlots;
204  QMap<int,int> _idToSlot;
205 
206  void doCooperativeStart();
207  void doPreemtiveStart();
208  void newScriptJob( JobSlot& jobSlot);
209 
210  Type _type;
211  int _runningId;
212  int _runningIndex;
213  ArnScriptJobFactory* _jobFactory;
214  ArnScriptWatchdogThread* _watchdogThread;
215 };
216 
217 #endif // ARNSCRIPTJOBS_HPP
Must be thread-safe as subclassed.
Is thread-safe (except doSetupJob)
Interface class to be normally used, is also Script Job interface.
#define ARNLIBSHARED_EXPORT
#define MQ_DECLARE_ENUM(EStruct)
Enums.
Definition: MQFlagsBase.hpp:70