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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "CobEngine.h"
#include "CobThread.h"
#include "CobFile.h"
CR_BIND(CCobEngine, )
CR_REG_METADATA(CCobEngine, (
CR_MEMBER(threadInstances),
CR_MEMBER(tickAddedThreads),
CR_MEMBER(runningThreadIDs),
CR_MEMBER(sleepingThreadIDs),
// always null/empty when saving
CR_IGNORED(waitingThreadIDs),
CR_IGNORED(curThread),
CR_MEMBER(currentTime),
CR_MEMBER(threadCounter)
))
CR_BIND(CCobEngine::SleepingThread, )
CR_REG_METADATA(CCobEngine::SleepingThread, (
CR_MEMBER(id),
CR_MEMBER(wt)
))
int CCobEngine::AddThread(CCobThread&& thread)
{
if (thread.GetID() == -1)
thread.SetID(GenThreadID());
CCobInstance* o = thread.cobInst;
CCobThread& t = threadInstances[thread.GetID()];
// move thread into registry, hand its ID to owner
t = std::move(thread);
o->AddThreadID(t.GetID());
return (t.GetID());
}
// a thread wants to continue running at a later time, and adds itself to the scheduler
void CCobEngine::ScheduleThread(const CCobThread* thread)
{
switch (thread->GetState()) {
case CCobThread::Run: {
waitingThreadIDs.push_back(thread->GetID());
} break;
case CCobThread::Sleep: {
sleepingThreadIDs.push(SleepingThread{thread->GetID(), thread->GetWakeTime()});
} break;
default: {
LOG_L(L_ERROR, "[COBEngine::%s] unknown state %d for thread %d", __func__, thread->GetState(), thread->GetID());
} break;
}
}
void CCobEngine::SanityCheckThreads(const CCobInstance* owner)
{
if (false) {
// no threads belonging to owner should be left
for (const auto& p: threadInstances) {
assert(p.second.cobInst != owner);
}
for (const CCobThread& t: tickAddedThreads) {
assert(t.cobInst != owner);
}
}
}
void CCobEngine::TickThread(CCobThread* thread)
{
// for error messages originating in CUnitScript
curThread = thread;
// NB: threadID is still in <runningThreadIDs> here, TickRunningThreads clears it
if (thread != nullptr && !thread->Tick())
RemoveThread(thread->GetID());
curThread = nullptr;
}
void CCobEngine::WakeSleepingThreads()
{
// check on the sleeping threads, remove any whose owner died
while (!sleepingThreadIDs.empty()) {
CCobThread* zzzThread = GetThread((sleepingThreadIDs.top()).id);
if (zzzThread == nullptr) {
sleepingThreadIDs.pop();
continue;
}
// not yet time to execute this thread or any subsequent sleepers
if (zzzThread->GetWakeTime() >= currentTime)
break;
// remove executing thread from the queue
sleepingThreadIDs.pop();
// wake up the thread and tick it (if not dead)
// this can quite possibly re-add the thread to <sleepingThreadIDs>
// again, but any thread is guaranteed to sleep for at least 1 tick
switch (zzzThread->GetState()) {
case CCobThread::Sleep: {
zzzThread->SetState(CCobThread::Run);
TickThread(zzzThread);
} break;
case CCobThread::Dead: {
RemoveThread(zzzThread->GetID());
} break;
default: {
LOG_L(L_ERROR, "[COBEngine::%s] unknown state %d for thread %d", __func__, zzzThread->GetState(), zzzThread->GetID());
} break;
}
}
}
void CCobEngine::Tick(int deltaTime)
{
currentTime += deltaTime;
TickRunningThreads();
AddQueuedThreads();
WakeSleepingThreads();
AddQueuedThreads();
}
void CCobEngine::ShowScriptError(const std::string& msg)
{
if (curThread != nullptr) {
curThread->ShowError(msg.c_str());
return;
}
LOG_L(L_ERROR, "[COBEngine::%s] \"%s\" outside script execution", __func__, msg.c_str());
}
|