diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-12 18:18:24 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-12 18:18:24 +0200 |
commit | dddc28153796f9c8eb256eddb335c8643226fd0b (patch) | |
tree | 641caa9a01933d0397a99f127cff249d3a77fdb5 /src/keeper.cpp | |
parent | c305ff3ed1f51d86ced2bb83b10b5e0632cd98a3 (diff) | |
download | lanes-dddc28153796f9c8eb256eddb335c8643226fd0b.tar.gz lanes-dddc28153796f9c8eb256eddb335c8643226fd0b.tar.bz2 lanes-dddc28153796f9c8eb256eddb335c8643226fd0b.zip |
linda :get(), :set(), :limit() return value changes
Diffstat (limited to 'src/keeper.cpp')
-rw-r--r-- | src/keeper.cpp | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/src/keeper.cpp b/src/keeper.cpp index 7000372..4175d84 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp | |||
@@ -123,26 +123,32 @@ KeyUD* KeyUD::GetPtr(KeeperState const K_, int idx_) | |||
123 | // ################################################################################################# | 123 | // ################################################################################################# |
124 | 124 | ||
125 | // in: fifo | 125 | // in: fifo |
126 | // out: ...|nothing | 126 | // out: bool ... |
127 | // pops the fifo, push as much data as is available (up to the specified count) without consuming it | 127 | // pops the fifo, push bool + as much data as is available (up to the specified count) without consuming it |
128 | // bool is true if the requested count was served, else false | ||
128 | void KeyUD::peek(KeeperState const K_, int const count_) | 129 | void KeyUD::peek(KeeperState const K_, int const count_) |
129 | { | 130 | { |
130 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, -1) == this); | 131 | STACK_CHECK_START_ABS(K_, 1); |
132 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, -1) == this); // K_: KeyUD | ||
131 | if (count <= 0) { // no data is available | 133 | if (count <= 0) { // no data is available |
132 | lua_pop(K_, 1); // K_: | 134 | lua_pop(K_, 1); // K_: |
135 | lua_pushinteger(K_, 0); // K_: 0 | ||
133 | return; | 136 | return; |
134 | } | 137 | } |
135 | 138 | ||
136 | // read <count_> value off the fifo | 139 | // read <count_> value off the fifo, if possible |
137 | prepareAccess(K_, -1); // K_: fifo | 140 | prepareAccess(K_, -1); // K_: fifo |
138 | int const _at{ lua_gettop(K_) }; | ||
139 | int const _count{ std::min(count_, count) }; | 141 | int const _count{ std::min(count_, count) }; |
142 | lua_pushinteger(K_, _count); // K_: fifo _count | ||
143 | lua_insert(K_, 1); // K_: _count fifo | ||
144 | STACK_CHECK(K_, 2); | ||
140 | STACK_GROW(K_, _count); | 145 | STACK_GROW(K_, _count); |
141 | for (int const _i : std::ranges::iota_view{ 1, _count }) { // push val2 to valN | 146 | for (int const _i : std::ranges::iota_view{ 1, _count }) { // push val2 to valN |
142 | lua_rawgeti(K_, 1, first + _i); // K_: fifo val2..N | 147 | lua_rawgeti(K_, 2, first + _i); // K_: _count fifo val2..N |
143 | } | 148 | } |
144 | lua_rawgeti(K_, 1, first); // push val1 // K_: fifo val2..N val1 | 149 | lua_rawgeti(K_, 2, first); // push val1 // K_: _count fifo val2..N val1 |
145 | lua_replace(K_, _at); // replace fifo by val1 to get the output properly ordered // K_: val1..N | 150 | lua_replace(K_, 2); // replace fifo by val1 to get the output properly ordered // K_: _count val1..N |
151 | STACK_CHECK(K_, 1 + _count); | ||
146 | } | 152 | } |
147 | 153 | ||
148 | // ################################################################################################# | 154 | // ################################################################################################# |
@@ -418,7 +424,7 @@ int keepercall_destruct(lua_State* const L_) | |||
418 | // ################################################################################################# | 424 | // ################################################################################################# |
419 | 425 | ||
420 | // in: linda_ud key [count] | 426 | // in: linda_ud key [count] |
421 | // out: at most <count> values | 427 | // out: bool + at most <count> values |
422 | int keepercall_get(lua_State* const L_) | 428 | int keepercall_get(lua_State* const L_) |
423 | { | 429 | { |
424 | KeeperState const _K{ L_ }; | 430 | KeeperState const _K{ L_ }; |
@@ -433,11 +439,13 @@ int keepercall_get(lua_State* const L_) | |||
433 | lua_remove(_K, 1); // _K: KeyUD | 439 | lua_remove(_K, 1); // _K: KeyUD |
434 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | 440 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; |
435 | if (_key != nullptr) { | 441 | if (_key != nullptr) { |
436 | _key->peek(_K, _count); // _K: val... | 442 | _key->peek(_K, _count); // _K: N val... |
437 | } else { | 443 | } else { |
438 | // no fifo was ever registered for this key, or it is empty | 444 | // no fifo was ever registered for this key, or it is empty |
439 | lua_pop(_K, 1); // _K: | 445 | lua_pop(_K, 1); // _K: |
446 | lua_pushinteger(_K, 0); // _K: 0 | ||
440 | } | 447 | } |
448 | LUA_ASSERT(_K, lua_isnumber(_K, 1)); | ||
441 | return lua_gettop(_K); | 449 | return lua_gettop(_K); |
442 | } | 450 | } |
443 | 451 | ||
@@ -462,13 +470,10 @@ int keepercall_limit(lua_State* const L_) | |||
462 | } | 470 | } |
463 | // remove any clutter on the stack | 471 | // remove any clutter on the stack |
464 | lua_settop(_K, 0); // _K: | 472 | lua_settop(_K, 0); // _K: |
465 | if (_key->changeLimit(_limit)) { | 473 | // return true if we decide that blocked threads waiting to write on that key should be awakened |
466 | // return true if we decide that blocked threads waiting to write on that key should be awakened | 474 | // this is the case if we detect the key was full but it is no longer the case |
467 | // this is the case if we detect the key was full but it is no longer the case | 475 | lua_pushboolean(_K, _key->changeLimit(_limit) ? 1 : 0); // _K: bool |
468 | lua_pushboolean(_K, 1); // _K: true | 476 | return 1; |
469 | } | ||
470 | // return 0 or 1 value | ||
471 | return lua_gettop(_K); | ||
472 | } | 477 | } |
473 | 478 | ||
474 | // ################################################################################################# | 479 | // ################################################################################################# |
@@ -570,7 +575,7 @@ int keepercall_send(lua_State* const L_) | |||
570 | // ################################################################################################# | 575 | // ################################################################################################# |
571 | 576 | ||
572 | // in: linda key [val...] | 577 | // in: linda key [val...] |
573 | // out: true if the linda was full but it's no longer the case, else nothing | 578 | // out: true if the linda was full but it's no longer the case, else false |
574 | int keepercall_set(lua_State* const L_) | 579 | int keepercall_set(lua_State* const L_) |
575 | { | 580 | { |
576 | KeeperState const _K{ L_ }; | 581 | KeeperState const _K{ L_ }; |
@@ -593,7 +598,7 @@ int keepercall_set(lua_State* const L_) | |||
593 | lua_pushnil(_K); // _K: KeysDB key nil | 598 | lua_pushnil(_K); // _K: KeysDB key nil |
594 | lua_rawset(_K, -3); // _K: KeysDB | 599 | lua_rawset(_K, -3); // _K: KeysDB |
595 | } else { | 600 | } else { |
596 | lua_remove(_K, -2); // _K: KeysDB KeyUD | 601 | lua_remove(_K, -2); // KeyUD::reset expects KeyUD at the top // _K: KeysDB KeyUD |
597 | // we create room if the KeyUD was full but it is no longer the case | 602 | // we create room if the KeyUD was full but it is no longer the case |
598 | _should_wake_writers = _key->reset(_K); | 603 | _should_wake_writers = _key->reset(_K); |
599 | } | 604 | } |
@@ -619,7 +624,9 @@ int keepercall_set(lua_State* const L_) | |||
619 | lua_replace(_K, -2 - _count); // _K: KeysDB KeyUD val... | 624 | lua_replace(_K, -2 - _count); // _K: KeysDB KeyUD val... |
620 | [[maybe_unused]] bool const _pushed{ _key->push(_K, _count) }; // _K: KeysDB | 625 | [[maybe_unused]] bool const _pushed{ _key->push(_K, _count) }; // _K: KeysDB |
621 | } | 626 | } |
622 | return _should_wake_writers ? (lua_pushboolean(_K, 1), 1) : 0; | 627 | // stack isn't the same here depending on what we did before, but that's not a problem |
628 | lua_pushboolean(_K, _should_wake_writers ? 1 : 0); // _K: ... bool | ||
629 | return 1; | ||
623 | } | 630 | } |
624 | 631 | ||
625 | // ################################################################################################# | 632 | // ################################################################################################# |