From 89598b602f57f17aabb8d9dbd2d87eb5176160f6 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 13 May 2024 16:26:02 +0200 Subject: Progressively applying the coding rules --- src/keeper.cpp | 328 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 164 insertions(+), 164 deletions(-) (limited to 'src/keeper.cpp') diff --git a/src/keeper.cpp b/src/keeper.cpp index 7367d0c..39d2e85 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -76,15 +76,15 @@ static constexpr int kContentsTableIndex{ 1 }; // replaces the fifo ud by its uservalue on the stack [[nodiscard]] static keeper_fifo* prepare_fifo_access(lua_State* L_, int idx_) { - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, idx_) }; - if (fifo != nullptr) { + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, idx_) }; + if (_fifo != nullptr) { idx_ = lua_absindex(L_, idx_); STACK_GROW(L_, 1); // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around lua_getiuservalue(L_, idx_, kContentsTableIndex); lua_replace(L_, idx_); } - return fifo; + return _fifo; } // ################################################################################################# @@ -95,12 +95,12 @@ static constexpr int kContentsTableIndex{ 1 }; { STACK_GROW(L_, 2); STACK_CHECK_START_REL(L_, 0); - keeper_fifo* const fifo{ new (L_) keeper_fifo{} }; + keeper_fifo* const _fifo{ new (L_) keeper_fifo{} }; STACK_CHECK(L_, 1); lua_newtable(L_); lua_setiuservalue(L_, -2, kContentsTableIndex); STACK_CHECK(L_, 1); - return fifo; + return _fifo; } // ################################################################################################# @@ -109,12 +109,12 @@ static constexpr int kContentsTableIndex{ 1 }; // out: nothing, removes all pushed values from the stack static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) { - int const idx{ lua_gettop(L_) - count_ }; - int const start{ fifo_->first + fifo_->count - 1 }; + int const _idx{ lua_gettop(L_) - count_ }; + int const _start{ fifo_->first + fifo_->count - 1 }; // pop all additional arguments, storing them in the fifo - for (int i = count_; i >= 1; --i) { + for (int _i = count_; _i >= 1; --_i) { // store in the fifo the value at the top of the stack at the specified index, popping it from the stack - lua_rawseti(L_, idx, start + i); + lua_rawseti(L_, _idx, _start + _i); } fifo_->count += count_; } @@ -129,8 +129,8 @@ static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) { STACK_GROW(L_, count_); - for (int i = 0; i < count_; ++i) { - lua_rawgeti(L_, 1, (fifo_->first + i)); + for (int _i = 0; _i < count_; ++_i) { + lua_rawgeti(L_, 1, (fifo_->first + _i)); } } @@ -141,32 +141,32 @@ static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) static void fifo_pop(lua_State* L_, keeper_fifo* fifo_, int count_) { LUA_ASSERT(L_, lua_istable(L_, -1)); - int const fifo_idx{ lua_gettop(L_) }; // L_: ... fifotbl + int const _fifo_idx{ lua_gettop(L_) }; // L_: ... fifotbl // each iteration pushes a value on the stack! STACK_GROW(L_, count_ + 2); // skip first item, we will push it last for (int i = 1; i < count_; ++i) { int const at{ fifo_->first + i }; // push item on the stack - lua_rawgeti(L_, fifo_idx, at); // L_: ... fifotbl val + lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl val // remove item from the fifo lua_pushnil(L_); // L_: ... fifotbl val nil - lua_rawseti(L_, fifo_idx, at); // L_: ... fifotbl val + lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl val } // now process first item { int const at{ fifo_->first }; - lua_rawgeti(L_, fifo_idx, at); // L_: ... fifotbl vals val + lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl vals val lua_pushnil(L_); // L_: ... fifotbl vals val nil - lua_rawseti(L_, fifo_idx, at); // L_: ... fifotbl vals val - lua_replace(L_, fifo_idx); // L_: ... vals + lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl vals val + lua_replace(L_, _fifo_idx); // L_: ... vals } // avoid ever-growing indexes by resetting each time we detect the fifo is empty { - int const new_count{ fifo_->count - count_ }; - fifo_->first = (new_count == 0) ? 1 : (fifo_->first + count_); - fifo_->count = new_count; + int const _new_count{ fifo_->count - count_ }; + fifo_->first = (_new_count == 0) ? 1 : (fifo_->first + count_); + fifo_->count = _new_count; } } @@ -202,34 +202,34 @@ static void push_table(lua_State* L_, int idx_) // only used by linda:dump() and linda:__towatch() for debugging purposes int keeper_push_linda_storage(Linda& linda_, DestState L_) { - Keeper* const K{ linda_.whichKeeper() }; - SourceState const KL{ K ? K->L : nullptr }; - if (KL == nullptr) + Keeper* const _K{ linda_.whichKeeper() }; + SourceState const _KL{ _K ? _K->L : nullptr }; + if (_KL == nullptr) return 0; - STACK_GROW(KL, 4); - STACK_CHECK_START_REL(KL, 0); - kFifosRegKey.pushValue(KL); // KL: fifos L_: - lua_pushlightuserdata(KL, &linda_); // KL: fifos ud L_: - lua_rawget(KL, -2); // KL: fifos storage L_: - lua_remove(KL, -2); // KL: storage L_: - if (!lua_istable(KL, -1)) { - lua_pop(KL, 1); // KL: L_: - STACK_CHECK(KL, 0); + STACK_GROW(_KL, 4); + STACK_CHECK_START_REL(_KL, 0); + kFifosRegKey.pushValue(_KL); // KL: fifos L_: + lua_pushlightuserdata(_KL, &linda_); // KL: fifos ud L_: + lua_rawget(_KL, -2); // KL: fifos storage L_: + lua_remove(_KL, -2); // KL: storage L_: + if (!lua_istable(_KL, -1)) { + lua_pop(_KL, 1); // KL: L_: + STACK_CHECK(_KL, 0); return 0; } // move data from keeper to destination state STACK_GROW(L_, 5); STACK_CHECK_START_REL(L_, 0); lua_newtable(L_); // KL: storage L_: out - InterCopyContext c{ linda_.U, L_, KL, {}, {}, {}, LookupMode::FromKeeper, {} }; - lua_pushnil(KL); // KL: storage nil L_: out - while (lua_next(KL, -2)) { // KL: storage key fifo L_: out - keeper_fifo* fifo = prepare_fifo_access(KL, -1); // KL: storage key fifotbl L_: out - lua_pushvalue(KL, -2); // KL: storage key fifotbl key L_: out - std::ignore = c.inter_move(1); // KL: storage key fifotbl L_: out key + InterCopyContext _c{ linda_.U, L_, _KL, {}, {}, {}, LookupMode::FromKeeper, {} }; + lua_pushnil(_KL); // KL: storage nil L_: out + while (lua_next(_KL, -2)) { // KL: storage key fifo L_: out + keeper_fifo* fifo = prepare_fifo_access(_KL, -1); // KL: storage key fifotbl L_: out + lua_pushvalue(_KL, -2); // KL: storage key fifotbl key L_: out + std::ignore = _c.inter_move(1); // KL: storage key fifotbl L_: out key STACK_CHECK(L_, 2); lua_newtable(L_); // KL: storage key L_: out key keyout - std::ignore = c.inter_move(1); // KL: storage key L_: out key keyout fifotbl + std::ignore = _c.inter_move(1); // KL: storage key L_: out key keyout fifotbl lua_pushinteger(L_, fifo->first); // KL: storage key L_: out key keyout fifotbl first STACK_CHECK(L_, 5); lua_setfield(L_, -3, "first"); // KL: storage key L_: out key keyout fifotbl @@ -244,8 +244,8 @@ int keeper_push_linda_storage(Linda& linda_, DestState L_) STACK_CHECK(L_, 1); } // KL_: storage L_: out STACK_CHECK(L_, 1); - lua_pop(KL, 1); // KL: L_: out - STACK_CHECK(KL, 0); + lua_pop(_KL, 1); // KL: L_: out + STACK_CHECK(_KL, 0); return 1; } @@ -271,7 +271,7 @@ int keepercall_clear(lua_State* L_) // out: true|false int keepercall_send(lua_State* L_) { - int const n{ lua_gettop(L_) - 2 }; + int const _n{ lua_gettop(L_) - 2 }; push_table(L_, 1); // L_: ud key ... fifos // get the fifo associated to this key in this linda, create it if it doesn't exist lua_pushvalue(L_, 2); // L_: ud key ... fifos key @@ -284,14 +284,14 @@ int keepercall_send(lua_State* L_) lua_rawset(L_, -4); // L_: ud key ... fifos fifo } lua_remove(L_, -2); // L_: ud key ... fifo - keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; - if (fifo->limit >= 0 && fifo->count + n > fifo->limit) { + keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; + if (_fifo->limit >= 0 && _fifo->count + _n > _fifo->limit) { lua_settop(L_, 0); // L_: lua_pushboolean(L_, 0); // L_:false } else { - fifo = prepare_fifo_access(L_, -1); // L_: ud fifotbl + _fifo = prepare_fifo_access(L_, -1); // L_: ud fifotbl lua_replace(L_, 2); // L_: ud fifotbl ... - fifo_push(L_, fifo, n); // L_: ud fifotbl + fifo_push(L_, _fifo, _n); // L_: ud fifotbl lua_settop(L_, 0); // L_: lua_pushboolean(L_, 1); // L_: true } @@ -304,19 +304,19 @@ int keepercall_send(lua_State* L_) // out: (key, val) or nothing int keepercall_receive(lua_State* L_) { - int const top{ lua_gettop(L_) }; + int const _top{ lua_gettop(L_) }; push_table(L_, 1); // L_: ud keys fifos lua_replace(L_, 1); // L_: fifos keys - for (int i = 2; i <= top; ++i) { - lua_pushvalue(L_, i); // L_: fifos keys key[i] + for (int _i = 2; _i <= _top; ++_i) { + lua_pushvalue(L_, _i); // L_: fifos keys key[i] lua_rawget(L_, 1); // L_: fifos keys fifo - keeper_fifo* const fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos keys fifotbl - if (fifo != nullptr && fifo->count > 0) { - fifo_pop(L_, fifo, 1); // L_: fifos keys val + keeper_fifo* const _fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos keys fifotbl + if (_fifo != nullptr && _fifo->count > 0) { + fifo_pop(L_, _fifo, 1); // L_: fifos keys val if (!lua_isnil(L_, -1)) { lua_replace(L_, 1); // L_: val keys - lua_settop(L_, i); // L_: val keys key[i] - if (i != 2) { + lua_settop(L_, _i); // L_: val keys key[i] + if (_i != 2) { lua_replace(L_, 2); // L_: val key keys lua_settop(L_, 2); // L_: val key } @@ -324,7 +324,7 @@ int keepercall_receive(lua_State* L_) return 2; } } - lua_settop(L_, top); // L_: data keys + lua_settop(L_, _top); // L_: data keys } // nothing to receive return 0; @@ -335,9 +335,9 @@ int keepercall_receive(lua_State* L_) // in: linda_ud key mincount [maxcount] int keepercall_receive_batched(lua_State* L_) { - int const min_count{ static_cast(lua_tointeger(L_, 3)) }; - if (min_count > 0) { - int const max_count{ static_cast(luaL_optinteger(L_, 4, min_count)) }; + int const _min_count{ static_cast(lua_tointeger(L_, 3)) }; + if (_min_count > 0) { + int const _max_count{ static_cast(luaL_optinteger(L_, 4, _min_count)) }; lua_settop(L_, 2); // L_: ud key lua_insert(L_, 1); // L_: key ud push_table(L_, 2); // L_: key ud fifos @@ -345,9 +345,9 @@ int keepercall_receive_batched(lua_State* L_) lua_pushvalue(L_, 1); // L_: key fifos key lua_rawget(L_, 2); // L_: key fifos fifo lua_remove(L_, 2); // L_: key fifo - keeper_fifo* const fifo{ prepare_fifo_access(L_, 2) }; // L_: key fifotbl - if (fifo != nullptr && fifo->count >= min_count) { - fifo_pop(L_, fifo, std::min(max_count, fifo->count)); // L_: key ... + keeper_fifo* const _fifo{ prepare_fifo_access(L_, 2) }; // L_: key fifotbl + if (_fifo != nullptr && _fifo->count >= _min_count) { + fifo_pop(L_, _fifo, std::min(_max_count, _fifo->count)); // L_: key ... } else { lua_settop(L_, 0); // L_: } @@ -363,16 +363,16 @@ int keepercall_receive_batched(lua_State* L_) // out: true or nil int keepercall_limit(lua_State* L_) { - int const limit{ static_cast(lua_tointeger(L_, 3)) }; + int const _limit{ static_cast(lua_tointeger(L_, 3)) }; push_table(L_, 1); // L_: ud key n fifos lua_replace(L_, 1); // L_: fifos key n lua_pop(L_, 1); // L_: fifos key lua_pushvalue(L_, -1); // L_: fifos key key lua_rawget(L_, -3); // L_: fifos key fifo|nil - keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; - if (fifo == nullptr) { // L_: fifos key nil + keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; + if (_fifo == nullptr) { // L_: fifos key nil lua_pop(L_, 1); // L_: fifos key - fifo = fifo_new(KeeperState{ L_ }); // L_: fifos key fifo + _fifo = fifo_new(KeeperState{ L_ }); // L_: fifos key fifo lua_rawset(L_, -3); // L_: fifos } // remove any clutter on the stack @@ -380,13 +380,13 @@ int keepercall_limit(lua_State* L_) // return true if we decide that blocked threads waiting to write on that key should be awakened // this is the case if we detect the key was full but it is no longer the case if ( - ((fifo->limit >= 0) && (fifo->count >= fifo->limit)) // the key was full if limited and count exceeded the previous limit - && ((limit < 0) || (fifo->count < limit)) // the key is not full if unlimited or count is lower than the new limit + ((_fifo->limit >= 0) && (_fifo->count >= _fifo->limit)) // the key was full if limited and count exceeded the previous limit + && ((_limit < 0) || (_fifo->count < _limit)) // the key is not full if unlimited or count is lower than the new limit ) { lua_pushboolean(L_, 1); // L_: true } // set the new limit - fifo->limit = limit; + _fifo->limit = _limit; // return 0 or 1 value return lua_gettop(L_); } @@ -397,7 +397,7 @@ int keepercall_limit(lua_State* L_) // out: true if the linda was full but it's no longer the case, else nothing int keepercall_set(lua_State* L_) { - bool should_wake_writers{ false }; + bool _should_wake_writers{ false }; STACK_GROW(L_, 6); // retrieve fifos associated with the linda @@ -409,28 +409,28 @@ int keepercall_set(lua_State* L_) lua_pushvalue(L_, -1); // L_: fifos key key lua_rawget(L_, 1); // L_: fifos key fifo|nil // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; - if (fifo != nullptr) { // might be nullptr if we set a nonexistent key to nil // L_: fifos key fifo - if (fifo->limit < 0) { // fifo limit value is the default (unlimited): we can totally remove it + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; + if (_fifo != nullptr) { // might be nullptr if we set a nonexistent key to nil // L_: fifos key fifo + if (_fifo->limit < 0) { // fifo limit value is the default (unlimited): we can totally remove it lua_pop(L_, 1); // L_: fifos key lua_pushnil(L_); // L_: fifos key nil lua_rawset(L_, -3); // L_: fifos } else { // we create room if the fifo was full but it is no longer the case - should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit); + _should_wake_writers = (_fifo->limit > 0) && (_fifo->count >= _fifo->limit); lua_remove(L_, -2); // L_: fifos fifo lua_newtable(L_); // L_: fifos fifo {} lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos fifo - fifo->first = 1; - fifo->count = 0; + _fifo->first = 1; + _fifo->count = 0; } } } else { // set/replace contents stored at the specified key? - int const count{ lua_gettop(L_) - 2 }; // number of items we want to store + int const _count{ lua_gettop(L_) - 2 }; // number of items we want to store lua_pushvalue(L_, 2); // L_: fifos key [val [, ...]] key lua_rawget(L_, 1); // L_: fifos key [val [, ...]] fifo|nil - keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; - if (fifo == nullptr) { // can be nullptr if we store a value at a new key // fifos key [val [, ...]] nil + keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; + if (_fifo == nullptr) { // can be nullptr if we store a value at a new key // fifos key [val [, ...]] nil // no need to wake writers in that case, because a writer can't wait on an inexistent key lua_pop(L_, 1); // L_: fifos key [val [, ...]] std::ignore = fifo_new(KeeperState{ L_ }); // L_: fifos key [val [, ...]] fifo @@ -440,19 +440,19 @@ int keepercall_set(lua_State* L_) } else { // L_: fifos key [val [, ...]] fifo // the fifo exists, we just want to update its contents // we create room if the fifo was full but it is no longer the case - should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit) && (count < fifo->limit); + _should_wake_writers = (_fifo->limit > 0) && (_fifo->count >= _fifo->limit) && (_count < _fifo->limit); // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! lua_newtable(L_); // L_: fifos key [val [, ...]] fifo {} lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos key [val [, ...]] fifo - fifo->first = 1; - fifo->count = 0; + _fifo->first = 1; + _fifo->count = 0; } - fifo = prepare_fifo_access(L_, -1); // L_: fifos key [val [, ...]] fifotbl + _fifo = prepare_fifo_access(L_, -1); // L_: fifos key [val [, ...]] fifotbl // move the fifo below the values we want to store lua_insert(L_, 3); // L_: fifos key fifotbl [val [, ...]] - fifo_push(L_, fifo, count); // L_: fifos key fifotbl + fifo_push(L_, _fifo, _count); // L_: fifos key fifotbl } - return should_wake_writers ? (lua_pushboolean(L_, 1), 1) : 0; + return _should_wake_writers ? (lua_pushboolean(L_, 1), 1) : 0; } // ################################################################################################# @@ -461,21 +461,21 @@ int keepercall_set(lua_State* L_) // out: at most values int keepercall_get(lua_State* L_) { - int count{ 1 }; + int _count{ 1 }; if (lua_gettop(L_) == 3) { // L_: ud key count - count = static_cast(lua_tointeger(L_, 3)); + _count = static_cast(lua_tointeger(L_, 3)); lua_pop(L_, 1); // L_: ud key } push_table(L_, 1); // L_: ud key fifos lua_replace(L_, 1); // L_: fifos key lua_rawget(L_, 1); // L_: fifos fifo - keeper_fifo* const fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos fifotbl - if (fifo != nullptr && fifo->count > 0) { + keeper_fifo* const _fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos fifotbl + if (_fifo != nullptr && _fifo->count > 0) { lua_remove(L_, 1); // L_: fifotbl - count = std::min(count, fifo->count); + _count = std::min(_count, _fifo->count); // read value off the fifo - fifo_peek(L_, fifo, count); // L_: fifotbl ... - return count; + fifo_peek(L_, _fifo, _count); // L_: fifotbl ... + return _count; } // no fifo was ever registered for this key, or it is empty return 0; @@ -494,10 +494,10 @@ int keepercall_count(lua_State* L_) lua_replace(L_, 1); // L_: out fifos lua_pushnil(L_); // L_: out fifos nil while (lua_next(L_, 2)) { // L_: out fifos key fifo - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; lua_pop(L_, 1); // L_: out fifos key lua_pushvalue(L_, -1); // L_: out fifos key key - lua_pushinteger(L_, fifo->count); // L_: out fifos key key count + lua_pushinteger(L_, _fifo->count); // L_: out fifos key key count lua_rawset(L_, -5); // L_: out fifos key } lua_pop(L_, 1); // L_: out @@ -510,8 +510,8 @@ int keepercall_count(lua_State* L_) if (lua_isnil(L_, -1)) { // L_: the key is unknown // L_: fifos nil lua_remove(L_, -2); // L_: nil } else { // the key is known // L_: fifos fifo - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; - lua_pushinteger(L_, fifo->count); // L_: fifos fifo count + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; + lua_pushinteger(L_, _fifo->count); // L_: fifos fifo count lua_replace(L_, -3); // L_: count fifo lua_pop(L_, 1); // L_: count } @@ -526,10 +526,10 @@ int keepercall_count(lua_State* L_) while (lua_gettop(L_) > 2) { lua_pushvalue(L_, -1); // L_: out fifos keys... key lua_rawget(L_, 2); // L_: out fifos keys... fifo|nil - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; lua_pop(L_, 1); // L_: out fifos keys... - if (fifo != nullptr) { // L_: the key is known - lua_pushinteger(L_, fifo->count); // L_: out fifos keys... count + if (_fifo != nullptr) { // L_: the key is known + lua_pushinteger(L_, _fifo->count); // L_: out fifos keys... count lua_rawset(L_, 1); // L_: out fifos keys... } else { // the key is unknown lua_pop(L_, 1); // L_: out fifos keys... @@ -557,27 +557,27 @@ int keepercall_count(lua_State* L_) void close_keepers(Universe* U_) { if (U_->keepers != nullptr) { - int nbKeepers{ U_->keepers->nb_keepers }; + int _nbKeepers{ U_->keepers->nb_keepers }; // NOTE: imagine some keeper state N+1 currently holds a linda that uses another keeper N, and a _gc that will make use of it // when keeper N+1 is closed, object is GCed, linda operation is called, which attempts to acquire keeper N, whose Lua state no longer exists // in that case, the linda operation should do nothing. which means that these operations must check for keeper acquisition success // which is early-outed with a U->keepers->nbKeepers null-check U_->keepers->nb_keepers = 0; - for (int i = 0; i < nbKeepers; ++i) { - lua_State* const K{ U_->keepers->keeper_array[i].L }; - U_->keepers->keeper_array[i].L = KeeperState{ nullptr }; + for (int _i = 0; _i < _nbKeepers; ++_i) { + lua_State* const K{ U_->keepers->keeper_array[_i].L }; + U_->keepers->keeper_array[_i].L = KeeperState{ nullptr }; if (K != nullptr) { lua_close(K); } else { // detected partial init: destroy only the mutexes that got initialized properly - nbKeepers = i; + _nbKeepers = _i; } } - for (int i = 0; i < nbKeepers; ++i) { - U_->keepers->keeper_array[i].~Keeper(); + for (int _i = 0; _i < _nbKeepers; ++_i) { + U_->keepers->keeper_array[_i].~Keeper(); } // free the keeper bookkeeping structure - U_->internalAllocator.free(U_->keepers, sizeof(Keepers) + (nbKeepers - 1) * sizeof(Keeper)); + U_->internalAllocator.free(U_->keepers, sizeof(Keepers) + (_nbKeepers - 1) * sizeof(Keeper)); U_->keepers = nullptr; } } @@ -600,10 +600,10 @@ void init_keepers(Universe* U_, lua_State* L_) LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); STACK_CHECK_START_REL(L_, 0); // L_: settings lua_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers - int const nb_keepers{ static_cast(lua_tointeger(L_, -1)) }; + int const _nb_keepers{ static_cast(lua_tointeger(L_, -1)) }; lua_pop(L_, 1); // L_: settings - if (nb_keepers < 1) { - raise_luaL_error(L_, "Bad number of keepers (%d)", nb_keepers); + if (_nb_keepers < 1) { + raise_luaL_error(L_, "Bad number of keepers (%d)", _nb_keepers); } STACK_CHECK(L_, 0); @@ -614,51 +614,51 @@ void init_keepers(Universe* U_, lua_State* L_) // Keepers contains an array of 1 Keeper, adjust for the actual number of keeper states { - size_t const bytes = sizeof(Keepers) + (nb_keepers - 1) * sizeof(Keeper); + size_t const bytes = sizeof(Keepers) + (_nb_keepers - 1) * sizeof(Keeper); U_->keepers = static_cast(U_->internalAllocator.alloc(bytes)); if (U_->keepers == nullptr) { raise_luaL_error(L_, "init_keepers() failed while creating keeper array; out of memory"); } U_->keepers->Keepers::Keepers(); U_->keepers->gc_threshold = keepers_gc_threshold; - U_->keepers->nb_keepers = nb_keepers; + U_->keepers->nb_keepers = _nb_keepers; - for (int i = 0; i < nb_keepers; ++i) { - U_->keepers->keeper_array[i].Keeper::Keeper(); + for (int _i = 0; _i < _nb_keepers; ++_i) { + U_->keepers->keeper_array[_i].Keeper::Keeper(); } } - for (int i = 0; i < nb_keepers; ++i) { + for (int _i = 0; _i < _nb_keepers; ++_i) { // note that we will leak K if we raise an error later - KeeperState const K{ create_state(U_, L_) }; // L_: settings K: - if (K == nullptr) { + KeeperState const _K{ create_state(U_, L_) }; // L_: settings K: + if (_K == nullptr) { raise_luaL_error(L_, "init_keepers() failed while creating keeper states; out of memory"); } - U_->keepers->keeper_array[i].L = K; + U_->keepers->keeper_array[_i].L = _K; if (U_->keepers->gc_threshold >= 0) { - lua_gc(K, LUA_GCSTOP, 0); + lua_gc(_K, LUA_GCSTOP, 0); } - STACK_CHECK_START_ABS(K, 0); + STACK_CHECK_START_ABS(_K, 0); // copy the universe pointer in the keeper itself - universe_store(K, U_); - STACK_CHECK(K, 0); + universe_store(_K, U_); + STACK_CHECK(_K, 0); // make sure 'package' is initialized in keeper states, so that we have require() // this because this is needed when transferring deep userdata object - luaL_requiref(K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package - lua_pop(K, 1); // L_: settings K: - STACK_CHECK(K, 0); - serialize_require(DEBUGSPEW_PARAM_COMMA(U_) K); - STACK_CHECK(K, 0); + luaL_requiref(_K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package + lua_pop(_K, 1); // L_: settings K: + STACK_CHECK(_K, 0); + serialize_require(DEBUGSPEW_PARAM_COMMA(U_) _K); + STACK_CHECK(_K, 0); // copy package.path and package.cpath from the source state if (luaG_getmodule(L_, LUA_LOADLIBNAME) != LuaType::NIL) { // L_: settings package K: // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately - InterCopyContext c{ U_, DestState{ K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} }; - if (c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K: + InterCopyContext _c{ U_, DestState{ _K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} }; + if (_c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K: // if something went wrong, the error message is at the top of the stack lua_remove(L_, -2); // L_: settings error_msg raise_lua_error(L_); @@ -666,19 +666,19 @@ void init_keepers(Universe* U_, lua_State* L_) } lua_pop(L_, 1); // L_: settings K: STACK_CHECK(L_, 0); - STACK_CHECK(K, 0); + STACK_CHECK(_K, 0); // attempt to call on_state_create(), if we have one and it is a C function // (only support a C function because we can't transfer executable Lua code in keepers) // will raise an error in L_ in case of problem - callOnStateCreate(U_, K, L_, LookupMode::ToKeeper); + callOnStateCreate(U_, _K, L_, LookupMode::ToKeeper); // to see VM name in Decoda debugger - lua_pushfstring(K, "Keeper #%d", i + 1); // L_: settings K: "Keeper #n" - lua_setglobal(K, "decoda_name"); // L_: settings K: + lua_pushfstring(_K, "Keeper #%d", _i + 1); // L_: settings K: "Keeper #n" + lua_setglobal(_K, "decoda_name"); // L_: settings K: // create the fifos table in the keeper state - kFifosRegKey.setValue(K, [](lua_State* L_) { lua_newtable(L_); }); // L_: settings K: - STACK_CHECK(K, 0); + kFifosRegKey.setValue(_K, [](lua_State* L_) { lua_newtable(L_); }); // L_: settings K: + STACK_CHECK(_K, 0); } STACK_CHECK(L_, 0); } @@ -687,12 +687,12 @@ void init_keepers(Universe* U_, lua_State* L_) Keeper* Linda::acquireKeeper() const { - int const nbKeepers{ U->keepers->nb_keepers }; + int const _nbKeepers{ U->keepers->nb_keepers }; // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) - if (nbKeepers) { - Keeper* const K{ &U->keepers->keeper_array[keeperIndex] }; - K->mutex.lock(); - return K; + if (_nbKeepers) { + Keeper* const _K{ &U->keepers->keeper_array[keeperIndex] }; + _K->mutex.lock(); + return _K; } return nullptr; } @@ -711,17 +711,17 @@ void Linda::releaseKeeper(Keeper* K_) const void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_) { - int const n{ lua_gettop(L_) }; - for (int i = start_; i <= n; ++i) { + int const _n{ lua_gettop(L_) }; + for (int _i = start_; _i <= _n; ++_i) { if (mode_ == LookupMode::ToKeeper) { - if (lua_isnil(L_, i)) { + if (lua_isnil(L_, _i)) { kNilSentinel.pushKey(L_); - lua_replace(L_, i); + lua_replace(L_, _i); } } else { - if (kNilSentinel.equals(L_, i)) { + if (kNilSentinel.equals(L_, _i)) { lua_pushnil(L_); - lua_replace(L_, i); + lua_replace(L_, _i); } } } @@ -740,21 +740,21 @@ void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mod */ KeeperCallResult keeper_call(Universe* U_, KeeperState K_, keeper_api_t func_, lua_State* L_, void* linda_, int starting_index_) { - KeeperCallResult result; - int const args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; // L: ... args... K_: - int const top_K{ lua_gettop(K_) }; + KeeperCallResult _result{}; + int const _args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; // L: ... args... K_: + int const _top_K{ lua_gettop(K_) }; // if we didn't do anything wrong, the keeper stack should be clean - LUA_ASSERT(L_, top_K == 0); + LUA_ASSERT(L_, _top_K == 0); STACK_GROW(K_, 2); PUSH_KEEPER_FUNC(K_, func_); // L: ... args... K_: func_ lua_pushlightuserdata(K_, linda_); // L: ... args... K_: func_ linda if ( - (args == 0) || - (InterCopyContext{ U_, DestState{ K_ }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(args) == InterCopyResult::Success) + (_args == 0) || + (InterCopyContext{ U_, DestState{ K_ }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(_args) == InterCopyResult::Success) ) { // L: ... args... K_: func_ linda args... - lua_call(K_, 1 + args, LUA_MULTRET); // L: ... args... K_: result... - int const retvals{ lua_gettop(K_) - top_K }; + lua_call(K_, 1 + _args, LUA_MULTRET); // L: ... args... K_: result... + int const retvals{ lua_gettop(K_) - _top_K }; // note that this can raise a lua error while the keeper state (and its mutex) is acquired // this may interrupt a lane, causing the destruction of the underlying OS thread // after this, another lane making use of this keeper can get an error code from the mutex-locking function @@ -763,29 +763,29 @@ KeeperCallResult keeper_call(Universe* U_, KeeperState K_, keeper_api_t func_, l (retvals == 0) || (InterCopyContext{ U_, DestState{ L_ }, SourceState{ K_ }, {}, {}, {}, LookupMode::FromKeeper, {} }.inter_move(retvals) == InterCopyResult::Success) ) { // L: ... args... result... K_: result... - result.emplace(retvals); + _result.emplace(retvals); } } // whatever happens, restore the stack to where it was at the origin - lua_settop(K_, top_K); // L: ... args... result... K_: + lua_settop(K_, _top_K); // L: ... args... result... K_: // don't do this for this particular function, as it is only called during Linda destruction, and we don't want to raise an error, ever if (func_ != KEEPER_API(clear)) [[unlikely]] { // since keeper state GC is stopped, let's run a step once in a while if required - int const gc_threshold{ U_->keepers->gc_threshold }; - if (gc_threshold == 0) [[unlikely]] { + int const _gc_threshold{ U_->keepers->gc_threshold }; + if (_gc_threshold == 0) [[unlikely]] { lua_gc(K_, LUA_GCSTEP, 0); - } else if (gc_threshold > 0) [[likely]] { - int const gc_usage{ lua_gc(K_, LUA_GCCOUNT, 0) }; - if (gc_usage >= gc_threshold) { + } else if (_gc_threshold > 0) [[likely]] { + int const _gc_usage{ lua_gc(K_, LUA_GCCOUNT, 0) }; + if (_gc_usage >= _gc_threshold) { lua_gc(K_, LUA_GCCOLLECT, 0); - int const gc_usage_after{ lua_gc(K_, LUA_GCCOUNT, 0) }; - if (gc_usage_after > gc_threshold) [[unlikely]] { - raise_luaL_error(L_, "Keeper GC threshold is too low, need at least %d", gc_usage_after); + int const _gc_usage_after{ lua_gc(K_, LUA_GCCOUNT, 0) }; + if (_gc_usage_after > _gc_threshold) [[unlikely]] { + raise_luaL_error(L_, "Keeper GC threshold is too low, need at least %d", _gc_usage_after); } } } } - return result; + return _result; } -- cgit v1.2.3-55-g6feb