| 77 | } |
| 78 | |
| 79 | void ScriptableThread::run() { |
| 80 | try { |
| 81 | double updateMeasureWindow = m_parameters.getDouble("updateMeasureWindow",0.5); |
| 82 | TickRateApproacher tickApproacher(1.0f / m_timestep, updateMeasureWindow); |
| 83 | |
| 84 | while (!m_stop && !m_errorOccurred) { |
| 85 | LogMap::set(strf("lua_{}_update", m_name), strf("{:4.2f}Hz", tickApproacher.rate())); |
| 86 | |
| 87 | update(); |
| 88 | tickApproacher.setTargetTickRate(1.0f / m_timestep); |
| 89 | tickApproacher.tick(); |
| 90 | |
| 91 | double spareTime = tickApproacher.spareTime(); |
| 92 | |
| 93 | int64_t spareMilliseconds = floor(spareTime * 1000); |
| 94 | if (spareMilliseconds > 0) |
| 95 | Thread::sleepPrecise(spareMilliseconds); |
| 96 | } |
| 97 | } catch (std::exception const& e) { |
| 98 | Logger::error("ScriptableThread exception caught: {}", outputException(e, true)); |
| 99 | m_errorOccurred = true; |
| 100 | } |
| 101 | for (auto& p : m_scriptContexts) |
| 102 | p.second->uninit(); |
| 103 | } |
| 104 | |
| 105 | Maybe<Json> ScriptableThread::receiveMessage(String const& message, JsonArray const& args) { |
| 106 | Maybe<Json> result; |