aboutsummaryrefslogtreecommitdiff
path: root/src/linda.cpp
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-10-08 18:42:39 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-10-08 18:42:39 +0200
commit16b5070c0cd56e10c5074eb9903dbc3ae4e15a61 (patch)
treef6d5cdb74b505e13aa3261f7ab6192da0133b7b9 /src/linda.cpp
parente939e5e6a894a042d3301e47faa05264445f27f6 (diff)
downloadlanes-16b5070c0cd56e10c5074eb9903dbc3ae4e15a61.tar.gz
lanes-16b5070c0cd56e10c5074eb9903dbc3ae4e15a61.tar.bz2
lanes-16b5070c0cd56e10c5074eb9903dbc3ae4e15a61.zip
Sprinkling StackIndex all over the place
Diffstat (limited to 'src/linda.cpp')
-rw-r--r--src/linda.cpp52
1 files changed, 26 insertions, 26 deletions
diff --git a/src/linda.cpp b/src/linda.cpp
index 3f5fd33..bd40f5e 100644
--- a/src/linda.cpp
+++ b/src/linda.cpp
@@ -280,7 +280,7 @@ void Linda::setName(std::string_view const& name_)
280LUAG_FUNC(linda_cancel) 280LUAG_FUNC(linda_cancel)
281{ 281{
282 Linda* const _linda{ ToLinda<false>(L_, StackIndex{ 1 }) }; 282 Linda* const _linda{ ToLinda<false>(L_, StackIndex{ 1 }) };
283 std::string_view const _who{ luaG_optstring(L_, 2, "both") }; 283 std::string_view const _who{ luaG_optstring(L_, StackIndex{ 2 }, "both") };
284 // make sure we got 2 arguments: the linda and the cancellation mode 284 // make sure we got 2 arguments: the linda and the cancellation mode
285 luaL_argcheck(L_, lua_gettop(L_) <= 2, 2, "wrong number of arguments"); 285 luaL_argcheck(L_, lua_gettop(L_) <= 2, 2, "wrong number of arguments");
286 286
@@ -371,7 +371,7 @@ LUAG_FUNC(linda_count)
371 CheckKeyTypes(L_, StackIndex{ 2 }, StackIndex{ lua_gettop(L_) }); 371 CheckKeyTypes(L_, StackIndex{ 2 }, StackIndex{ lua_gettop(L_) });
372 372
373 Keeper* const _keeper{ _linda->whichKeeper() }; 373 Keeper* const _keeper{ _linda->whichKeeper() };
374 KeeperCallResult const _pushed{ keeper_call(_keeper->K, KEEPER_API(count), L_, _linda, 2) }; 374 KeeperCallResult const _pushed{ keeper_call(_keeper->K, KEEPER_API(count), L_, _linda, StackIndex{ 2 }) };
375 return OptionalValue(_pushed, L_, "tried to count an invalid key"); 375 return OptionalValue(_pushed, L_, "tried to count an invalid key");
376 } 376 }
377 }; 377 };
@@ -435,7 +435,7 @@ LUAG_FUNC(linda_get)
435 KeeperCallResult _pushed; 435 KeeperCallResult _pushed;
436 if (_linda->cancelRequest == CancelRequest::None) { 436 if (_linda->cancelRequest == CancelRequest::None) {
437 Keeper* const _keeper{ _linda->whichKeeper() }; 437 Keeper* const _keeper{ _linda->whichKeeper() };
438 _pushed = keeper_call(_keeper->K, KEEPER_API(get), L_, _linda, 2); 438 _pushed = keeper_call(_keeper->K, KEEPER_API(get), L_, _linda, StackIndex{ 2 });
439 } else { // linda is cancelled 439 } else { // linda is cancelled
440 // do nothing and return nil,lanes.cancel_error 440 // do nothing and return nil,lanes.cancel_error
441 lua_pushnil(L_); 441 lua_pushnil(L_);
@@ -468,10 +468,10 @@ LUAG_FUNC(linda_limit)
468 int const _nargs{ lua_gettop(L_) }; 468 int const _nargs{ lua_gettop(L_) };
469 luaL_argcheck(L_, _nargs == 2 || _nargs == 3, 2, "wrong number of arguments"); 469 luaL_argcheck(L_, _nargs == 2 || _nargs == 3, 2, "wrong number of arguments");
470 // make sure we got a numeric limit, or "unlimited", (or nothing) 470 // make sure we got a numeric limit, or "unlimited", (or nothing)
471 bool const _unlimited{ luaG_tostring(L_, 3) == "unlimited" }; 471 bool const _unlimited{ luaG_tostring(L_, StackIndex{ 3 }) == "unlimited" };
472 LindaLimit const _val{ _unlimited ? std::numeric_limits<LindaLimit::type>::max() : LindaLimit{ static_cast<LindaLimit::type>(luaL_optinteger(L_, 3, 0)) } }; 472 LindaLimit const _val{ _unlimited ? std::numeric_limits<LindaLimit::type>::max() : LindaLimit{ static_cast<LindaLimit::type>(luaL_optinteger(L_, 3, 0)) } };
473 if (_val < 0) { 473 if (_val < 0) {
474 raise_luaL_argerror(L_, 3, "limit must be >= 0"); 474 raise_luaL_argerror(L_, StackIndex{ 3 }, "limit must be >= 0");
475 } 475 }
476 // make sure the key is of a valid type 476 // make sure the key is of a valid type
477 CheckKeyTypes(L_, StackIndex{ 2 }, StackIndex{ 2 }); 477 CheckKeyTypes(L_, StackIndex{ 2 }, StackIndex{ 2 });
@@ -479,23 +479,23 @@ LUAG_FUNC(linda_limit)
479 KeeperCallResult _pushed; 479 KeeperCallResult _pushed;
480 if (_linda->cancelRequest == CancelRequest::None) { 480 if (_linda->cancelRequest == CancelRequest::None) {
481 if (_unlimited) { 481 if (_unlimited) {
482 LUA_ASSERT(L_, lua_gettop(L_) == 3 && luaG_tostring(L_, 3) == "unlimited"); 482 LUA_ASSERT(L_, lua_gettop(L_) == 3 && luaG_tostring(L_, StackIndex{ 3 }) == "unlimited");
483 // inside the Keeper, unlimited is signified with a -1 limit (can't use nil because of nil kNilSentinel conversions!) 483 // inside the Keeper, unlimited is signified with a -1 limit (can't use nil because of nil kNilSentinel conversions!)
484 lua_pop(L_, 1); // L_: linda key 484 lua_pop(L_, 1); // L_: linda key
485 lua_pushinteger(L_, -1); // L_: linda key nil 485 lua_pushinteger(L_, -1); // L_: linda key nil
486 } 486 }
487 Keeper* const _keeper{ _linda->whichKeeper() }; 487 Keeper* const _keeper{ _linda->whichKeeper() };
488 _pushed = keeper_call(_keeper->K, KEEPER_API(limit), L_, _linda, 2); 488 _pushed = keeper_call(_keeper->K, KEEPER_API(limit), L_, _linda, StackIndex{ 2 });
489 LUA_ASSERT(L_, _pushed.has_value() && (_pushed.value() == 2) && luaG_type(L_, -1) == LuaType::STRING); 489 LUA_ASSERT(L_, _pushed.has_value() && (_pushed.value() == 2) && luaG_type(L_, kIdxTop) == LuaType::STRING);
490 if (_nargs == 3) { // 3 args: setting the limit 490 if (_nargs == 3) { // 3 args: setting the limit
491 // changing the limit: no error, boolean value saying if we should wake blocked writer threads 491 // changing the limit: no error, boolean value saying if we should wake blocked writer threads
492 LUA_ASSERT(L_, luaG_type(L_, -2) == LuaType::BOOLEAN); // L_: bool string 492 LUA_ASSERT(L_, luaG_type(L_, StackIndex{ -2 }) == LuaType::BOOLEAN); // L_: bool string
493 if (lua_toboolean(L_, -2)) { 493 if (lua_toboolean(L_, -2)) {
494 _linda->readHappened.notify_all(); // To be done from within the 'K' locking area 494 _linda->readHappened.notify_all(); // To be done from within the 'K' locking area
495 } 495 }
496 } else { // 2 args: reading the limit 496 } else { // 2 args: reading the limit
497 // reading the limit: a number >=0 or "unlimited" 497 // reading the limit: a number >=0 or "unlimited"
498 LUA_ASSERT(L_, luaG_type(L_, -2) == LuaType::NUMBER || luaG_tostring(L_, -2) == "unlimited"); 498 LUA_ASSERT(L_, luaG_type(L_, StackIndex{ -2 }) == LuaType::NUMBER || luaG_tostring(L_, StackIndex{ -2 }) == "unlimited");
499 } 499 }
500 } else { // linda is cancelled 500 } else { // linda is cancelled
501 // do nothing and return nil,lanes.cancel_error 501 // do nothing and return nil,lanes.cancel_error
@@ -530,12 +530,12 @@ LUAG_FUNC(linda_receive)
530 StackIndex _key_i{ 2 }; // index of first key, if timeout not there 530 StackIndex _key_i{ 2 }; // index of first key, if timeout not there
531 531
532 std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; 532 std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() };
533 if (luaG_type(L_, 2) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion 533 if (luaG_type(L_, StackIndex{ 2 }) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion
534 lua_Duration const _duration{ lua_tonumber(L_, 2) }; 534 lua_Duration const _duration{ lua_tonumber(L_, 2) };
535 if (_duration.count() >= 0.0) { 535 if (_duration.count() >= 0.0) {
536 _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(_duration); 536 _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(_duration);
537 } else { 537 } else {
538 raise_luaL_argerror(L_, 2, "duration cannot be < 0"); 538 raise_luaL_argerror(L_, StackIndex{ 2 }, "duration cannot be < 0");
539 } 539 }
540 ++_key_i; 540 ++_key_i;
541 } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key 541 } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key
@@ -555,14 +555,14 @@ LUAG_FUNC(linda_receive)
555 // we expect a user-defined amount of return value 555 // we expect a user-defined amount of return value
556 _expected_pushed_min = (int) luaL_checkinteger(L_, _key_i + 1); 556 _expected_pushed_min = (int) luaL_checkinteger(L_, _key_i + 1);
557 if (_expected_pushed_min < 1) { 557 if (_expected_pushed_min < 1) {
558 raise_luaL_argerror(L_, _key_i + 1, "bad min count"); 558 raise_luaL_argerror(L_, StackIndex{ _key_i + 1 }, "bad min count");
559 } 559 }
560 _expected_pushed_max = (int) luaL_optinteger(L_, _key_i + 2, _expected_pushed_min); 560 _expected_pushed_max = (int) luaL_optinteger(L_, _key_i + 2, _expected_pushed_min);
561 // don't forget to count the key in addition to the values 561 // don't forget to count the key in addition to the values
562 ++_expected_pushed_min; 562 ++_expected_pushed_min;
563 ++_expected_pushed_max; 563 ++_expected_pushed_max;
564 if (_expected_pushed_min > _expected_pushed_max) { 564 if (_expected_pushed_min > _expected_pushed_max) {
565 raise_luaL_argerror(L_, _key_i + 2, "batched min/max error"); 565 raise_luaL_argerror(L_, StackIndex{ _key_i + 2 }, "batched min/max error");
566 } 566 }
567 } else { 567 } else {
568 // make sure the keys are of a valid type 568 // make sure the keys are of a valid type
@@ -686,12 +686,12 @@ LUAG_FUNC(linda_send)
686 StackIndex _key_i{ 2 }; // index of first key, if timeout not there 686 StackIndex _key_i{ 2 }; // index of first key, if timeout not there
687 687
688 std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; 688 std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() };
689 if (luaG_type(L_, 2) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion 689 if (luaG_type(L_, StackIndex{ 2 }) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion
690 lua_Duration const _duration{ lua_tonumber(L_, 2) }; 690 lua_Duration const _duration{ lua_tonumber(L_, 2) };
691 if (_duration.count() >= 0.0) { 691 if (_duration.count() >= 0.0) {
692 _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(_duration); 692 _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(_duration);
693 } else { 693 } else {
694 raise_luaL_argerror(L_, 2, "duration cannot be < 0"); 694 raise_luaL_argerror(L_, StackIndex{ 2 }, "duration cannot be < 0");
695 } 695 }
696 ++_key_i; 696 ++_key_i;
697 } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key 697 } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key
@@ -828,9 +828,9 @@ LUAG_FUNC(linda_set)
828 KeeperCallResult _pushed; 828 KeeperCallResult _pushed;
829 if (_linda->cancelRequest == CancelRequest::None) { 829 if (_linda->cancelRequest == CancelRequest::None) {
830 Keeper* const _keeper{ _linda->whichKeeper() }; 830 Keeper* const _keeper{ _linda->whichKeeper() };
831 _pushed = keeper_call(_keeper->K, KEEPER_API(set), L_, _linda, 2); 831 _pushed = keeper_call(_keeper->K, KEEPER_API(set), L_, _linda, StackIndex{ 2 });
832 if (_pushed.has_value()) { // no error? 832 if (_pushed.has_value()) { // no error?
833 LUA_ASSERT(L_, _pushed.value() == 2 && luaG_type(L_, -1) == LuaType::STRING && luaG_type(L_, -2) == LuaType::BOOLEAN); 833 LUA_ASSERT(L_, _pushed.value() == 2 && luaG_type(L_, kIdxTop) == LuaType::STRING && luaG_type(L_, StackIndex{ -2 }) == LuaType::BOOLEAN);
834 834
835 if (_has_data) { 835 if (_has_data) {
836 // we put some data in the slot, tell readers that they should wake 836 // we put some data in the slot, tell readers that they should wake
@@ -922,13 +922,13 @@ namespace {
922 */ 922 */
923LUAG_FUNC(linda) 923LUAG_FUNC(linda)
924{ 924{
925 static constexpr int kLastArg{ LUA_VERSION_NUM >= 504 ? 3 : 2}; 925 static constexpr StackIndex kLastArg{ LUA_VERSION_NUM >= 504 ? 3 : 2 };
926 int const _top{ lua_gettop(L_) }; 926 StackIndex const _top{ lua_gettop(L_) };
927 luaL_argcheck(L_, _top <= kLastArg, _top, "too many arguments"); 927 luaL_argcheck(L_, _top <= kLastArg, _top, "too many arguments");
928 int _closeHandlerIdx{}; 928 StackIndex _closeHandlerIdx{};
929 int _nameIdx{}; 929 StackIndex _nameIdx{};
930 int _groupIdx{}; 930 StackIndex _groupIdx{};
931 for (int const _i : std::ranges::iota_view{ 1, _top + 1 }) { 931 for (StackIndex const _i : std::ranges::iota_view{ StackIndex{ 1 }, StackIndex{ _top + 1 }}) {
932 switch (luaG_type(L_, _i)) { 932 switch (luaG_type(L_, _i)) {
933#if LUA_VERSION_NUM >= 504 // to-be-closed support starts with Lua 5.4 933#if LUA_VERSION_NUM >= 504 // to-be-closed support starts with Lua 5.4
934 case LuaType::FUNCTION: 934 case LuaType::FUNCTION:
@@ -985,11 +985,11 @@ LUAG_FUNC(linda)
985 LindaFactory::Instance.pushDeepUserdata(DestState{ L_ }, _nuv); // L_: name group close_handler linda 985 LindaFactory::Instance.pushDeepUserdata(DestState{ L_ }, _nuv); // L_: name group close_handler linda
986 if (_closeHandlerIdx != 0) { 986 if (_closeHandlerIdx != 0) {
987 lua_replace(L_, 2); // L_: name linda close_handler 987 lua_replace(L_, 2); // L_: name linda close_handler
988 lua_setiuservalue(L_, 2, 1); // L_: name linda 988 lua_setiuservalue(L_, StackIndex{ 2 }, 1); // L_: name linda
989 } 989 }
990 // depending on whether we have a handler or not, the stack is not in the same state at this point 990 // depending on whether we have a handler or not, the stack is not in the same state at this point
991 // just make sure we have our Linda at the top 991 // just make sure we have our Linda at the top
992 LUA_ASSERT(L_, ToLinda<true>(L_, StackIndex{ -1 })); 992 LUA_ASSERT(L_, ToLinda<true>(L_, kIdxTop));
993 return 1; 993 return 1;
994 } else { // no to-be-closed support 994 } else { // no to-be-closed support
995 // ensure we have name, group in that order on the stack 995 // ensure we have name, group in that order on the stack