diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-14 17:29:39 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-14 17:29:39 +0200 |
| commit | f9f4c841e77ea36797a2ea06cd8859581dfbef94 (patch) | |
| tree | 947223403341ed2fe3fbf1fcde7a0d43f6647090 /src/keeper.cpp | |
| parent | 53eef99942ecca7b1dc43ec6b35d09a5fe6b1b1b (diff) | |
| download | lanes-f9f4c841e77ea36797a2ea06cd8859581dfbef94.tar.gz lanes-f9f4c841e77ea36797a2ea06cd8859581dfbef94.tar.bz2 lanes-f9f4c841e77ea36797a2ea06cd8859581dfbef94.zip | |
Shuffling code around
Diffstat (limited to 'src/keeper.cpp')
| -rw-r--r-- | src/keeper.cpp | 147 |
1 files changed, 7 insertions, 140 deletions
diff --git a/src/keeper.cpp b/src/keeper.cpp index dcfa2ec..dcfb431 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp | |||
| @@ -545,146 +545,6 @@ int keepercall_count(lua_State* L_) | |||
| 545 | // Keeper API, accessed from linda methods | 545 | // Keeper API, accessed from linda methods |
| 546 | // ################################################################################################# | 546 | // ################################################################################################# |
| 547 | 547 | ||
| 548 | /* | ||
| 549 | * Pool of keeper states | ||
| 550 | * | ||
| 551 | * Access to keeper states is locked (only one OS thread at a time) so the | ||
| 552 | * bigger the pool, the less chances of unnecessary waits. Lindas map to the | ||
| 553 | * keepers randomly, by a hash. | ||
| 554 | */ | ||
| 555 | |||
| 556 | // called as __gc for the keepers array userdata | ||
| 557 | void close_keepers(Universe* U_) | ||
| 558 | { | ||
| 559 | if (U_->keepers != nullptr) { | ||
| 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 | ||
| 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 | ||
| 564 | // which is early-outed with a U->keepers->nbKeepers null-check | ||
| 565 | U_->keepers->nb_keepers = 0; | ||
| 566 | for (int _i = 0; _i < _nbKeepers; ++_i) { | ||
| 567 | lua_State* const K{ U_->keepers->keeper_array[_i].L }; | ||
| 568 | U_->keepers->keeper_array[_i].L = KeeperState{ nullptr }; | ||
| 569 | if (K != nullptr) { | ||
| 570 | lua_close(K); | ||
| 571 | } else { | ||
| 572 | // detected partial init: destroy only the mutexes that got initialized properly | ||
| 573 | _nbKeepers = _i; | ||
| 574 | } | ||
| 575 | } | ||
| 576 | for (int _i = 0; _i < _nbKeepers; ++_i) { | ||
| 577 | U_->keepers->keeper_array[_i].~Keeper(); | ||
| 578 | } | ||
| 579 | // free the keeper bookkeeping structure | ||
| 580 | U_->internalAllocator.free(U_->keepers, sizeof(Keepers) + (_nbKeepers - 1) * sizeof(Keeper)); | ||
| 581 | U_->keepers = nullptr; | ||
| 582 | } | ||
| 583 | } | ||
| 584 | |||
| 585 | // ################################################################################################# | ||
| 586 | |||
| 587 | /* | ||
| 588 | * Initialize keeper states | ||
| 589 | * | ||
| 590 | * If there is a problem, returns nullptr and pushes the error message on the stack | ||
| 591 | * else returns the keepers bookkeeping structure. | ||
| 592 | * | ||
| 593 | * Note: Any problems would be design flaws; the created Lua state is left | ||
| 594 | * unclosed, because it does not really matter. In production code, this | ||
| 595 | * function never fails. | ||
| 596 | * settings table is expected at position 1 on the stack | ||
| 597 | */ | ||
| 598 | void init_keepers(Universe* U_, lua_State* L_) | ||
| 599 | { | ||
| 600 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); | ||
| 601 | STACK_CHECK_START_REL(L_, 0); // L_: settings | ||
| 602 | lua_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers | ||
| 603 | int const _nb_keepers{ static_cast<int>(lua_tointeger(L_, -1)) }; | ||
| 604 | lua_pop(L_, 1); // L_: settings | ||
| 605 | if (_nb_keepers < 1) { | ||
| 606 | raise_luaL_error(L_, "Bad number of keepers (%d)", _nb_keepers); | ||
| 607 | } | ||
| 608 | STACK_CHECK(L_, 0); | ||
| 609 | |||
| 610 | lua_getfield(L_, 1, "keepers_gc_threshold"); // L_: settings keepers_gc_threshold | ||
| 611 | int const keepers_gc_threshold{ static_cast<int>(lua_tointeger(L_, -1)) }; | ||
| 612 | lua_pop(L_, 1); // L_: settings | ||
| 613 | STACK_CHECK(L_, 0); | ||
| 614 | |||
| 615 | // Keepers contains an array of 1 Keeper, adjust for the actual number of keeper states | ||
| 616 | { | ||
| 617 | size_t const bytes = sizeof(Keepers) + (_nb_keepers - 1) * sizeof(Keeper); | ||
| 618 | U_->keepers = static_cast<Keepers*>(U_->internalAllocator.alloc(bytes)); | ||
| 619 | if (U_->keepers == nullptr) { | ||
| 620 | raise_luaL_error(L_, "init_keepers() failed while creating keeper array; out of memory"); | ||
| 621 | } | ||
| 622 | U_->keepers->Keepers::Keepers(); | ||
| 623 | U_->keepers->gc_threshold = keepers_gc_threshold; | ||
| 624 | U_->keepers->nb_keepers = _nb_keepers; | ||
| 625 | |||
| 626 | for (int _i = 0; _i < _nb_keepers; ++_i) { | ||
| 627 | U_->keepers->keeper_array[_i].Keeper::Keeper(); | ||
| 628 | } | ||
| 629 | } | ||
| 630 | for (int _i = 0; _i < _nb_keepers; ++_i) { | ||
| 631 | // note that we will leak K if we raise an error later | ||
| 632 | KeeperState const _K{ create_state(U_, L_) }; // L_: settings K: | ||
| 633 | if (_K == nullptr) { | ||
| 634 | raise_luaL_error(L_, "init_keepers() failed while creating keeper states; out of memory"); | ||
| 635 | } | ||
| 636 | |||
| 637 | U_->keepers->keeper_array[_i].L = _K; | ||
| 638 | |||
| 639 | if (U_->keepers->gc_threshold >= 0) { | ||
| 640 | lua_gc(_K, LUA_GCSTOP, 0); | ||
| 641 | } | ||
| 642 | |||
| 643 | STACK_CHECK_START_ABS(_K, 0); | ||
| 644 | |||
| 645 | // copy the universe pointer in the keeper itself | ||
| 646 | universe_store(_K, U_); | ||
| 647 | STACK_CHECK(_K, 0); | ||
| 648 | |||
| 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 | ||
| 651 | luaL_requiref(_K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package | ||
| 652 | lua_pop(_K, 1); // L_: settings K: | ||
| 653 | STACK_CHECK(_K, 0); | ||
| 654 | serialize_require(DEBUGSPEW_PARAM_COMMA(U_) _K); | ||
| 655 | STACK_CHECK(_K, 0); | ||
| 656 | |||
| 657 | // copy package.path and package.cpath from the source state | ||
| 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 | ||
| 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: | ||
| 662 | // if something went wrong, the error message is at the top of the stack | ||
| 663 | lua_remove(L_, -2); // L_: settings error_msg | ||
| 664 | raise_lua_error(L_); | ||
| 665 | } | ||
| 666 | } | ||
| 667 | lua_pop(L_, 1); // L_: settings K: | ||
| 668 | STACK_CHECK(L_, 0); | ||
| 669 | STACK_CHECK(_K, 0); | ||
| 670 | |||
| 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) | ||
| 673 | // will raise an error in L_ in case of problem | ||
| 674 | callOnStateCreate(U_, _K, L_, LookupMode::ToKeeper); | ||
| 675 | |||
| 676 | // to see VM name in Decoda debugger | ||
| 677 | lua_pushfstring(_K, "Keeper #%d", _i + 1); // L_: settings K: "Keeper #n" | ||
| 678 | lua_setglobal(_K, "decoda_name"); // L_: settings K: | ||
| 679 | // create the fifos table in the keeper state | ||
| 680 | kFifosRegKey.setValue(_K, [](lua_State* L_) { lua_newtable(L_); }); // L_: settings K: | ||
| 681 | STACK_CHECK(_K, 0); | ||
| 682 | } | ||
| 683 | STACK_CHECK(L_, 0); | ||
| 684 | } | ||
| 685 | |||
| 686 | // ################################################################################################# | ||
| 687 | |||
| 688 | Keeper* Linda::acquireKeeper() const | 548 | Keeper* Linda::acquireKeeper() const |
| 689 | { | 549 | { |
| 690 | int const _nbKeepers{ U->keepers->nb_keepers }; | 550 | int const _nbKeepers{ U->keepers->nb_keepers }; |
| @@ -789,3 +649,10 @@ KeeperCallResult keeper_call(KeeperState K_, keeper_api_t func_, lua_State* L_, | |||
| 789 | 649 | ||
| 790 | return _result; | 650 | return _result; |
| 791 | } | 651 | } |
| 652 | |||
| 653 | // ################################################################################################# | ||
| 654 | |||
| 655 | void Keepers::CreateFifosTable(lua_State* L_) | ||
| 656 | { | ||
| 657 | kFifosRegKey.setValue(L_, [](lua_State* L_) { lua_newtable(L_); }); | ||
| 658 | } | ||
