added /tick command
This commit is contained in:
@@ -69,6 +69,9 @@ World::World(util::WorldType wt) {
|
||||
source_db_.init(state());
|
||||
std::string srcerrs = source_db_.rebuild();
|
||||
|
||||
// Clear the clock.
|
||||
clock_ = 0;
|
||||
|
||||
// There shouldn't be any lua errors in the sourceDB at this
|
||||
// point, since there's no lua code in the sourceDB.
|
||||
assert(srcerrs == "");
|
||||
@@ -402,6 +405,8 @@ void World::invoke(const Invocation &inv) {
|
||||
case Invocation::KIND_FLUSH_PRINTS:
|
||||
invoke_flush_prints(inv.actor(), inv.place(), inv.action(), inv.data());
|
||||
break;
|
||||
case Invocation::KIND_TICK:
|
||||
invoke_tick(inv.actor(), inv.place(), inv.action(), inv.data());
|
||||
default:
|
||||
// Do nothing. Standard behavior for any invalid command is to
|
||||
// simply do nothing at all. Perhaps eventually we may add a flag
|
||||
@@ -576,18 +581,25 @@ void World::invoke_plan(int64_t actor_id, int64_t place_id, const std::string &a
|
||||
// Push the thread's ID into the runnable thread queue,
|
||||
// then run the thread queue.
|
||||
thread_sched_.add(0, tid, place_id);
|
||||
run_scheduled_threads(0);
|
||||
run_scheduled_threads();
|
||||
assert(stack_is_clear());
|
||||
}
|
||||
|
||||
void World::run_scheduled_threads(int64_t clk) {
|
||||
void World::invoke_tick(int64_t actor_id, int64_t place_id, const std::string &action, const InvocationData &data) {
|
||||
if (util::world_type_authoritative(world_type_)) {
|
||||
clock_ += 1;
|
||||
run_scheduled_threads();
|
||||
}
|
||||
}
|
||||
|
||||
void World::run_scheduled_threads() {
|
||||
assert(stack_is_clear());
|
||||
lua_State *L = state();
|
||||
LuaVar tangibles, place, mt, threads, thinfo, actorid, isnew, useppool, thread;
|
||||
LuaStack LS(L, tangibles, place, mt, threads, thinfo, actorid, isnew, useppool, thread);
|
||||
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
||||
|
||||
while (thread_sched_.ready(clk)) {
|
||||
while (thread_sched_.ready(clock_)) {
|
||||
SchedEntry sched = thread_sched_.pop();
|
||||
LS.rawget(place, tangibles, sched.place_id());
|
||||
if (!LS.istable(place)) {
|
||||
@@ -632,7 +644,7 @@ void World::run_scheduled_threads(int64_t clk) {
|
||||
if (status == LUA_YIELD) {
|
||||
// If there's nothing on the stack, infer that tangible.nopredict yielded.
|
||||
if (lua_gettop(CO) == 0) {
|
||||
std::cerr << "Thread killed self using tangible.nopredict" << std::endl;
|
||||
// std::cerr << "Thread killed self using tangible.nopredict" << std::endl;
|
||||
LS.rawset(threads, sched.thread_id(), LuaNil);
|
||||
}
|
||||
// If there's a single number on the stack, infer that 'wait' yielded.
|
||||
@@ -641,9 +653,9 @@ void World::run_scheduled_threads(int64_t clk) {
|
||||
lua_settop(CO, 0);
|
||||
LS.rawset(thinfo, "isnew", false);
|
||||
LS.rawset(thinfo, "useppool", false);
|
||||
std::cerr << "Thread wait = " << delay << std::endl;
|
||||
// std::cerr << "Thread wait = " << delay << std::endl;
|
||||
thread_sched_.add(sched.clock() + int64_t(delay), sched.thread_id(), sched.place_id());
|
||||
std::cerr << "Added to schedule." << std::endl;
|
||||
// std::cerr << "Added to schedule." << std::endl;
|
||||
}
|
||||
// In any other case, generate an error and kill the coroutine.
|
||||
else {
|
||||
@@ -652,12 +664,12 @@ void World::run_scheduled_threads(int64_t clk) {
|
||||
}
|
||||
} else if (status == LUA_OK) {
|
||||
// Successfully ran to completion. Remove from thread table.
|
||||
std::cerr << "Thread ran to completion." << std::endl;
|
||||
// std::cerr << "Thread ran to completion." << std::endl;
|
||||
LS.rawset(threads, sched.thread_id(), LuaNil);
|
||||
} else {
|
||||
// Generated an error. Add a traceback, print, and kill the coroutine.
|
||||
traceback_coroutine(CO);
|
||||
std::cerr << lua_tostring(CO, -1);
|
||||
// std::cerr << lua_tostring(CO, -1);
|
||||
LS.rawset(threads, sched.thread_id(), LuaNil);
|
||||
}
|
||||
}
|
||||
@@ -733,6 +745,7 @@ void World::serialize(StreamBuffer *sb) {
|
||||
// int64_t wc0 = sb->total_writes();
|
||||
lua_snap_.serialize(sb);
|
||||
id_global_pool_.serialize(sb);
|
||||
sb->write_int64(clock_);
|
||||
thread_sched_.serialize(sb);
|
||||
sb->write_uint32(tangibles_.size());
|
||||
for (const auto &p : tangibles_) {
|
||||
@@ -749,6 +762,7 @@ void World::deserialize(StreamBuffer *sb) {
|
||||
redirects_.clear();
|
||||
lua_snap_.deserialize(sb);
|
||||
id_global_pool_.deserialize(sb);
|
||||
clock_ = sb->read_int64();
|
||||
thread_sched_.deserialize(sb);
|
||||
// Mark all tangibles for deletion by setting ID to zero.
|
||||
for (const auto &p : tangibles_) {
|
||||
|
||||
Reference in New Issue
Block a user