diff options
Diffstat (limited to 'src/keeper.cpp')
-rw-r--r-- | src/keeper.cpp | 328 |
1 files changed, 164 insertions, 164 deletions
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 }; | |||
76 | // replaces the fifo ud by its uservalue on the stack | 76 | // replaces the fifo ud by its uservalue on the stack |
77 | [[nodiscard]] static keeper_fifo* prepare_fifo_access(lua_State* L_, int idx_) | 77 | [[nodiscard]] static keeper_fifo* prepare_fifo_access(lua_State* L_, int idx_) |
78 | { | 78 | { |
79 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, idx_) }; | 79 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, idx_) }; |
80 | if (fifo != nullptr) { | 80 | if (_fifo != nullptr) { |
81 | idx_ = lua_absindex(L_, idx_); | 81 | idx_ = lua_absindex(L_, idx_); |
82 | STACK_GROW(L_, 1); | 82 | STACK_GROW(L_, 1); |
83 | // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around | 83 | // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around |
84 | lua_getiuservalue(L_, idx_, kContentsTableIndex); | 84 | lua_getiuservalue(L_, idx_, kContentsTableIndex); |
85 | lua_replace(L_, idx_); | 85 | lua_replace(L_, idx_); |
86 | } | 86 | } |
87 | return fifo; | 87 | return _fifo; |
88 | } | 88 | } |
89 | 89 | ||
90 | // ################################################################################################# | 90 | // ################################################################################################# |
@@ -95,12 +95,12 @@ static constexpr int kContentsTableIndex{ 1 }; | |||
95 | { | 95 | { |
96 | STACK_GROW(L_, 2); | 96 | STACK_GROW(L_, 2); |
97 | STACK_CHECK_START_REL(L_, 0); | 97 | STACK_CHECK_START_REL(L_, 0); |
98 | keeper_fifo* const fifo{ new (L_) keeper_fifo{} }; | 98 | keeper_fifo* const _fifo{ new (L_) keeper_fifo{} }; |
99 | STACK_CHECK(L_, 1); | 99 | STACK_CHECK(L_, 1); |
100 | lua_newtable(L_); | 100 | lua_newtable(L_); |
101 | lua_setiuservalue(L_, -2, kContentsTableIndex); | 101 | lua_setiuservalue(L_, -2, kContentsTableIndex); |
102 | STACK_CHECK(L_, 1); | 102 | STACK_CHECK(L_, 1); |
103 | return fifo; | 103 | return _fifo; |
104 | } | 104 | } |
105 | 105 | ||
106 | // ################################################################################################# | 106 | // ################################################################################################# |
@@ -109,12 +109,12 @@ static constexpr int kContentsTableIndex{ 1 }; | |||
109 | // out: nothing, removes all pushed values from the stack | 109 | // out: nothing, removes all pushed values from the stack |
110 | static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) | 110 | static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) |
111 | { | 111 | { |
112 | int const idx{ lua_gettop(L_) - count_ }; | 112 | int const _idx{ lua_gettop(L_) - count_ }; |
113 | int const start{ fifo_->first + fifo_->count - 1 }; | 113 | int const _start{ fifo_->first + fifo_->count - 1 }; |
114 | // pop all additional arguments, storing them in the fifo | 114 | // pop all additional arguments, storing them in the fifo |
115 | for (int i = count_; i >= 1; --i) { | 115 | for (int _i = count_; _i >= 1; --_i) { |
116 | // store in the fifo the value at the top of the stack at the specified index, popping it from the stack | 116 | // store in the fifo the value at the top of the stack at the specified index, popping it from the stack |
117 | lua_rawseti(L_, idx, start + i); | 117 | lua_rawseti(L_, _idx, _start + _i); |
118 | } | 118 | } |
119 | fifo_->count += count_; | 119 | fifo_->count += count_; |
120 | } | 120 | } |
@@ -129,8 +129,8 @@ static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) | |||
129 | static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) | 129 | static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) |
130 | { | 130 | { |
131 | STACK_GROW(L_, count_); | 131 | STACK_GROW(L_, count_); |
132 | for (int i = 0; i < count_; ++i) { | 132 | for (int _i = 0; _i < count_; ++_i) { |
133 | lua_rawgeti(L_, 1, (fifo_->first + i)); | 133 | lua_rawgeti(L_, 1, (fifo_->first + _i)); |
134 | } | 134 | } |
135 | } | 135 | } |
136 | 136 | ||
@@ -141,32 +141,32 @@ static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) | |||
141 | static void fifo_pop(lua_State* L_, keeper_fifo* fifo_, int count_) | 141 | static void fifo_pop(lua_State* L_, keeper_fifo* fifo_, int count_) |
142 | { | 142 | { |
143 | LUA_ASSERT(L_, lua_istable(L_, -1)); | 143 | LUA_ASSERT(L_, lua_istable(L_, -1)); |
144 | int const fifo_idx{ lua_gettop(L_) }; // L_: ... fifotbl | 144 | int const _fifo_idx{ lua_gettop(L_) }; // L_: ... fifotbl |
145 | // each iteration pushes a value on the stack! | 145 | // each iteration pushes a value on the stack! |
146 | STACK_GROW(L_, count_ + 2); | 146 | STACK_GROW(L_, count_ + 2); |
147 | // skip first item, we will push it last | 147 | // skip first item, we will push it last |
148 | for (int i = 1; i < count_; ++i) { | 148 | for (int i = 1; i < count_; ++i) { |
149 | int const at{ fifo_->first + i }; | 149 | int const at{ fifo_->first + i }; |
150 | // push item on the stack | 150 | // push item on the stack |
151 | lua_rawgeti(L_, fifo_idx, at); // L_: ... fifotbl val | 151 | lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl val |
152 | // remove item from the fifo | 152 | // remove item from the fifo |
153 | lua_pushnil(L_); // L_: ... fifotbl val nil | 153 | lua_pushnil(L_); // L_: ... fifotbl val nil |
154 | lua_rawseti(L_, fifo_idx, at); // L_: ... fifotbl val | 154 | lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl val |
155 | } | 155 | } |
156 | // now process first item | 156 | // now process first item |
157 | { | 157 | { |
158 | int const at{ fifo_->first }; | 158 | int const at{ fifo_->first }; |
159 | lua_rawgeti(L_, fifo_idx, at); // L_: ... fifotbl vals val | 159 | lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl vals val |
160 | lua_pushnil(L_); // L_: ... fifotbl vals val nil | 160 | lua_pushnil(L_); // L_: ... fifotbl vals val nil |
161 | lua_rawseti(L_, fifo_idx, at); // L_: ... fifotbl vals val | 161 | lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl vals val |
162 | lua_replace(L_, fifo_idx); // L_: ... vals | 162 | lua_replace(L_, _fifo_idx); // L_: ... vals |
163 | } | 163 | } |
164 | 164 | ||
165 | // avoid ever-growing indexes by resetting each time we detect the fifo is empty | 165 | // avoid ever-growing indexes by resetting each time we detect the fifo is empty |
166 | { | 166 | { |
167 | int const new_count{ fifo_->count - count_ }; | 167 | int const _new_count{ fifo_->count - count_ }; |
168 | fifo_->first = (new_count == 0) ? 1 : (fifo_->first + count_); | 168 | fifo_->first = (_new_count == 0) ? 1 : (fifo_->first + count_); |
169 | fifo_->count = new_count; | 169 | fifo_->count = _new_count; |
170 | } | 170 | } |
171 | } | 171 | } |
172 | 172 | ||
@@ -202,34 +202,34 @@ static void push_table(lua_State* L_, int idx_) | |||
202 | // only used by linda:dump() and linda:__towatch() for debugging purposes | 202 | // only used by linda:dump() and linda:__towatch() for debugging purposes |
203 | int keeper_push_linda_storage(Linda& linda_, DestState L_) | 203 | int keeper_push_linda_storage(Linda& linda_, DestState L_) |
204 | { | 204 | { |
205 | Keeper* const K{ linda_.whichKeeper() }; | 205 | Keeper* const _K{ linda_.whichKeeper() }; |
206 | SourceState const KL{ K ? K->L : nullptr }; | 206 | SourceState const _KL{ _K ? _K->L : nullptr }; |
207 | if (KL == nullptr) | 207 | if (_KL == nullptr) |
208 | return 0; | 208 | return 0; |
209 | STACK_GROW(KL, 4); | 209 | STACK_GROW(_KL, 4); |
210 | STACK_CHECK_START_REL(KL, 0); | 210 | STACK_CHECK_START_REL(_KL, 0); |
211 | kFifosRegKey.pushValue(KL); // KL: fifos L_: | 211 | kFifosRegKey.pushValue(_KL); // KL: fifos L_: |
212 | lua_pushlightuserdata(KL, &linda_); // KL: fifos ud L_: | 212 | lua_pushlightuserdata(_KL, &linda_); // KL: fifos ud L_: |
213 | lua_rawget(KL, -2); // KL: fifos storage L_: | 213 | lua_rawget(_KL, -2); // KL: fifos storage L_: |
214 | lua_remove(KL, -2); // KL: storage L_: | 214 | lua_remove(_KL, -2); // KL: storage L_: |
215 | if (!lua_istable(KL, -1)) { | 215 | if (!lua_istable(_KL, -1)) { |
216 | lua_pop(KL, 1); // KL: L_: | 216 | lua_pop(_KL, 1); // KL: L_: |
217 | STACK_CHECK(KL, 0); | 217 | STACK_CHECK(_KL, 0); |
218 | return 0; | 218 | return 0; |
219 | } | 219 | } |
220 | // move data from keeper to destination state | 220 | // move data from keeper to destination state |
221 | STACK_GROW(L_, 5); | 221 | STACK_GROW(L_, 5); |
222 | STACK_CHECK_START_REL(L_, 0); | 222 | STACK_CHECK_START_REL(L_, 0); |
223 | lua_newtable(L_); // KL: storage L_: out | 223 | lua_newtable(L_); // KL: storage L_: out |
224 | InterCopyContext c{ linda_.U, L_, KL, {}, {}, {}, LookupMode::FromKeeper, {} }; | 224 | InterCopyContext _c{ linda_.U, L_, _KL, {}, {}, {}, LookupMode::FromKeeper, {} }; |
225 | lua_pushnil(KL); // KL: storage nil L_: out | 225 | lua_pushnil(_KL); // KL: storage nil L_: out |
226 | while (lua_next(KL, -2)) { // KL: storage key fifo L_: out | 226 | while (lua_next(_KL, -2)) { // KL: storage key fifo L_: out |
227 | keeper_fifo* fifo = prepare_fifo_access(KL, -1); // KL: storage key fifotbl L_: out | 227 | keeper_fifo* fifo = prepare_fifo_access(_KL, -1); // KL: storage key fifotbl L_: out |
228 | lua_pushvalue(KL, -2); // KL: storage key fifotbl key L_: out | 228 | lua_pushvalue(_KL, -2); // KL: storage key fifotbl key L_: out |
229 | std::ignore = c.inter_move(1); // KL: storage key fifotbl L_: out key | 229 | std::ignore = _c.inter_move(1); // KL: storage key fifotbl L_: out key |
230 | STACK_CHECK(L_, 2); | 230 | STACK_CHECK(L_, 2); |
231 | lua_newtable(L_); // KL: storage key L_: out key keyout | 231 | lua_newtable(L_); // KL: storage key L_: out key keyout |
232 | std::ignore = c.inter_move(1); // KL: storage key L_: out key keyout fifotbl | 232 | std::ignore = _c.inter_move(1); // KL: storage key L_: out key keyout fifotbl |
233 | lua_pushinteger(L_, fifo->first); // KL: storage key L_: out key keyout fifotbl first | 233 | lua_pushinteger(L_, fifo->first); // KL: storage key L_: out key keyout fifotbl first |
234 | STACK_CHECK(L_, 5); | 234 | STACK_CHECK(L_, 5); |
235 | lua_setfield(L_, -3, "first"); // KL: storage key L_: out key keyout fifotbl | 235 | 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_) | |||
244 | STACK_CHECK(L_, 1); | 244 | STACK_CHECK(L_, 1); |
245 | } // KL_: storage L_: out | 245 | } // KL_: storage L_: out |
246 | STACK_CHECK(L_, 1); | 246 | STACK_CHECK(L_, 1); |
247 | lua_pop(KL, 1); // KL: L_: out | 247 | lua_pop(_KL, 1); // KL: L_: out |
248 | STACK_CHECK(KL, 0); | 248 | STACK_CHECK(_KL, 0); |
249 | return 1; | 249 | return 1; |
250 | } | 250 | } |
251 | 251 | ||
@@ -271,7 +271,7 @@ int keepercall_clear(lua_State* L_) | |||
271 | // out: true|false | 271 | // out: true|false |
272 | int keepercall_send(lua_State* L_) | 272 | int keepercall_send(lua_State* L_) |
273 | { | 273 | { |
274 | int const n{ lua_gettop(L_) - 2 }; | 274 | int const _n{ lua_gettop(L_) - 2 }; |
275 | push_table(L_, 1); // L_: ud key ... fifos | 275 | push_table(L_, 1); // L_: ud key ... fifos |
276 | // get the fifo associated to this key in this linda, create it if it doesn't exist | 276 | // get the fifo associated to this key in this linda, create it if it doesn't exist |
277 | lua_pushvalue(L_, 2); // L_: ud key ... fifos key | 277 | lua_pushvalue(L_, 2); // L_: ud key ... fifos key |
@@ -284,14 +284,14 @@ int keepercall_send(lua_State* L_) | |||
284 | lua_rawset(L_, -4); // L_: ud key ... fifos fifo | 284 | lua_rawset(L_, -4); // L_: ud key ... fifos fifo |
285 | } | 285 | } |
286 | lua_remove(L_, -2); // L_: ud key ... fifo | 286 | lua_remove(L_, -2); // L_: ud key ... fifo |
287 | keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; | 287 | keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; |
288 | if (fifo->limit >= 0 && fifo->count + n > fifo->limit) { | 288 | if (_fifo->limit >= 0 && _fifo->count + _n > _fifo->limit) { |
289 | lua_settop(L_, 0); // L_: | 289 | lua_settop(L_, 0); // L_: |
290 | lua_pushboolean(L_, 0); // L_:false | 290 | lua_pushboolean(L_, 0); // L_:false |
291 | } else { | 291 | } else { |
292 | fifo = prepare_fifo_access(L_, -1); // L_: ud fifotbl | 292 | _fifo = prepare_fifo_access(L_, -1); // L_: ud fifotbl |
293 | lua_replace(L_, 2); // L_: ud fifotbl ... | 293 | lua_replace(L_, 2); // L_: ud fifotbl ... |
294 | fifo_push(L_, fifo, n); // L_: ud fifotbl | 294 | fifo_push(L_, _fifo, _n); // L_: ud fifotbl |
295 | lua_settop(L_, 0); // L_: | 295 | lua_settop(L_, 0); // L_: |
296 | lua_pushboolean(L_, 1); // L_: true | 296 | lua_pushboolean(L_, 1); // L_: true |
297 | } | 297 | } |
@@ -304,19 +304,19 @@ int keepercall_send(lua_State* L_) | |||
304 | // out: (key, val) or nothing | 304 | // out: (key, val) or nothing |
305 | int keepercall_receive(lua_State* L_) | 305 | int keepercall_receive(lua_State* L_) |
306 | { | 306 | { |
307 | int const top{ lua_gettop(L_) }; | 307 | int const _top{ lua_gettop(L_) }; |
308 | push_table(L_, 1); // L_: ud keys fifos | 308 | push_table(L_, 1); // L_: ud keys fifos |
309 | lua_replace(L_, 1); // L_: fifos keys | 309 | lua_replace(L_, 1); // L_: fifos keys |
310 | for (int i = 2; i <= top; ++i) { | 310 | for (int _i = 2; _i <= _top; ++_i) { |
311 | lua_pushvalue(L_, i); // L_: fifos keys key[i] | 311 | lua_pushvalue(L_, _i); // L_: fifos keys key[i] |
312 | lua_rawget(L_, 1); // L_: fifos keys fifo | 312 | lua_rawget(L_, 1); // L_: fifos keys fifo |
313 | keeper_fifo* const fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos keys fifotbl | 313 | keeper_fifo* const _fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos keys fifotbl |
314 | if (fifo != nullptr && fifo->count > 0) { | 314 | if (_fifo != nullptr && _fifo->count > 0) { |
315 | fifo_pop(L_, fifo, 1); // L_: fifos keys val | 315 | fifo_pop(L_, _fifo, 1); // L_: fifos keys val |
316 | if (!lua_isnil(L_, -1)) { | 316 | if (!lua_isnil(L_, -1)) { |
317 | lua_replace(L_, 1); // L_: val keys | 317 | lua_replace(L_, 1); // L_: val keys |
318 | lua_settop(L_, i); // L_: val keys key[i] | 318 | lua_settop(L_, _i); // L_: val keys key[i] |
319 | if (i != 2) { | 319 | if (_i != 2) { |
320 | lua_replace(L_, 2); // L_: val key keys | 320 | lua_replace(L_, 2); // L_: val key keys |
321 | lua_settop(L_, 2); // L_: val key | 321 | lua_settop(L_, 2); // L_: val key |
322 | } | 322 | } |
@@ -324,7 +324,7 @@ int keepercall_receive(lua_State* L_) | |||
324 | return 2; | 324 | return 2; |
325 | } | 325 | } |
326 | } | 326 | } |
327 | lua_settop(L_, top); // L_: data keys | 327 | lua_settop(L_, _top); // L_: data keys |
328 | } | 328 | } |
329 | // nothing to receive | 329 | // nothing to receive |
330 | return 0; | 330 | return 0; |
@@ -335,9 +335,9 @@ int keepercall_receive(lua_State* L_) | |||
335 | // in: linda_ud key mincount [maxcount] | 335 | // in: linda_ud key mincount [maxcount] |
336 | int keepercall_receive_batched(lua_State* L_) | 336 | int keepercall_receive_batched(lua_State* L_) |
337 | { | 337 | { |
338 | int const min_count{ static_cast<int>(lua_tointeger(L_, 3)) }; | 338 | int const _min_count{ static_cast<int>(lua_tointeger(L_, 3)) }; |
339 | if (min_count > 0) { | 339 | if (_min_count > 0) { |
340 | int const max_count{ static_cast<int>(luaL_optinteger(L_, 4, min_count)) }; | 340 | int const _max_count{ static_cast<int>(luaL_optinteger(L_, 4, _min_count)) }; |
341 | lua_settop(L_, 2); // L_: ud key | 341 | lua_settop(L_, 2); // L_: ud key |
342 | lua_insert(L_, 1); // L_: key ud | 342 | lua_insert(L_, 1); // L_: key ud |
343 | push_table(L_, 2); // L_: key ud fifos | 343 | push_table(L_, 2); // L_: key ud fifos |
@@ -345,9 +345,9 @@ int keepercall_receive_batched(lua_State* L_) | |||
345 | lua_pushvalue(L_, 1); // L_: key fifos key | 345 | lua_pushvalue(L_, 1); // L_: key fifos key |
346 | lua_rawget(L_, 2); // L_: key fifos fifo | 346 | lua_rawget(L_, 2); // L_: key fifos fifo |
347 | lua_remove(L_, 2); // L_: key fifo | 347 | lua_remove(L_, 2); // L_: key fifo |
348 | keeper_fifo* const fifo{ prepare_fifo_access(L_, 2) }; // L_: key fifotbl | 348 | keeper_fifo* const _fifo{ prepare_fifo_access(L_, 2) }; // L_: key fifotbl |
349 | if (fifo != nullptr && fifo->count >= min_count) { | 349 | if (_fifo != nullptr && _fifo->count >= _min_count) { |
350 | fifo_pop(L_, fifo, std::min(max_count, fifo->count)); // L_: key ... | 350 | fifo_pop(L_, _fifo, std::min(_max_count, _fifo->count)); // L_: key ... |
351 | } else { | 351 | } else { |
352 | lua_settop(L_, 0); // L_: | 352 | lua_settop(L_, 0); // L_: |
353 | } | 353 | } |
@@ -363,16 +363,16 @@ int keepercall_receive_batched(lua_State* L_) | |||
363 | // out: true or nil | 363 | // out: true or nil |
364 | int keepercall_limit(lua_State* L_) | 364 | int keepercall_limit(lua_State* L_) |
365 | { | 365 | { |
366 | int const limit{ static_cast<int>(lua_tointeger(L_, 3)) }; | 366 | int const _limit{ static_cast<int>(lua_tointeger(L_, 3)) }; |
367 | push_table(L_, 1); // L_: ud key n fifos | 367 | push_table(L_, 1); // L_: ud key n fifos |
368 | lua_replace(L_, 1); // L_: fifos key n | 368 | lua_replace(L_, 1); // L_: fifos key n |
369 | lua_pop(L_, 1); // L_: fifos key | 369 | lua_pop(L_, 1); // L_: fifos key |
370 | lua_pushvalue(L_, -1); // L_: fifos key key | 370 | lua_pushvalue(L_, -1); // L_: fifos key key |
371 | lua_rawget(L_, -3); // L_: fifos key fifo|nil | 371 | lua_rawget(L_, -3); // L_: fifos key fifo|nil |
372 | keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; | 372 | keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; |
373 | if (fifo == nullptr) { // L_: fifos key nil | 373 | if (_fifo == nullptr) { // L_: fifos key nil |
374 | lua_pop(L_, 1); // L_: fifos key | 374 | lua_pop(L_, 1); // L_: fifos key |
375 | fifo = fifo_new(KeeperState{ L_ }); // L_: fifos key fifo | 375 | _fifo = fifo_new(KeeperState{ L_ }); // L_: fifos key fifo |
376 | lua_rawset(L_, -3); // L_: fifos | 376 | lua_rawset(L_, -3); // L_: fifos |
377 | } | 377 | } |
378 | // remove any clutter on the stack | 378 | // remove any clutter on the stack |
@@ -380,13 +380,13 @@ int keepercall_limit(lua_State* L_) | |||
380 | // return true if we decide that blocked threads waiting to write on that key should be awakened | 380 | // return true if we decide that blocked threads waiting to write on that key should be awakened |
381 | // this is the case if we detect the key was full but it is no longer the case | 381 | // this is the case if we detect the key was full but it is no longer the case |
382 | if ( | 382 | if ( |
383 | ((fifo->limit >= 0) && (fifo->count >= fifo->limit)) // the key was full if limited and count exceeded the previous limit | 383 | ((_fifo->limit >= 0) && (_fifo->count >= _fifo->limit)) // the key was full if limited and count exceeded the previous limit |
384 | && ((limit < 0) || (fifo->count < limit)) // the key is not full if unlimited or count is lower than the new limit | 384 | && ((_limit < 0) || (_fifo->count < _limit)) // the key is not full if unlimited or count is lower than the new limit |
385 | ) { | 385 | ) { |
386 | lua_pushboolean(L_, 1); // L_: true | 386 | lua_pushboolean(L_, 1); // L_: true |
387 | } | 387 | } |
388 | // set the new limit | 388 | // set the new limit |
389 | fifo->limit = limit; | 389 | _fifo->limit = _limit; |
390 | // return 0 or 1 value | 390 | // return 0 or 1 value |
391 | return lua_gettop(L_); | 391 | return lua_gettop(L_); |
392 | } | 392 | } |
@@ -397,7 +397,7 @@ int keepercall_limit(lua_State* L_) | |||
397 | // out: true if the linda was full but it's no longer the case, else nothing | 397 | // out: true if the linda was full but it's no longer the case, else nothing |
398 | int keepercall_set(lua_State* L_) | 398 | int keepercall_set(lua_State* L_) |
399 | { | 399 | { |
400 | bool should_wake_writers{ false }; | 400 | bool _should_wake_writers{ false }; |
401 | STACK_GROW(L_, 6); | 401 | STACK_GROW(L_, 6); |
402 | 402 | ||
403 | // retrieve fifos associated with the linda | 403 | // retrieve fifos associated with the linda |
@@ -409,28 +409,28 @@ int keepercall_set(lua_State* L_) | |||
409 | lua_pushvalue(L_, -1); // L_: fifos key key | 409 | lua_pushvalue(L_, -1); // L_: fifos key key |
410 | lua_rawget(L_, 1); // L_: fifos key fifo|nil | 410 | lua_rawget(L_, 1); // L_: fifos key fifo|nil |
411 | // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! | 411 | // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! |
412 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; | 412 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; |
413 | if (fifo != nullptr) { // might be nullptr if we set a nonexistent key to nil // L_: fifos key fifo | 413 | if (_fifo != nullptr) { // might be nullptr if we set a nonexistent key to nil // L_: fifos key fifo |
414 | if (fifo->limit < 0) { // fifo limit value is the default (unlimited): we can totally remove it | 414 | if (_fifo->limit < 0) { // fifo limit value is the default (unlimited): we can totally remove it |
415 | lua_pop(L_, 1); // L_: fifos key | 415 | lua_pop(L_, 1); // L_: fifos key |
416 | lua_pushnil(L_); // L_: fifos key nil | 416 | lua_pushnil(L_); // L_: fifos key nil |
417 | lua_rawset(L_, -3); // L_: fifos | 417 | lua_rawset(L_, -3); // L_: fifos |
418 | } else { | 418 | } else { |
419 | // we create room if the fifo was full but it is no longer the case | 419 | // we create room if the fifo was full but it is no longer the case |
420 | should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit); | 420 | _should_wake_writers = (_fifo->limit > 0) && (_fifo->count >= _fifo->limit); |
421 | lua_remove(L_, -2); // L_: fifos fifo | 421 | lua_remove(L_, -2); // L_: fifos fifo |
422 | lua_newtable(L_); // L_: fifos fifo {} | 422 | lua_newtable(L_); // L_: fifos fifo {} |
423 | lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos fifo | 423 | lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos fifo |
424 | fifo->first = 1; | 424 | _fifo->first = 1; |
425 | fifo->count = 0; | 425 | _fifo->count = 0; |
426 | } | 426 | } |
427 | } | 427 | } |
428 | } else { // set/replace contents stored at the specified key? | 428 | } else { // set/replace contents stored at the specified key? |
429 | int const count{ lua_gettop(L_) - 2 }; // number of items we want to store | 429 | int const _count{ lua_gettop(L_) - 2 }; // number of items we want to store |
430 | lua_pushvalue(L_, 2); // L_: fifos key [val [, ...]] key | 430 | lua_pushvalue(L_, 2); // L_: fifos key [val [, ...]] key |
431 | lua_rawget(L_, 1); // L_: fifos key [val [, ...]] fifo|nil | 431 | lua_rawget(L_, 1); // L_: fifos key [val [, ...]] fifo|nil |
432 | keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; | 432 | keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; |
433 | if (fifo == nullptr) { // can be nullptr if we store a value at a new key // fifos key [val [, ...]] nil | 433 | if (_fifo == nullptr) { // can be nullptr if we store a value at a new key // fifos key [val [, ...]] nil |
434 | // no need to wake writers in that case, because a writer can't wait on an inexistent key | 434 | // no need to wake writers in that case, because a writer can't wait on an inexistent key |
435 | lua_pop(L_, 1); // L_: fifos key [val [, ...]] | 435 | lua_pop(L_, 1); // L_: fifos key [val [, ...]] |
436 | std::ignore = fifo_new(KeeperState{ L_ }); // L_: fifos key [val [, ...]] fifo | 436 | std::ignore = fifo_new(KeeperState{ L_ }); // L_: fifos key [val [, ...]] fifo |
@@ -440,19 +440,19 @@ int keepercall_set(lua_State* L_) | |||
440 | } else { // L_: fifos key [val [, ...]] fifo | 440 | } else { // L_: fifos key [val [, ...]] fifo |
441 | // the fifo exists, we just want to update its contents | 441 | // the fifo exists, we just want to update its contents |
442 | // we create room if the fifo was full but it is no longer the case | 442 | // we create room if the fifo was full but it is no longer the case |
443 | should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit) && (count < fifo->limit); | 443 | _should_wake_writers = (_fifo->limit > 0) && (_fifo->count >= _fifo->limit) && (_count < _fifo->limit); |
444 | // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! | 444 | // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! |
445 | lua_newtable(L_); // L_: fifos key [val [, ...]] fifo {} | 445 | lua_newtable(L_); // L_: fifos key [val [, ...]] fifo {} |
446 | lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos key [val [, ...]] fifo | 446 | lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos key [val [, ...]] fifo |
447 | fifo->first = 1; | 447 | _fifo->first = 1; |
448 | fifo->count = 0; | 448 | _fifo->count = 0; |
449 | } | 449 | } |
450 | fifo = prepare_fifo_access(L_, -1); // L_: fifos key [val [, ...]] fifotbl | 450 | _fifo = prepare_fifo_access(L_, -1); // L_: fifos key [val [, ...]] fifotbl |
451 | // move the fifo below the values we want to store | 451 | // move the fifo below the values we want to store |
452 | lua_insert(L_, 3); // L_: fifos key fifotbl [val [, ...]] | 452 | lua_insert(L_, 3); // L_: fifos key fifotbl [val [, ...]] |
453 | fifo_push(L_, fifo, count); // L_: fifos key fifotbl | 453 | fifo_push(L_, _fifo, _count); // L_: fifos key fifotbl |
454 | } | 454 | } |
455 | return should_wake_writers ? (lua_pushboolean(L_, 1), 1) : 0; | 455 | return _should_wake_writers ? (lua_pushboolean(L_, 1), 1) : 0; |
456 | } | 456 | } |
457 | 457 | ||
458 | // ################################################################################################# | 458 | // ################################################################################################# |
@@ -461,21 +461,21 @@ int keepercall_set(lua_State* L_) | |||
461 | // out: at most <count> values | 461 | // out: at most <count> values |
462 | int keepercall_get(lua_State* L_) | 462 | int keepercall_get(lua_State* L_) |
463 | { | 463 | { |
464 | int count{ 1 }; | 464 | int _count{ 1 }; |
465 | if (lua_gettop(L_) == 3) { // L_: ud key count | 465 | if (lua_gettop(L_) == 3) { // L_: ud key count |
466 | count = static_cast<int>(lua_tointeger(L_, 3)); | 466 | _count = static_cast<int>(lua_tointeger(L_, 3)); |
467 | lua_pop(L_, 1); // L_: ud key | 467 | lua_pop(L_, 1); // L_: ud key |
468 | } | 468 | } |
469 | push_table(L_, 1); // L_: ud key fifos | 469 | push_table(L_, 1); // L_: ud key fifos |
470 | lua_replace(L_, 1); // L_: fifos key | 470 | lua_replace(L_, 1); // L_: fifos key |
471 | lua_rawget(L_, 1); // L_: fifos fifo | 471 | lua_rawget(L_, 1); // L_: fifos fifo |
472 | keeper_fifo* const fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos fifotbl | 472 | keeper_fifo* const _fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos fifotbl |
473 | if (fifo != nullptr && fifo->count > 0) { | 473 | if (_fifo != nullptr && _fifo->count > 0) { |
474 | lua_remove(L_, 1); // L_: fifotbl | 474 | lua_remove(L_, 1); // L_: fifotbl |
475 | count = std::min(count, fifo->count); | 475 | _count = std::min(_count, _fifo->count); |
476 | // read <count> value off the fifo | 476 | // read <count> value off the fifo |
477 | fifo_peek(L_, fifo, count); // L_: fifotbl ... | 477 | fifo_peek(L_, _fifo, _count); // L_: fifotbl ... |
478 | return count; | 478 | return _count; |
479 | } | 479 | } |
480 | // no fifo was ever registered for this key, or it is empty | 480 | // no fifo was ever registered for this key, or it is empty |
481 | return 0; | 481 | return 0; |
@@ -494,10 +494,10 @@ int keepercall_count(lua_State* L_) | |||
494 | lua_replace(L_, 1); // L_: out fifos | 494 | lua_replace(L_, 1); // L_: out fifos |
495 | lua_pushnil(L_); // L_: out fifos nil | 495 | lua_pushnil(L_); // L_: out fifos nil |
496 | while (lua_next(L_, 2)) { // L_: out fifos key fifo | 496 | while (lua_next(L_, 2)) { // L_: out fifos key fifo |
497 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; | 497 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; |
498 | lua_pop(L_, 1); // L_: out fifos key | 498 | lua_pop(L_, 1); // L_: out fifos key |
499 | lua_pushvalue(L_, -1); // L_: out fifos key key | 499 | lua_pushvalue(L_, -1); // L_: out fifos key key |
500 | lua_pushinteger(L_, fifo->count); // L_: out fifos key key count | 500 | lua_pushinteger(L_, _fifo->count); // L_: out fifos key key count |
501 | lua_rawset(L_, -5); // L_: out fifos key | 501 | lua_rawset(L_, -5); // L_: out fifos key |
502 | } | 502 | } |
503 | lua_pop(L_, 1); // L_: out | 503 | lua_pop(L_, 1); // L_: out |
@@ -510,8 +510,8 @@ int keepercall_count(lua_State* L_) | |||
510 | if (lua_isnil(L_, -1)) { // L_: the key is unknown // L_: fifos nil | 510 | if (lua_isnil(L_, -1)) { // L_: the key is unknown // L_: fifos nil |
511 | lua_remove(L_, -2); // L_: nil | 511 | lua_remove(L_, -2); // L_: nil |
512 | } else { // the key is known // L_: fifos fifo | 512 | } else { // the key is known // L_: fifos fifo |
513 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; | 513 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; |
514 | lua_pushinteger(L_, fifo->count); // L_: fifos fifo count | 514 | lua_pushinteger(L_, _fifo->count); // L_: fifos fifo count |
515 | lua_replace(L_, -3); // L_: count fifo | 515 | lua_replace(L_, -3); // L_: count fifo |
516 | lua_pop(L_, 1); // L_: count | 516 | lua_pop(L_, 1); // L_: count |
517 | } | 517 | } |
@@ -526,10 +526,10 @@ int keepercall_count(lua_State* L_) | |||
526 | while (lua_gettop(L_) > 2) { | 526 | while (lua_gettop(L_) > 2) { |
527 | lua_pushvalue(L_, -1); // L_: out fifos keys... key | 527 | lua_pushvalue(L_, -1); // L_: out fifos keys... key |
528 | lua_rawget(L_, 2); // L_: out fifos keys... fifo|nil | 528 | lua_rawget(L_, 2); // L_: out fifos keys... fifo|nil |
529 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; | 529 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; |
530 | lua_pop(L_, 1); // L_: out fifos keys... | 530 | lua_pop(L_, 1); // L_: out fifos keys... |
531 | if (fifo != nullptr) { // L_: the key is known | 531 | if (_fifo != nullptr) { // L_: the key is known |
532 | lua_pushinteger(L_, fifo->count); // L_: out fifos keys... count | 532 | lua_pushinteger(L_, _fifo->count); // L_: out fifos keys... count |
533 | lua_rawset(L_, 1); // L_: out fifos keys... | 533 | lua_rawset(L_, 1); // L_: out fifos keys... |
534 | } else { // the key is unknown | 534 | } else { // the key is unknown |
535 | lua_pop(L_, 1); // L_: out fifos keys... | 535 | lua_pop(L_, 1); // L_: out fifos keys... |
@@ -557,27 +557,27 @@ int keepercall_count(lua_State* L_) | |||
557 | void close_keepers(Universe* U_) | 557 | void close_keepers(Universe* U_) |
558 | { | 558 | { |
559 | if (U_->keepers != nullptr) { | 559 | if (U_->keepers != nullptr) { |
560 | int nbKeepers{ U_->keepers->nb_keepers }; | 560 | int _nbKeepers{ U_->keepers->nb_keepers }; |
561 | // 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 | 561 | // 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 |
562 | // 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 | 562 | // 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 |
563 | // in that case, the linda operation should do nothing. which means that these operations must check for keeper acquisition success | 563 | // in that case, the linda operation should do nothing. which means that these operations must check for keeper acquisition success |
564 | // which is early-outed with a U->keepers->nbKeepers null-check | 564 | // which is early-outed with a U->keepers->nbKeepers null-check |
565 | U_->keepers->nb_keepers = 0; | 565 | U_->keepers->nb_keepers = 0; |
566 | for (int i = 0; i < nbKeepers; ++i) { | 566 | for (int _i = 0; _i < _nbKeepers; ++_i) { |
567 | lua_State* const K{ U_->keepers->keeper_array[i].L }; | 567 | lua_State* const K{ U_->keepers->keeper_array[_i].L }; |
568 | U_->keepers->keeper_array[i].L = KeeperState{ nullptr }; | 568 | U_->keepers->keeper_array[_i].L = KeeperState{ nullptr }; |
569 | if (K != nullptr) { | 569 | if (K != nullptr) { |
570 | lua_close(K); | 570 | lua_close(K); |
571 | } else { | 571 | } else { |
572 | // detected partial init: destroy only the mutexes that got initialized properly | 572 | // detected partial init: destroy only the mutexes that got initialized properly |
573 | nbKeepers = i; | 573 | _nbKeepers = _i; |
574 | } | 574 | } |
575 | } | 575 | } |
576 | for (int i = 0; i < nbKeepers; ++i) { | 576 | for (int _i = 0; _i < _nbKeepers; ++_i) { |
577 | U_->keepers->keeper_array[i].~Keeper(); | 577 | U_->keepers->keeper_array[_i].~Keeper(); |
578 | } | 578 | } |
579 | // free the keeper bookkeeping structure | 579 | // free the keeper bookkeeping structure |
580 | U_->internalAllocator.free(U_->keepers, sizeof(Keepers) + (nbKeepers - 1) * sizeof(Keeper)); | 580 | U_->internalAllocator.free(U_->keepers, sizeof(Keepers) + (_nbKeepers - 1) * sizeof(Keeper)); |
581 | U_->keepers = nullptr; | 581 | U_->keepers = nullptr; |
582 | } | 582 | } |
583 | } | 583 | } |
@@ -600,10 +600,10 @@ void init_keepers(Universe* U_, lua_State* L_) | |||
600 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); | 600 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); |
601 | STACK_CHECK_START_REL(L_, 0); // L_: settings | 601 | STACK_CHECK_START_REL(L_, 0); // L_: settings |
602 | lua_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers | 602 | lua_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers |
603 | int const nb_keepers{ static_cast<int>(lua_tointeger(L_, -1)) }; | 603 | int const _nb_keepers{ static_cast<int>(lua_tointeger(L_, -1)) }; |
604 | lua_pop(L_, 1); // L_: settings | 604 | lua_pop(L_, 1); // L_: settings |
605 | if (nb_keepers < 1) { | 605 | if (_nb_keepers < 1) { |
606 | raise_luaL_error(L_, "Bad number of keepers (%d)", nb_keepers); | 606 | raise_luaL_error(L_, "Bad number of keepers (%d)", _nb_keepers); |
607 | } | 607 | } |
608 | STACK_CHECK(L_, 0); | 608 | STACK_CHECK(L_, 0); |
609 | 609 | ||
@@ -614,51 +614,51 @@ void init_keepers(Universe* U_, lua_State* L_) | |||
614 | 614 | ||
615 | // Keepers contains an array of 1 Keeper, adjust for the actual number of keeper states | 615 | // Keepers contains an array of 1 Keeper, adjust for the actual number of keeper states |
616 | { | 616 | { |
617 | size_t const bytes = sizeof(Keepers) + (nb_keepers - 1) * sizeof(Keeper); | 617 | size_t const bytes = sizeof(Keepers) + (_nb_keepers - 1) * sizeof(Keeper); |
618 | U_->keepers = static_cast<Keepers*>(U_->internalAllocator.alloc(bytes)); | 618 | U_->keepers = static_cast<Keepers*>(U_->internalAllocator.alloc(bytes)); |
619 | if (U_->keepers == nullptr) { | 619 | if (U_->keepers == nullptr) { |
620 | raise_luaL_error(L_, "init_keepers() failed while creating keeper array; out of memory"); | 620 | raise_luaL_error(L_, "init_keepers() failed while creating keeper array; out of memory"); |
621 | } | 621 | } |
622 | U_->keepers->Keepers::Keepers(); | 622 | U_->keepers->Keepers::Keepers(); |
623 | U_->keepers->gc_threshold = keepers_gc_threshold; | 623 | U_->keepers->gc_threshold = keepers_gc_threshold; |
624 | U_->keepers->nb_keepers = nb_keepers; | 624 | U_->keepers->nb_keepers = _nb_keepers; |
625 | 625 | ||
626 | for (int i = 0; i < nb_keepers; ++i) { | 626 | for (int _i = 0; _i < _nb_keepers; ++_i) { |
627 | U_->keepers->keeper_array[i].Keeper::Keeper(); | 627 | U_->keepers->keeper_array[_i].Keeper::Keeper(); |
628 | } | 628 | } |
629 | } | 629 | } |
630 | for (int i = 0; i < nb_keepers; ++i) { | 630 | for (int _i = 0; _i < _nb_keepers; ++_i) { |
631 | // note that we will leak K if we raise an error later | 631 | // note that we will leak K if we raise an error later |
632 | KeeperState const K{ create_state(U_, L_) }; // L_: settings K: | 632 | KeeperState const _K{ create_state(U_, L_) }; // L_: settings K: |
633 | if (K == nullptr) { | 633 | if (_K == nullptr) { |
634 | raise_luaL_error(L_, "init_keepers() failed while creating keeper states; out of memory"); | 634 | raise_luaL_error(L_, "init_keepers() failed while creating keeper states; out of memory"); |
635 | } | 635 | } |
636 | 636 | ||
637 | U_->keepers->keeper_array[i].L = K; | 637 | U_->keepers->keeper_array[_i].L = _K; |
638 | 638 | ||
639 | if (U_->keepers->gc_threshold >= 0) { | 639 | if (U_->keepers->gc_threshold >= 0) { |
640 | lua_gc(K, LUA_GCSTOP, 0); | 640 | lua_gc(_K, LUA_GCSTOP, 0); |
641 | } | 641 | } |
642 | 642 | ||
643 | STACK_CHECK_START_ABS(K, 0); | 643 | STACK_CHECK_START_ABS(_K, 0); |
644 | 644 | ||
645 | // copy the universe pointer in the keeper itself | 645 | // copy the universe pointer in the keeper itself |
646 | universe_store(K, U_); | 646 | universe_store(_K, U_); |
647 | STACK_CHECK(K, 0); | 647 | STACK_CHECK(_K, 0); |
648 | 648 | ||
649 | // make sure 'package' is initialized in keeper states, so that we have require() | 649 | // make sure 'package' is initialized in keeper states, so that we have require() |
650 | // this because this is needed when transferring deep userdata object | 650 | // this because this is needed when transferring deep userdata object |
651 | luaL_requiref(K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package | 651 | luaL_requiref(_K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package |
652 | lua_pop(K, 1); // L_: settings K: | 652 | lua_pop(_K, 1); // L_: settings K: |
653 | STACK_CHECK(K, 0); | 653 | STACK_CHECK(_K, 0); |
654 | serialize_require(DEBUGSPEW_PARAM_COMMA(U_) K); | 654 | serialize_require(DEBUGSPEW_PARAM_COMMA(U_) _K); |
655 | STACK_CHECK(K, 0); | 655 | STACK_CHECK(_K, 0); |
656 | 656 | ||
657 | // copy package.path and package.cpath from the source state | 657 | // copy package.path and package.cpath from the source state |
658 | if (luaG_getmodule(L_, LUA_LOADLIBNAME) != LuaType::NIL) { // L_: settings package K: | 658 | if (luaG_getmodule(L_, LUA_LOADLIBNAME) != LuaType::NIL) { // L_: settings package K: |
659 | // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately | 659 | // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately |
660 | InterCopyContext c{ U_, DestState{ K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} }; | 660 | InterCopyContext _c{ U_, DestState{ _K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} }; |
661 | if (c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K: | 661 | if (_c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K: |
662 | // if something went wrong, the error message is at the top of the stack | 662 | // if something went wrong, the error message is at the top of the stack |
663 | lua_remove(L_, -2); // L_: settings error_msg | 663 | lua_remove(L_, -2); // L_: settings error_msg |
664 | raise_lua_error(L_); | 664 | raise_lua_error(L_); |
@@ -666,19 +666,19 @@ void init_keepers(Universe* U_, lua_State* L_) | |||
666 | } | 666 | } |
667 | lua_pop(L_, 1); // L_: settings K: | 667 | lua_pop(L_, 1); // L_: settings K: |
668 | STACK_CHECK(L_, 0); | 668 | STACK_CHECK(L_, 0); |
669 | STACK_CHECK(K, 0); | 669 | STACK_CHECK(_K, 0); |
670 | 670 | ||
671 | // attempt to call on_state_create(), if we have one and it is a C function | 671 | // attempt to call on_state_create(), if we have one and it is a C function |
672 | // (only support a C function because we can't transfer executable Lua code in keepers) | 672 | // (only support a C function because we can't transfer executable Lua code in keepers) |
673 | // will raise an error in L_ in case of problem | 673 | // will raise an error in L_ in case of problem |
674 | callOnStateCreate(U_, K, L_, LookupMode::ToKeeper); | 674 | callOnStateCreate(U_, _K, L_, LookupMode::ToKeeper); |
675 | 675 | ||
676 | // to see VM name in Decoda debugger | 676 | // to see VM name in Decoda debugger |
677 | lua_pushfstring(K, "Keeper #%d", i + 1); // L_: settings K: "Keeper #n" | 677 | lua_pushfstring(_K, "Keeper #%d", _i + 1); // L_: settings K: "Keeper #n" |
678 | lua_setglobal(K, "decoda_name"); // L_: settings K: | 678 | lua_setglobal(_K, "decoda_name"); // L_: settings K: |
679 | // create the fifos table in the keeper state | 679 | // create the fifos table in the keeper state |
680 | kFifosRegKey.setValue(K, [](lua_State* L_) { lua_newtable(L_); }); // L_: settings K: | 680 | kFifosRegKey.setValue(_K, [](lua_State* L_) { lua_newtable(L_); }); // L_: settings K: |
681 | STACK_CHECK(K, 0); | 681 | STACK_CHECK(_K, 0); |
682 | } | 682 | } |
683 | STACK_CHECK(L_, 0); | 683 | STACK_CHECK(L_, 0); |
684 | } | 684 | } |
@@ -687,12 +687,12 @@ void init_keepers(Universe* U_, lua_State* L_) | |||
687 | 687 | ||
688 | Keeper* Linda::acquireKeeper() const | 688 | Keeper* Linda::acquireKeeper() const |
689 | { | 689 | { |
690 | int const nbKeepers{ U->keepers->nb_keepers }; | 690 | int const _nbKeepers{ U->keepers->nb_keepers }; |
691 | // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) | 691 | // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) |
692 | if (nbKeepers) { | 692 | if (_nbKeepers) { |
693 | Keeper* const K{ &U->keepers->keeper_array[keeperIndex] }; | 693 | Keeper* const _K{ &U->keepers->keeper_array[keeperIndex] }; |
694 | K->mutex.lock(); | 694 | _K->mutex.lock(); |
695 | return K; | 695 | return _K; |
696 | } | 696 | } |
697 | return nullptr; | 697 | return nullptr; |
698 | } | 698 | } |
@@ -711,17 +711,17 @@ void Linda::releaseKeeper(Keeper* K_) const | |||
711 | 711 | ||
712 | void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_) | 712 | void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_) |
713 | { | 713 | { |
714 | int const n{ lua_gettop(L_) }; | 714 | int const _n{ lua_gettop(L_) }; |
715 | for (int i = start_; i <= n; ++i) { | 715 | for (int _i = start_; _i <= _n; ++_i) { |
716 | if (mode_ == LookupMode::ToKeeper) { | 716 | if (mode_ == LookupMode::ToKeeper) { |
717 | if (lua_isnil(L_, i)) { | 717 | if (lua_isnil(L_, _i)) { |
718 | kNilSentinel.pushKey(L_); | 718 | kNilSentinel.pushKey(L_); |
719 | lua_replace(L_, i); | 719 | lua_replace(L_, _i); |
720 | } | 720 | } |
721 | } else { | 721 | } else { |
722 | if (kNilSentinel.equals(L_, i)) { | 722 | if (kNilSentinel.equals(L_, _i)) { |
723 | lua_pushnil(L_); | 723 | lua_pushnil(L_); |
724 | lua_replace(L_, i); | 724 | lua_replace(L_, _i); |
725 | } | 725 | } |
726 | } | 726 | } |
727 | } | 727 | } |
@@ -740,21 +740,21 @@ void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mod | |||
740 | */ | 740 | */ |
741 | KeeperCallResult keeper_call(Universe* U_, KeeperState K_, keeper_api_t func_, lua_State* L_, void* linda_, int starting_index_) | 741 | KeeperCallResult keeper_call(Universe* U_, KeeperState K_, keeper_api_t func_, lua_State* L_, void* linda_, int starting_index_) |
742 | { | 742 | { |
743 | KeeperCallResult result; | 743 | KeeperCallResult _result{}; |
744 | int const args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; // L: ... args... K_: | 744 | int const _args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; // L: ... args... K_: |
745 | int const top_K{ lua_gettop(K_) }; | 745 | int const _top_K{ lua_gettop(K_) }; |
746 | // if we didn't do anything wrong, the keeper stack should be clean | 746 | // if we didn't do anything wrong, the keeper stack should be clean |
747 | LUA_ASSERT(L_, top_K == 0); | 747 | LUA_ASSERT(L_, _top_K == 0); |
748 | 748 | ||
749 | STACK_GROW(K_, 2); | 749 | STACK_GROW(K_, 2); |
750 | PUSH_KEEPER_FUNC(K_, func_); // L: ... args... K_: func_ | 750 | PUSH_KEEPER_FUNC(K_, func_); // L: ... args... K_: func_ |
751 | lua_pushlightuserdata(K_, linda_); // L: ... args... K_: func_ linda | 751 | lua_pushlightuserdata(K_, linda_); // L: ... args... K_: func_ linda |
752 | if ( | 752 | if ( |
753 | (args == 0) || | 753 | (_args == 0) || |
754 | (InterCopyContext{ U_, DestState{ K_ }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(args) == InterCopyResult::Success) | 754 | (InterCopyContext{ U_, DestState{ K_ }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(_args) == InterCopyResult::Success) |
755 | ) { // L: ... args... K_: func_ linda args... | 755 | ) { // L: ... args... K_: func_ linda args... |
756 | lua_call(K_, 1 + args, LUA_MULTRET); // L: ... args... K_: result... | 756 | lua_call(K_, 1 + _args, LUA_MULTRET); // L: ... args... K_: result... |
757 | int const retvals{ lua_gettop(K_) - top_K }; | 757 | int const retvals{ lua_gettop(K_) - _top_K }; |
758 | // note that this can raise a lua error while the keeper state (and its mutex) is acquired | 758 | // note that this can raise a lua error while the keeper state (and its mutex) is acquired |
759 | // this may interrupt a lane, causing the destruction of the underlying OS thread | 759 | // this may interrupt a lane, causing the destruction of the underlying OS thread |
760 | // after this, another lane making use of this keeper can get an error code from the mutex-locking function | 760 | // 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 | |||
763 | (retvals == 0) || | 763 | (retvals == 0) || |
764 | (InterCopyContext{ U_, DestState{ L_ }, SourceState{ K_ }, {}, {}, {}, LookupMode::FromKeeper, {} }.inter_move(retvals) == InterCopyResult::Success) | 764 | (InterCopyContext{ U_, DestState{ L_ }, SourceState{ K_ }, {}, {}, {}, LookupMode::FromKeeper, {} }.inter_move(retvals) == InterCopyResult::Success) |
765 | ) { // L: ... args... result... K_: result... | 765 | ) { // L: ... args... result... K_: result... |
766 | result.emplace(retvals); | 766 | _result.emplace(retvals); |
767 | } | 767 | } |
768 | } | 768 | } |
769 | // whatever happens, restore the stack to where it was at the origin | 769 | // whatever happens, restore the stack to where it was at the origin |
770 | lua_settop(K_, top_K); // L: ... args... result... K_: | 770 | lua_settop(K_, _top_K); // L: ... args... result... K_: |
771 | 771 | ||
772 | // 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 | 772 | // 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 |
773 | if (func_ != KEEPER_API(clear)) [[unlikely]] { | 773 | if (func_ != KEEPER_API(clear)) [[unlikely]] { |
774 | // since keeper state GC is stopped, let's run a step once in a while if required | 774 | // since keeper state GC is stopped, let's run a step once in a while if required |
775 | int const gc_threshold{ U_->keepers->gc_threshold }; | 775 | int const _gc_threshold{ U_->keepers->gc_threshold }; |
776 | if (gc_threshold == 0) [[unlikely]] { | 776 | if (_gc_threshold == 0) [[unlikely]] { |
777 | lua_gc(K_, LUA_GCSTEP, 0); | 777 | lua_gc(K_, LUA_GCSTEP, 0); |
778 | } else if (gc_threshold > 0) [[likely]] { | 778 | } else if (_gc_threshold > 0) [[likely]] { |
779 | int const gc_usage{ lua_gc(K_, LUA_GCCOUNT, 0) }; | 779 | int const _gc_usage{ lua_gc(K_, LUA_GCCOUNT, 0) }; |
780 | if (gc_usage >= gc_threshold) { | 780 | if (_gc_usage >= _gc_threshold) { |
781 | lua_gc(K_, LUA_GCCOLLECT, 0); | 781 | lua_gc(K_, LUA_GCCOLLECT, 0); |
782 | int const gc_usage_after{ lua_gc(K_, LUA_GCCOUNT, 0) }; | 782 | int const _gc_usage_after{ lua_gc(K_, LUA_GCCOUNT, 0) }; |
783 | if (gc_usage_after > gc_threshold) [[unlikely]] { | 783 | if (_gc_usage_after > _gc_threshold) [[unlikely]] { |
784 | raise_luaL_error(L_, "Keeper GC threshold is too low, need at least %d", gc_usage_after); | 784 | raise_luaL_error(L_, "Keeper GC threshold is too low, need at least %d", _gc_usage_after); |
785 | } | 785 | } |
786 | } | 786 | } |
787 | } | 787 | } |
788 | } | 788 | } |
789 | 789 | ||
790 | return result; | 790 | return _result; |
791 | } | 791 | } |