Redesigned ID allocator and tangible.actor
This commit is contained in:
@@ -61,24 +61,6 @@ void IdGlobalPool::salvage(int64_t batch) {
|
||||
salvaged_.push_back(batch);
|
||||
}
|
||||
|
||||
void IdGlobalPool::salvage_thread(lua_State *L) {
|
||||
salvage(lua_getnextid(L));
|
||||
lua_setnextid(L, 0);
|
||||
}
|
||||
|
||||
int64_t IdGlobalPool::alloc_id_for_thread(lua_State *L) {
|
||||
int64_t batch = lua_getnextid(L);
|
||||
if (batch != 0) {
|
||||
int64_t id = batch;
|
||||
batch += 1;
|
||||
if ((batch & 0xFF) == 0) batch = 0;
|
||||
lua_setnextid(L, batch);
|
||||
return id;
|
||||
} else {
|
||||
return get_one();
|
||||
}
|
||||
}
|
||||
|
||||
void IdGlobalPool::serialize(StreamBuffer *sb) const {
|
||||
sb->write_int64(next_batch_);
|
||||
sb->write_int64(next_id_);
|
||||
@@ -146,30 +128,22 @@ void IdPlayerPool::test_clear_ranges() {
|
||||
ranges_.clear();
|
||||
}
|
||||
|
||||
int64_t IdPlayerPool::get_batch() {
|
||||
while (int(ranges_.size()) < fifo_capacity_ + 1) {
|
||||
int64_t batch = global_->get_batch();
|
||||
if (batch == 0) break;
|
||||
ranges_.push_back(batch);
|
||||
}
|
||||
int64_t IdPlayerPool::get_one() {
|
||||
refill();
|
||||
if (ranges_.empty()) {
|
||||
return 0;
|
||||
return global_->get_one();
|
||||
} else {
|
||||
int64_t batch = ranges_.front();
|
||||
ranges_.pop_front();
|
||||
return batch;
|
||||
int64_t id = ranges_.front();
|
||||
if ((id & 0xFF) == 0xFF) {
|
||||
ranges_.pop_front();
|
||||
refill();
|
||||
} else {
|
||||
ranges_.front() = id + 1;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
void IdPlayerPool::salvage_thread(lua_State *L) {
|
||||
global_->salvage_thread(L);
|
||||
}
|
||||
|
||||
void IdPlayerPool::prepare_thread(lua_State *L) {
|
||||
global_->salvage_thread(L);
|
||||
lua_setnextid(L, get_batch());
|
||||
}
|
||||
|
||||
void IdPlayerPool::serialize(StreamBuffer *sb) const {
|
||||
sb->write_uint8(fifo_capacity_);
|
||||
sb->write_uint8(ranges_.size());
|
||||
@@ -324,25 +298,24 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(2));
|
||||
|
||||
// In the synchronous model, refill should do nothing.
|
||||
// The player pool should shunt through to the global.
|
||||
pp.test_clear_ranges();
|
||||
gp.init_synch();
|
||||
pp.set_fifo_capacity(3);
|
||||
pp.refill();
|
||||
LuaAssert(L, pp.size() == 0);
|
||||
LuaAssert(L, pp.get_batch() == 0);
|
||||
LuaAssert(L, pp.get_one() == 0x001E000000000000);
|
||||
LuaAssert(L, pp.size() == 0);
|
||||
LuaAssert(L, pp.get_batch() == 0);
|
||||
|
||||
// In the master model, with fifo disabled. Fifo should remain
|
||||
// empty, but batches should be returned.
|
||||
// empty, and the player pool should shunt through to the global.
|
||||
gp.init_master();
|
||||
pp.test_clear_ranges();
|
||||
pp.set_fifo_capacity(0);
|
||||
pp.refill();
|
||||
LuaAssert(L, pp.size() == 0);
|
||||
LuaAssert(L, pp.get_batch() == nthbatch(0));
|
||||
LuaAssert(L, pp.get_one() == 0x0010000000000000);
|
||||
LuaAssert(L, pp.size() == 0);
|
||||
LuaAssert(L, pp.get_batch() == nthbatch(1));
|
||||
|
||||
// Test refill from master (with enabled fifo).
|
||||
gp.init_master();
|
||||
@@ -351,44 +324,24 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
pp.refill();
|
||||
LuaAssert(L, ranges_equal(pp.ranges_, nthbatch(0), nthbatch(1), nthbatch(2)));
|
||||
|
||||
// Now test that get_batch keeps the pool filled from master.
|
||||
LuaAssert(L, pp.get_batch() == nthbatch(0));
|
||||
LuaAssert(L, ranges_equal(pp.ranges_, nthbatch(1), nthbatch(2), nthbatch(3)));
|
||||
// Now test that get_one() keeps the pool filled from master.
|
||||
for (int i = 0; i < 256; i++) {
|
||||
LuaAssert(L, pp.get_one() == nthbatch(0) + i);
|
||||
}
|
||||
for (int i = 0; i < 256; i++) {
|
||||
LuaAssert(L, pp.get_one() == nthbatch(1) + i);
|
||||
}
|
||||
LuaAssert(L, ranges_equal(pp.ranges_, nthbatch(2), nthbatch(3), nthbatch(4)));
|
||||
|
||||
// Test unqueueing the batches.
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(4));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(5));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(6));
|
||||
pp.set_fifo_capacity(0);
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(1));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(2));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(3));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(6));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(4));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(7));
|
||||
|
||||
// Try preparing a thread and salvaging a thread.
|
||||
gp.init_master();
|
||||
pp.test_clear_ranges();
|
||||
pp.set_fifo_capacity(3);
|
||||
lua_setnextid(L, 0);
|
||||
pp.prepare_thread(L);
|
||||
LuaAssert(L, lua_getnextid(L) == nthbatch(0));
|
||||
lua_setnextid(L, 0);
|
||||
pp.prepare_thread(L);
|
||||
LuaAssert(L, lua_getnextid(L) == nthbatch(1));
|
||||
|
||||
// Try salvaging the pool from the thread.
|
||||
pp.salvage_thread(L);
|
||||
LuaAssert(L, lua_getnextid(L) == 0);
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(1));
|
||||
|
||||
// Allocate IDs from inside a thread.
|
||||
gp.init_master();
|
||||
lua_setnextid(L, 0xFD);
|
||||
LuaAssert(L, gp.alloc_id_for_thread(L) == 0xFD);
|
||||
LuaAssert(L, gp.alloc_id_for_thread(L) == 0xFE);
|
||||
LuaAssert(L, gp.alloc_id_for_thread(L) == 0xFF);
|
||||
LuaAssert(L, gp.alloc_id_for_thread(L) == 0x0010000000000000);
|
||||
LuaAssert(L, lua_getnextid(L) == 0);
|
||||
|
||||
// Serialize and deserialize a global pool.
|
||||
gp.init_master();
|
||||
gpds.init_master();
|
||||
@@ -415,9 +368,7 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
ppds.deserialize(&sb);
|
||||
LuaAssert(L, ppds.get_fifo_capacity()==3);
|
||||
LuaAssert(L, ppds.size() == 3);
|
||||
LuaAssert(L, ppds.get_batch() == nthbatch(1));
|
||||
LuaAssert(L, ppds.get_batch() == nthbatch(2));
|
||||
LuaAssert(L, ppds.get_batch() == nthbatch(3));
|
||||
LuaAssert(L, ranges_equal(ppds.ranges_, nthbatch(1), nthbatch(2), nthbatch(3)));
|
||||
|
||||
// Difference transmit compare two empty pools.
|
||||
gp.init_master();
|
||||
|
||||
Reference in New Issue
Block a user