trikRuntime
threading.h
Go to the documentation of this file.
1 /* Copyright 2014 CyberTech Labs Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14 
15 #pragma once
16 
17 #include <QtCore/QQueue>
18 #include <QtCore/QThread>
19 #include <QtCore/QMutex>
20 #include <QtCore/QWaitCondition>
21 #include <QtCore/QSet>
22 
23 #include <QtScript/QScriptEngine>
24 
25 #include "scriptExecutionControl.h"
26 
27 namespace trikScriptRunner {
28 
29 class ScriptEngineWorker;
30 class ScriptThread;
31 
34 class Threading : public QObject
35 {
36  Q_OBJECT
37 
38 public:
40  explicit Threading(ScriptEngineWorker *scriptWorker, ScriptExecutionControl &scriptControl);
41  ~Threading() override;
42 
44  void startMainThread(const QString &script);
45 
48  Q_INVOKABLE void startThread(const QScriptValue &threadId, const QScriptValue &function);
49 
51  Q_INVOKABLE void joinThread(const QString &threadId);
52 
55  Q_INVOKABLE void sendMessage(const QString &threadId, const QScriptValue &message);
56 
58  Q_INVOKABLE QScriptValue receiveMessage(bool waitForMessage = true);
59 
61  Q_INVOKABLE void killThread(const QString &threadId);
62 
65  void waitForAll();
66 
70  void waitForAllYielding();
71 
73  Q_INVOKABLE void reset();
74 
76  QString errorMessage() const;
77 
79  void threadFinished(const QString &id);
80 
82  bool inEventDrivenMode() const;
83 
84 signals:
86  void finished();
87 
90  void getVariables(const QString &propertyName);
91 
94  void variablesReady(const QJsonObject &data);
95 
96 private:
100  void startThread(const QString &threadId, QScriptEngine *engine, const QString &script);
101 
104  QScriptEngine *cloneEngine(QScriptEngine *engine);
105 
107  bool tryLockReset();
108 
109  QHash<QString, ScriptThread *> mThreads;
110  QSet<QString> mFinishedThreads;
111  QSet<QString> mPreventFromStart;
112  QMutex mThreadsMutex;
113  QString mErrorMessage;
114 
115  QHash<QString, QQueue<QScriptValue>> mMessageQueues;
116  QMutex mMessageMutex;
117  QHash<QString, QMutex *> mMessageQueueMutexes;
118  QHash<QString, QWaitCondition *> mMessageQueueConditions;
119 
120  bool mResetStarted = false;
121  QMutex mResetMutex;
122 
123  ScriptEngineWorker * const mScriptWorker; // Doesn't have ownership.
124  ScriptExecutionControl &mScriptControl;
125  QString mScript;
126 
127  QScriptEngine *mMainScriptEngine; // Doesn't have ownership.
128  const QString mMainThreadName = "main";
129 };
130 
131 }
Q_INVOKABLE void killThread(const QString &threadId)
Stops given thread.
Definition: threading.cpp:288
Q_INVOKABLE QScriptValue receiveMessage(bool waitForMessage=true)
Designed to be called from a thread receiving a message.
Definition: threading.cpp:249
~Threading() override
Definition: threading.cpp:38
QString errorMessage() const
The last error message.
Definition: threading.cpp:312
Q_INVOKABLE void joinThread(const QString &threadId)
Joins a thread with given threadId. Does nothing if there is no thread with such id.
Definition: threading.cpp:133
void variablesReady(const QJsonObject &data)
Emitted when variables values are ready.
Definition: trikCommunicator.h:23
Q_INVOKABLE void startThread(const QScriptValue &threadId, const QScriptValue &function)
Starts a thread with given threadId.
Definition: threading.cpp:57
Designed to support OS threads from a Qt Script.
Definition: threading.h:34
Q_INVOKABLE void sendMessage(const QString &threadId, const QScriptValue &message)
Sends message to a mailbox with given threadId, even if such thread does not exist.
Definition: threading.cpp:230
Q_INVOKABLE void reset()
Aborts evalutation of all threads, resets to initial state.
Definition: threading.cpp:166
bool inEventDrivenMode() const
Returns true if the script is being evaluated in event-driven mode.
Definition: threading.cpp:327
void waitForAll()
Wait until all threads finish execution.
Definition: threading.cpp:114
Threading(ScriptEngineWorker *scriptWorker, ScriptExecutionControl &scriptControl)
Constructs a Threading object with given script worker as a parent.
Definition: threading.cpp:29
Worker object to be run in a separate thread for Qt Script execution.
Definition: scriptEngineWorker.h:35
void waitForAllYielding()
Wait until all threads finish execution.
Definition: threading.cpp:126
void finished()
Signals that all threads have finished.
void threadFinished(const QString &id)
Designed to be called from a thread that&#39;s finished execution.
Definition: threading.cpp:205
Script execution controller, provides related functions to scripts.
Definition: scriptExecutionControl.h:24
void startMainThread(const QString &script)
Starts the main thread of a script.
Definition: threading.cpp:43
void getVariables(const QString &propertyName)
Emitted when there is a request for variables values.
script
Definition: system.py:83