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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "CobEngine.h"
#include "CobThread.h"
#include "CobInstance.h"
#include "CobFile.h"
#include "System/FileSystem/FileHandler.h"
#include "System/TimeProfiler.h"
CCobEngine* cobEngine = nullptr;
CCobFileHandler* cobFileHandler = nullptr;
/******************************************************************************/
/******************************************************************************/
CR_BIND(CCobEngine, )
CR_REG_METADATA(CCobEngine, (
CR_MEMBER(currentTime),
CR_MEMBER(running),
CR_MEMBER(sleeping),
//always null/empty when saving
CR_IGNORED(wantToRun),
CR_IGNORED(curThread)
))
CCobEngine::CCobEngine()
: curThread(nullptr)
, currentTime(0)
{ }
CCobEngine::~CCobEngine()
{
//Should delete all things that the scheduler knows
do {
while (!running.empty()) {
CCobThread* tmp = running.back();
running.pop_back();
delete tmp;
}
while (!wantToRun.empty()) {
CCobThread* tmp = wantToRun.back();
wantToRun.pop_back();
delete tmp;
}
while (!sleeping.empty()) {
CCobThread* tmp = sleeping.top();
sleeping.pop();
delete tmp;
}
// callbacks may add new threads
} while (!running.empty() || !wantToRun.empty() || !sleeping.empty());
}
CCobFileHandler::~CCobFileHandler()
{
//Free all cobfiles
for (std::map<std::string, CCobFile *>::iterator i = cobFiles.begin(); i != cobFiles.end(); ++i) {
delete i->second;
}
}
//A thread wants to continue running at a later time, and adds itself to the scheduler
void CCobEngine::AddThread(CCobThread *thread)
{
switch (thread->state) {
case CCobThread::Run:
wantToRun.push_back(thread);
break;
case CCobThread::Sleep:
sleeping.push(thread);
break;
default:
LOG_L(L_ERROR, "thread added to scheduler with unknown state (%d)", thread->state);
break;
}
}
void CCobEngine::TickThread(CCobThread* thread)
{
curThread = thread; // for error messages originating in CUnitScript
if (!thread->Tick())
delete thread;
curThread = NULL;
}
void CCobEngine::Tick(int deltaTime)
{
SCOPED_TIMER("CobEngine::Tick");
currentTime += deltaTime;
// Advance all running threads
for (CCobThread* t: running) {
//LOG_L(L_DEBUG, "Now 1running %d: %s", currentTime, (*i)->GetName().c_str());
TickThread(t);
}
// A thread can never go from running->running, so clear the list
// note: if preemption was to be added, this would no longer hold
// however, ta scripts can not run preemptively anyway since there
// isn't any synchronization methods available
running.clear();
// The threads that just ran may have added new threads that should run next tick
std::swap(running, wantToRun);
//Check on the sleeping threads
if (!sleeping.empty()) {
CCobThread* cur = sleeping.top();
while ((cur != NULL) && (cur->GetWakeTime() < currentTime)) {
// Start with removing the executing thread from the queue
sleeping.pop();
//Run forward again. This can quite possibly readd the thread to the sleeping array again
//But it will not interfere since it is guaranteed to sleep > 0 ms
//LOG_L(L_DEBUG, "Now 2running %d: %s", currentTime, cur->GetName().c_str());
if (cur->state == CCobThread::Sleep) {
cur->state = CCobThread::Run;
TickThread(cur);
} else if (cur->state == CCobThread::Dead) {
delete cur;
} else {
LOG_L(L_ERROR, "Sleeping thread strange state %d", cur->state);
}
if (!sleeping.empty())
cur = sleeping.top();
else
cur = NULL;
}
}
}
void CCobEngine::ShowScriptError(const string& msg)
{
if (curThread)
curThread->ShowError(msg);
else
LOG_L(L_ERROR, "%s outside script execution", msg.c_str());
}
/******************************************************************************/
/******************************************************************************/
CCobFile* CCobFileHandler::GetCobFile(const string& name)
{
//Already known?
map<string, CCobFile *>::iterator i;
if ((i = cobFiles.find(name)) != cobFiles.end()) {
return i->second;
}
CFileHandler f(name);
if (!f.FileExists()) {
return NULL;
}
CCobFile *cf = new CCobFile(f, name);
cobFiles[name] = cf;
return cf;
}
CCobFile* CCobFileHandler::ReloadCobFile(const string& name)
{
map<string, CCobFile *>::iterator it = cobFiles.find(name);
if (it == cobFiles.end()) {
return GetCobFile(name);
}
delete it->second;
cobFiles.erase(it);
return GetCobFile(name);
}
const CCobFile* CCobFileHandler::GetScriptAddr(const string& name) const
{
map<string, CCobFile *>::const_iterator it = cobFiles.find(name);
if (it != cobFiles.end()) {
return it->second;
}
return NULL;
}
/******************************************************************************/
/******************************************************************************/
|