blob: 65f007eaf23c3e2b27a11c6b16d753bcce58b3ff (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#pragma once
#include "StarThread.hpp"
#include "StarLuaRoot.hpp"
#include "StarLuaComponents.hpp"
#include "StarRpcThreadPromise.hpp"
namespace Star {
STAR_CLASS(ScriptableThread);
// Runs a Lua in a separate thread and guards exceptions that occur in
// it. All methods are designed to not throw exceptions, but will instead log
// the error and trigger the ScriptableThread error state.
class ScriptableThread : public Thread {
public:
struct Message {
String message;
JsonArray args;
RpcThreadPromiseKeeper<Json> promise;
};
typedef LuaMessageHandlingComponent<LuaUpdatableComponent<LuaBaseComponent>> ScriptComponent;
typedef shared_ptr<ScriptComponent> ScriptComponentPtr;
ScriptableThread(Json parameters);
~ScriptableThread();
void start();
// Signals the ScriptableThread to stop and then joins it
void stop();
void setPause(bool pause);
// An exception occurred and the
// ScriptableThread has stopped running.
bool errorOccurred();
bool shouldExpire();
//
void passMessage(Message&& message);
protected:
virtual void run();
private:
void update();
Maybe<Json> receiveMessage(String const& message, JsonArray const& args);
mutable RecursiveMutex m_mutex;
LuaRootPtr m_luaRoot;
StringMap<ScriptComponentPtr> m_scriptContexts;
Json m_parameters;
String m_name;
float m_timestep;
mutable RecursiveMutex m_messageMutex;
List<Message> m_messages;
atomic<bool> m_stop;
atomic<bool> m_pause;
mutable atomic<bool> m_errorOccurred;
mutable atomic<bool> m_shouldExpire;
LuaCallbacks makeThreadCallbacks();
Json configValue(String const& name, Json def) const;
};
}
|