Tangible serialization and design improvements

This commit is contained in:
2021-03-14 18:17:34 -04:00
parent 4426fa157a
commit 7c9fd1e46b
7 changed files with 108 additions and 44 deletions

View File

@@ -104,22 +104,34 @@ void IdGlobalPool::deserialize(StreamBuffer *sb) {
}
}
IdPlayerPool::IdPlayerPool(IdGlobalPool *gp) {
global_ = gp;
IdPlayerPool::IdPlayerPool(IdGlobalPool *g) {
global_ = g;
fifo_enabled_ = false;
}
IdPlayerPool::~IdPlayerPool() {
}
void IdPlayerPool::enable_fifo() {
fifo_enabled_ = true;
}
void IdPlayerPool::disable_fifo() {
unqueue();
fifo_enabled_ = false;
}
void IdPlayerPool::purge() {
ranges_.clear();
}
void IdPlayerPool::refill() {
while (int(ranges_.size()) < global_->queue_fill()) {
int64_t batch = global_->get_batch();
if (batch == 0) break;
ranges_.push_back(batch);
if (fifo_enabled_) {
while (int(ranges_.size()) < global_->queue_fill()) {
int64_t batch = global_->get_batch();
if (batch == 0) break;
ranges_.push_back(batch);
}
}
}
@@ -131,7 +143,8 @@ void IdPlayerPool::unqueue() {
}
int64_t IdPlayerPool::get_batch() {
while (int(ranges_.size()) < global_->queue_fill() + 1) {
int fill = fifo_enabled_ ? global_->queue_fill() : 0;
while (int(ranges_.size()) < fill + 1) {
int64_t batch = global_->get_batch();
if (batch == 0) break;
ranges_.push_back(batch);
@@ -155,6 +168,7 @@ void IdPlayerPool::prepare_thread(lua_State *L) {
}
void IdPlayerPool::serialize(StreamBuffer *sb) {
sb->write_bool(fifo_enabled_);
sb->write_size(ranges_.size());
for (int64_t batch : ranges_) {
sb->write_int64(batch);
@@ -162,6 +176,7 @@ void IdPlayerPool::serialize(StreamBuffer *sb) {
}
void IdPlayerPool::deserialize(StreamBuffer *sb) {
fifo_enabled_ = sb->read_bool();
size_t ranges_size = sb->read_size();
ranges_.resize(ranges_size);
for (int i=0; i < int(ranges_size); i++) {
@@ -230,6 +245,7 @@ LuaDefine(unittests_idalloc, "c") {
// In the synchronous model, refill should do nothing.
pp.purge();
pp.enable_fifo();
gp.init_synch(3);
pp.refill();
LuaAssert(L, pp.size() == 0);
@@ -237,8 +253,20 @@ LuaDefine(unittests_idalloc, "c") {
LuaAssert(L, pp.size() == 0);
LuaAssert(L, pp.get_batch() == 0);
// Test refill from master.
// In the master model, with fifo disabled. Fifo should remain
// empty, but batches should be returned.
pp.purge();
pp.disable_fifo();
gp.init_master(3);
pp.refill();
LuaAssert(L, pp.size() == 0);
LuaAssert(L, pp.get_batch() == nthbatch(0));
LuaAssert(L, pp.size() == 0);
LuaAssert(L, pp.get_batch() == nthbatch(1));
// Test refill from master (with enabled fifo).
pp.purge();
pp.enable_fifo();
gp.init_master(3);
pp.refill();
LuaAssert(L, ranges_equal(pp.ranges_, nthbatch(0), nthbatch(1), nthbatch(2)));
@@ -258,6 +286,7 @@ LuaDefine(unittests_idalloc, "c") {
// Try preparing a thread and salvaging a thread.
pp.purge();
pp.enable_fifo();
gp.init_master(3);
lua_setnextid(L, 0);
pp.prepare_thread(L);
@@ -300,11 +329,14 @@ LuaDefine(unittests_idalloc, "c") {
gpds.init_synch(5);
LuaAssert(L, gp.get_batch() == nthbatch(0));
pp.purge();
pp.enable_fifo();
pp.refill();
LuaAssert(L, pp.fifo_enabled());
LuaAssert(L, pp.size() == 3);
ppds.purge();
pp.serialize(&sb);
ppds.deserialize(&sb);
LuaAssert(L, ppds.fifo_enabled());
LuaAssert(L, ppds.size() == 3);
LuaAssert(L, ppds.get_batch() == nthbatch(1));
LuaAssert(L, ppds.get_batch() == nthbatch(2));