/*
 * LANES.CPP                            Copyright (c) 2007-08, Asko Kauppi
 *                                      Copyright (C) 2009-24, Benoit Germain
 *
 * Multithreading in Lua.
 *
 * History:
 *      See CHANGES
 *
 * Platforms (tested internally):
 *      OS X (10.5.7 PowerPC/Intel)
 *      Linux x86 (Ubuntu 8.04)
 *      Win32 (Windows XP Home SP2, Visual C++ 2005/2008 Express)
 *
 * Platforms (tested externally):
 *      Win32 (MSYS) by Ross Berteig.
 *
 * Platforms (testers appreciated):
 *      Win64 - should work???
 *      Linux x64 - should work
 *      FreeBSD - should work
 *      QNX - porting shouldn't be hard
 *      Sun Solaris - porting shouldn't be hard
 *
 * References:
 *      "Porting multithreaded applications from Win32 to Mac OS X":
 *      <http://developer.apple.com/macosx/multithreadedprogramming.html>
 *
 *      Pthreads:
 *      <http://vergil.chemistry.gatech.edu/resources/programming/threads.html>
 *
 *      MSDN: <http://msdn2.microsoft.com/en-us/library/ms686679.aspx>
 *
 *      <http://ridiculousfish.com/blog/archives/2007/02/17/barrier>
 *
 * Defines:
 *      -DLINUX_SCHED_RR: all threads are lifted to SCHED_RR category, to
 *          allow negative priorities [-3,-1] be used. Even without this,
 *          using priorities will require 'sudo' privileges on Linux.
 *
 *      -DUSE_PTHREAD_TIMEDJOIN: use 'pthread_timedjoin_np()' for waiting
 *          for threads with a timeout. This changes the thread cleanup
 *          mechanism slightly (cleans up at the join, not once the thread
 *          has finished). May or may not be a good idea to use it.
 *          Available only in selected operating systems (Linux).
 *
 * Bugs:
 *
 * To-do:
 *
 * Make waiting threads cancellable.
 *      ...
 */

/*
===============================================================================

Copyright (C) 2007-10 Asko Kauppi <akauppi@gmail.com>
              2011-24 Benoit Germain <bnt.germain@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

===============================================================================
*/

#include "_pch.hpp"
#include "lanes.hpp"

#include "deep.hpp"
#include "intercopycontext.hpp"
#include "keeper.hpp"
#include "lane.hpp"
#include "linda.hpp"
#include "nameof.hpp"
#include "state.hpp"
#include "threading.hpp"
#include "tools.hpp"

#if !(defined(PLATFORM_XBOX) || defined(PLATFORM_WIN32) || defined(PLATFORM_POCKETPC))
#include <sys/time.h>
#endif

/* geteuid() */
#ifdef PLATFORM_LINUX
#include <unistd.h>
#include <sys/types.h>
#endif

// #################################################################################################
// ########################################### Threads #############################################
// #################################################################################################

//---
// = _single( [cores_uint=1] )
//
// Limits the process to use only 'cores' CPU cores. To be used for performance
// testing on multicore devices. DEBUGGING ONLY!
//
LUAG_FUNC(set_singlethreaded)
{
    [[maybe_unused]] lua_Integer const _cores{ luaL_optinteger(L_, 1, 1) };

#ifdef PLATFORM_OSX
#ifdef _UTILBINDTHREADTOCPU
    if (_cores > 1) {
        raise_luaL_error(L_, "Limiting to N>1 cores not possible");
    }
    // requires 'chudInitialize()'
    utilBindThreadToCPU(0); // # of CPU to run on (we cannot limit to 2..N CPUs?)
    return 0;
#else
    raise_luaL_error(L_, "Not available: compile with _UTILBINDTHREADTOCPU");
#endif
#else
    raise_luaL_error(L_, "not implemented");
#endif
}

// #################################################################################################

LUAG_FUNC(set_thread_priority)
{
    lua_Integer const _prio{ luaL_checkinteger(L_, 1) };
    NativePrioFlag const _native{ std::string_view{ "native" } == luaL_optstring(L_, 2, "mapped") };
    // public Lanes API accepts a generic range -3/+3
    // that will be remapped into the platform-specific scheduler priority scheme
    // On some platforms, -3 is equivalent to -2 and +3 to +2
    if (!_native && (_prio < kThreadPrioMin || _prio > kThreadPrioMax)) {
        raise_luaL_error(L_, "priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, _prio);
    }

    THREAD_SET_PRIORITY(L_, static_cast<int>(_prio), _native, Universe::Get(L_)->sudo);
    return 0;
}

// #################################################################################################

LUAG_FUNC(set_thread_affinity)
{
    lua_Integer const _affinity{ luaL_checkinteger(L_, 1) };
    if (_affinity <= 0) {
        raise_luaL_error(L_, "invalid affinity (%d)", _affinity);
    }

    THREAD_SET_AFFINITY(L_, static_cast<unsigned int>(_affinity));
    return 0;
}

// #################################################################################################

LUAG_FUNC(sleep)
{
    extern LUAG_FUNC(linda_receive);

    Universe* const _U{ Universe::Get(L_) };
    lua_settop(L_, 1);                                                                             // L_: duration?
    lua_pushcfunction(L_, LG_linda_receive);                                                       // L_: duration? receive()
    STACK_CHECK_START_REL(L_, 0); // we pushed the function we intend to call, now prepare the arguments
    _U->timerLinda->push(L_);                                                                      // L_: duration? receive() timerLinda
    if (lua_isnumber(L_, 1)) {
        auto const _n{ lua_tonumber(L_, 1) };
        if (_n < 0) {
            raise_luaL_argerror(L_, StackIndex{ 1 }, "duration must be >= 0");
        }
        lua_pushvalue(L_, StackIndex{ 1 });                                                        // L_: duration? receive() timerLinda timeout
    } else if (lua_isnoneornil(L_, 1)) {
        lua_pushnil(L_);                                                                           // L_: duration? receive() timerLinda timeout
    } else {
        raise_luaL_argerror(L_, StackIndex{ 1 }, "duration must be a number");
    }

    luaW_pushstring(L_, "ac100de1-a696-4619-b2f0-a26de9d58ab8");                                   // L_: duration? receive() timerLinda timeout key
    STACK_CHECK(L_, 3); // 3 arguments ready
    lua_call(L_, 3, LUA_MULTRET); // timerLinda:receive(duration,key)                              // L_: duration? result...
    return lua_gettop(L_) - 1;
}

// #################################################################################################

// --- If a client wants to transfer stuff of a given module from the current state to another Lane, the module must be required
// with lanes.require, that will call the regular 'require', then populate the lookup database in the source lane
// module = lanes.require( "modname")
// upvalue[1]: _G.require
LUAG_FUNC(require)
{
    std::string_view const _name{ luaW_tostring(L_, StackIndex{ 1 }) };                            // L_: "name" ...
    int const _nargs{ lua_gettop(L_) };
    DEBUGSPEW_CODE(Universe * _U{ Universe::Get(L_) });
    STACK_CHECK_START_REL(L_, 0);
    DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.require '" << _name << "' BEGIN" << std::endl);
    DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
    lua_pushvalue(L_, lua_upvalueindex(1));                                                        // L_: "name" ... require
    lua_insert(L_, 1);                                                                             // L_: require "name" ...
    lua_call(L_, _nargs, 1);                                                                       // L_: module
    tools::PopulateFuncLookupTable(L_, kIdxTop, _name);
    DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.require '" << _name << "' END" << std::endl);
    STACK_CHECK(L_, 0);
    return 1;
}

// #################################################################################################

// --- If a client wants to transfer stuff of a previously required module from the current state to another Lane, the module must be registered
// to populate the lookup database in the source lane (and in the destination too, of course)
// lanes.register( "modname", module)
int lanes_register(lua_State* const L_)
{
    Universe* const _U{ Universe::Get(L_) };
    if (!_U) {
        raise_luaL_error(L_, "Lanes is not ready");
    }
    std::string_view const _name{ luaW_checkstring(L_, StackIndex{ 1 }) };
    LuaType const _mod_type{ luaW_type(L_, StackIndex{ 2 }) };
    // ignore extra arguments, just in case
    lua_settop(L_, 2);
    luaL_argcheck(L_, (_mod_type == LuaType::TABLE) || (_mod_type == LuaType::FUNCTION), 2, "unexpected module type");
    STACK_CHECK_START_REL(L_, 0); // "name" mod_table
    DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.register '" << _name << "' BEGIN" << std::endl);
    DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
    tools::PopulateFuncLookupTable(L_, kIdxTop, _name);
    DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.register '" << _name << "' END" << std::endl);
    STACK_CHECK(L_, 0);
    return 0;
}

// #################################################################################################

LUAG_FUNC(thread_priority_range)
{
    NativePrioFlag const _native{ std::string_view{ "native" } == luaL_optstring(L_, 1, "mapped") };
    if (_native) {
        auto const [_prio_min, _prio_max] = THREAD_NATIVE_PRIOS();
        lua_pushinteger(L_, _prio_min);
        lua_pushinteger(L_, _prio_max);
    } else {
        lua_pushinteger(L_, kThreadPrioMin);
        lua_pushinteger(L_, kThreadPrioMax);
    }
    return 2;
}

// #################################################################################################

//--- [] means can be nil
// lane_ud = lane_new( function
//                   , [libs_str]
//                   , [prio_is_native_bool]
//                   , [priority_int]
//                   , [globals_tbl]
//                   , [package_tbl]
//                   , [required_tbl]
//                   , [gc_cb_func]
//                   , [name]
//                   , error_trace_level
//                   , as_coroutine
//                  [, ... args ...])
//
// Upvalues: metatable to use for 'lane_ud'
//
LUAG_FUNC(lane_new)
{
    static constexpr StackIndex kFuncIdx{ 1 };
    static constexpr StackIndex kLibsIdx{ 2 };
    static constexpr StackIndex kPrinIdx{ 3 };
    static constexpr StackIndex kPrioIdx{ 4 };
    static constexpr StackIndex kGlobIdx{ 5 };
    static constexpr StackIndex kPackIdx{ 6 };
    static constexpr StackIndex kRequIdx{ 7 };
    static constexpr StackIndex kGcCbIdx{ 8 };
    static constexpr StackIndex kNameIdx{ 9 };
    static constexpr StackIndex kErTlIdx{ 10 };
    static constexpr StackIndex kAsCoro{ 11 };
    static constexpr StackIndex kFixedArgsIdx{ 11 };

    int const _nargs{ lua_gettop(L_) - kFixedArgsIdx };
    LUA_ASSERT(L_, _nargs >= 0);

    Universe* const _U{ Universe::Get(L_) };
    DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: setup" << std::endl);

    std::optional<std::string_view> _libs_str{ lua_isnil(L_, kLibsIdx) ? std::nullopt : std::make_optional(luaW_tostring(L_, kLibsIdx)) };
    lua_State* const _S{ state::NewLaneState(_U, SourceState{ L_ }, _libs_str) };                 // L_: [fixed] ...                                L2:
    STACK_CHECK_START_REL(_S, 0);

    // 'lane' is allocated from heap, not Lua, since its life span may surpass the handle's (if free running thread)
    Lane::ErrorTraceLevel const _errorTraceLevel{ static_cast<Lane::ErrorTraceLevel>(lua_tointeger(L_, kErTlIdx)) };
    bool const _asCoroutine{ lua_toboolean(L_, kAsCoro) ? true : false };
    Lane* const _lane{ new (_U) Lane{ _U, _S, _errorTraceLevel, _asCoroutine } };
    STACK_CHECK(_S, _asCoroutine ? 1 : 0); // the Lane's thread is on the Lane's state stack
    lua_State* const _L2{ _lane->L };
    STACK_CHECK_START_REL(_L2, 0);
    if (_lane == nullptr) {
        raise_luaL_error(L_, "could not create lane: out of memory");
    }

    class OnExit final
    {
        private:
        lua_State* const L;
        Lane* lane{ nullptr };
        DEBUGSPEW_CODE(DebugSpewIndentScope scope);

        public:
        OnExit(lua_State* L_, Lane* lane_)
        : L{ L_ }
        , lane{ lane_ }
        DEBUGSPEW_COMMA_PARAM(scope{ lane_->U })
        {
        }

        ~OnExit()
        {
            if (lane) {
                STACK_CHECK_START_REL(L, 0);
                // we still need a full userdata so that garbage collection can do its thing
                prepareUserData();
                // remove it immediately from the stack so that the error that landed us here is at the top
                lua_pop(L, 1);
                STACK_CHECK(L, 0);
                // leave a single cancel_error on the stack for the caller
                lua_settop(lane->L, 0);
                kCancelError.pushKey(lane->L);
                {
                    std::lock_guard _guard{ lane->doneMutex };
                    // this will cause lane_main to skip actual running (because we are not Pending anymore)
                    lane->status.store(Lane::Running, std::memory_order_release);
                }
                // unblock the thread so that it can terminate gracefully
#ifndef __PROSPERO__
                lane->ready.count_down();
#else // __PROSPERO__
                lane->ready.test_and_set();
#endif // __PROSPERO__
            }
        }

        private:
        void prepareUserData()
        {
            DEBUGSPEW_CODE(DebugSpew(lane->U) << "lane_new: preparing lane userdata" << std::endl);
            STACK_CHECK_START_REL(L, 0);
            // a Lane full userdata needs a single uservalue
            Lane** const _ud{ luaW_newuserdatauv<Lane*>(L, UserValueCount{ 1 }) };                 // L: ... lane
            *_ud = lane; // don't forget to store the pointer in the userdata!

            // Set metatable for the userdata
            lua_pushvalue(L, lua_upvalueindex(1));                                                 // L: ... lane mt
            lua_setmetatable(L, -2);                                                               // L: ... lane
            STACK_CHECK(L, 1);

            // Create uservalue for the userdata. There can be only one that must be a table, due to Lua 5.1 compatibility.
            // (this is where lane body return values will be stored when the handle is indexed by a numeric key)
            lua_newtable(L);                                                                       // L: ... lane {uv}

            // Store the gc_cb callback in the uservalue
            StackIndex const _gc_cb_idx{ lua_isnoneornil(L, kGcCbIdx) ? kIdxNone : kGcCbIdx };
            if (_gc_cb_idx > 0) {
                kLaneGC.pushKey(L);                                                                // L: ... lane {uv} k
                lua_pushvalue(L, _gc_cb_idx);                                                      // L: ... lane {uv} k gc_cb
                lua_rawset(L, -3);                                                                 // L: ... lane {uv}
            }
            STACK_CHECK(L, 2);
            // store the uservalue in the Lane full userdata
            lua_setiuservalue(L, StackIndex{ -2 }, UserValueIndex{ 1 });                           // L: ... lane

            StackIndex const _name_idx{ lua_isnoneornil(L, kNameIdx) ? kIdxNone : kNameIdx };
            std::string_view _debugName{ (_name_idx > 0) ? luaW_tostring(L, _name_idx) : std::string_view{} };
            if (!_debugName.empty())
            {
                if (_debugName == "auto") {
                    if (luaW_type(L, kFuncIdx) == LuaType::STRING) {
                        lua_Debug _ar;
                        if (lua_getstack(L, 2, &_ar) == 0) { // 0 is here, 1 is lanes.gen, 2 is its caller
                            lua_getstack(L, 1, &_ar); // level 2 may not exist with LuaJIT, try again with level 1
                        }
                        lua_getinfo(L, "Sl", &_ar);
                        luaW_pushstring(L, "%s:%d", _ar.short_src, _ar.currentline);               // L: ... lane "<name>"
                    } else {
                        lua_Debug _ar;
                        lua_pushvalue(L, kFuncIdx);                                                // L: ... lane func
                        lua_getinfo(L, ">S", &_ar);                                                // L: ... lane
                        luaW_pushstring(L, "%s:%d", _ar.short_src, _ar.linedefined);               // L: ... lane "<name>"
                    }
                    lua_replace(L, _name_idx);                                                     // L: ... lane
                    _debugName = luaW_tostring(L, _name_idx);
                }
                lane->storeDebugName(_debugName);
            }
            STACK_CHECK(L, 1);
        }

        public:
        void success()
        {
            prepareUserData();
            // unblock the thread so that it can terminate gracefully
#ifndef __PROSPERO__
            lane->ready.count_down();
#else // __PROSPERO__
            lane->ready.test_and_set();
#endif // __PROSPERO__
            lane = nullptr;
        }
    } _onExit{ L_, _lane};
    // launch the thread early, it will sync with a std::latch to parallelize OS thread warmup and L2 preparation
    DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: launching thread" << std::endl);
    // public Lanes API accepts a generic range -3/+3
    // that will be remapped into the platform-specific scheduler priority scheme
    // On some platforms, -3 is equivalent to -2 and +3 to +2
    auto const [_priority, _native] {
        std::invoke([L = L_]() {
            NativePrioFlag const _native{ static_cast<bool>(lua_toboolean(L, kPrinIdx)) };
            StackIndex const _prio_idx{ lua_isnoneornil(L, kPrioIdx) ? kIdxNone : kPrioIdx };
            if (_prio_idx == kIdxNone) {
                return std::make_pair(kThreadPrioDefault, _native);
            }
            int const _priority{ static_cast<int>(lua_tointeger(L, _prio_idx)) };
            if (!_native && (_priority < kThreadPrioMin || _priority > kThreadPrioMax)) {
                raise_luaL_error(L, "Priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, _priority);
            }
            return std::make_pair(_priority, _native);
        })
    };

    _lane->startThread(L_, _priority, _native);

    STACK_GROW(_L2, _nargs + 3);
    STACK_GROW(L_, 3);
    STACK_CHECK_START_REL(L_, 0);

    // package
    StackIndex const _package_idx{ lua_isnoneornil(L_, kPackIdx) ? kIdxNone : kPackIdx };
    if (_package_idx != 0) {
        DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: update 'package'" << std::endl);
        // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack
        InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, SourceIndex{ _package_idx }, {}, {}, {} };
        [[maybe_unused]] InterCopyResult const _ret{ _c.interCopyPackage() };
        LUA_ASSERT(L_, _ret == InterCopyResult::Success); // either all went well, or we should not even get here
    }
    STACK_CHECK(L_, 0);
    STACK_CHECK(_L2, 0);

    // modules to require in the target lane *before* the function is transfered!
    StackIndex const _required_idx{ lua_isnoneornil(L_, kRequIdx) ? kIdxNone : kRequIdx };
    if (_required_idx != 0) {
        int _nbRequired{ 1 };
        DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: process 'required' list" << std::endl);
        DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
        // should not happen, was checked in lanes.lua before calling lane_new()
        if (luaW_type(L_, _required_idx) != LuaType::TABLE) {
            raise_luaL_error(L_, "expected required module list as a table, got %s", luaL_typename(L_, _required_idx));
        }

        lua_pushnil(L_);                                                                           // L_: [fixed] args... nil                        L2:
        while (lua_next(L_, _required_idx) != 0) {                                                 // L_: [fixed] args... n "modname"                L2:
            if (luaW_type(L_, kIdxTop) != LuaType::STRING || luaW_type(L_, StackIndex{ -2 }) != LuaType::NUMBER || lua_tonumber(L_, -2) != _nbRequired) {
                raise_luaL_error(L_, "required module list should be a list of strings");
            } else {
                // require the module in the target state, and populate the lookup table there too
                std::string_view const _name{ luaW_tostring(L_, kIdxTop) };
                DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: require '" << _name << "'" << std::endl);

                // require the module in the target lane
                lua_getglobal(_L2, "require");                                                     // L_: [fixed] args... n "modname"                L2: require()?
                if (lua_isnil(_L2, -1)) {
                    lua_pop(_L2, 1);                                                               // L_: [fixed] args... n "modname"                L2:
                    raise_luaL_error(L_, "cannot pre-require modules without loading 'package' library first");
                } else {
                    luaW_pushstring(_L2, _name);                                                   // L_: [fixed] args... n "modname"                L2: require() name
                    LuaError const _rc{ lua_pcall(_L2, 1, 1, 0) };                                 // L_: [fixed] args... n "modname"                L2: ret/errcode
                    if (_rc != LuaError::OK) {
                        // propagate error to main state if any
                        InterCopyContext _c{ _U, DestState{ L_ }, SourceState{ _L2 }, {}, {}, {}, {}, {} };
                        std::ignore = _c.interMove(1);                                             // L_: [fixed] args... n "modname" error          L2:
                        raise_lua_error(L_);
                    }
                    // here the module was successfully required                                   // L_: [fixed] args... n "modname"                L2: ret
                    // after requiring the module, register the functions it exported in our name<->function database
                    tools::PopulateFuncLookupTable(_L2, kIdxTop, _name);
                    lua_pop(_L2, 1);                                                               // L_: [fixed] args... n "modname"                L2:
                }
            }
            lua_pop(L_, 1);                                                                        // L_: [fixed] args... n                          L2:
            ++_nbRequired;
        }                                                                                          // L_: [fixed] args...
    }
    STACK_CHECK(L_, 0);
    STACK_CHECK(_L2, 0);                                                                           // L_: [fixed] args...                            L2:

    // Appending the specified globals to the global environment
    // *after* stdlibs have been loaded and modules required, in case we transfer references to native functions they exposed...
    //
    StackIndex const _globals_idx{ lua_isnoneornil(L_, kGlobIdx) ? kIdxNone : kGlobIdx };
    if (_globals_idx != 0) {
        DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer globals" << std::endl);
        if (!lua_istable(L_, _globals_idx)) {
            raise_luaL_error(L_, "Expected table, got %s", luaL_typename(L_, _globals_idx));
        }

        DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
        lua_pushnil(L_);                                                                           // L_: [fixed] args... nil                        L2:
        // Lua 5.2 wants us to push the globals table on the stack
        InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} };
        luaW_pushglobaltable(_L2);                                                                 // L_: [fixed] args... nil                        L2: _G
        while (lua_next(L_, _globals_idx)) {                                                       // L_: [fixed] args... k v                        L2: _G
            std::ignore = _c.interCopy(2);                                                         // L_: [fixed] args... k v                        L2: _G k v
            // assign it in L2's globals table
            lua_rawset(_L2, -3);                                                                   // L_: [fixed] args... k v                        L2: _G
            lua_pop(L_, 1);                                                                        // L_: [fixed] args... k
        }                                                                                          // L_: [fixed] args...
        lua_pop(_L2, 1);                                                                           // L_: [fixed] args...                            L2:
    }
    STACK_CHECK(L_, 0);
    STACK_CHECK(_L2, 0);

    // Lane main function
    [[maybe_unused]] int const _errorHandlerCount{ _lane->pushErrorHandler() };                    // L_: [fixed] args...                            L2: eh?
    LuaType const _func_type{ luaW_type(L_, kFuncIdx) };
    if (_func_type == LuaType::FUNCTION) {
        DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer lane body" << std::endl);
        DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
        lua_pushvalue(L_, kFuncIdx);                                                               // L_: [fixed] args... func                       L2: eh?
        InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} };
        InterCopyResult const _res{ _c.interMove(1) };                                             // L_: [fixed] args...                            L2: eh? func
        if (_res != InterCopyResult::Success) {
            raise_luaL_error(L_, "tried to copy unsupported types");
        }
    } else if (_func_type == LuaType::STRING) {
        DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: compile lane body" << std::endl);
        // compile the string
        if (luaL_loadstring(_L2, lua_tostring(L_, kFuncIdx)) != 0) {                               // L_: [fixed] args...                            L2: eh? func
            raise_luaL_error(L_, "error when parsing lane function code");
        }
    } else {
        raise_luaL_error(L_, "Expected function, got %s", luaW_typename(L_, _func_type).data());
    }
    STACK_CHECK(L_, 0);
    STACK_CHECK(_L2, _errorHandlerCount + 1);
    LUA_ASSERT(L_, lua_isfunction(_L2, _errorHandlerCount + 1));

    // revive arguments
    if (_nargs > 0) {
        DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer lane arguments" << std::endl);
        DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
        InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} };
        InterCopyResult const res{ _c.interMove(_nargs) };                                         // L_: [fixed]                                    L2: eh? func args...
        if (res != InterCopyResult::Success) {
            raise_luaL_error(L_, "tried to copy unsupported types");
        }
    }
    STACK_CHECK(L_, -_nargs);
    LUA_ASSERT(L_, lua_gettop(L_) == kFixedArgsIdx);
    STACK_CHECK(_L2, _errorHandlerCount + 1 + _nargs);

    // Store 'lane' in the lane's registry, for 'cancel_test()' (we do cancel tests at pending send/receive).
    kLanePointerRegKey.setValue(
        _L2, [lane = _lane](lua_State* L_) { lua_pushlightuserdata(L_, lane); }                    // L_: [fixed]                                    L2: eh? func args...
    );
    STACK_CHECK(_L2, _errorHandlerCount + 1 + _nargs);

    // if in coroutine mode, the Lane's master state stack should contain the thread
    if (_asCoroutine) {
        LUA_ASSERT(L_, _S != _L2);
        STACK_CHECK(_S, 1);
    }
    // and the thread's stack has whatever is needed to run
    STACK_CHECK(_L2, _errorHandlerCount + 1 + _nargs);

    STACK_CHECK_RESET_REL(L_, 0);
    // all went well, the lane's thread can start working
    _onExit.success();                                                                             // L_: [fixed] lane                               L2: <living its own life>
    // we should have the lane userdata on top of the stack
    STACK_CHECK(L_, 1);
    return 1;
}

// #################################################################################################

// threads() -> {}|nil
// Return a list of all known lanes
LUAG_FUNC(threads)
{
    LaneTracker const& _tracker = Universe::Get(L_)->tracker;
    return _tracker.pushThreadsTable(L_);
}

// #################################################################################################
// ######################################## Timer support ##########################################
// #################################################################################################

/*
 * secs = now_secs()
 *
 * Returns the current time, as seconds. Resolution depends on std::system_clock implementation
 * Can't use std::chrono::steady_clock because we need the same baseline as std::mktime
 */
LUAG_FUNC(now_secs)
{
    auto const _now{ std::chrono::system_clock::now() };
    lua_Duration duration{ _now.time_since_epoch() };

    lua_pushnumber(L_, duration.count());
    return 1;
}

// #################################################################################################

// wakeup_at_secs= wakeup_conv(date_tbl)
LUAG_FUNC(wakeup_conv)
{
    // date_tbl
    // .year (four digits)
    // .month (1..12)
    // .day (1..31)
    // .hour (0..23)
    // .min (0..59)
    // .sec (0..61)
    // .yday (day of the year)
    // .isdst (daylight saving on/off)

    STACK_CHECK_START_REL(L_, 0);
    auto _readInteger = [L = L_](std::string_view const& name_) {
        std::ignore = luaW_getfield(L, StackIndex{ 1 }, name_);
        lua_Integer const val{ lua_tointeger(L, -1) };
        lua_pop(L, 1);
        return static_cast<int>(val);
    };
    int const _year{ _readInteger("year") };
    int const _month{ _readInteger("month") };
    int const _day{ _readInteger("day") };
    int const _hour{ _readInteger("hour") };
    int const _min{ _readInteger("min") };
    int const _sec{ _readInteger("sec") };
    STACK_CHECK(L_, 0);

    // If Lua table has '.isdst' we trust that. If it does not, we'll let
    // 'mktime' decide on whether the time is within DST or not (value -1).
    //
    int const _isdst{ (luaW_getfield(L_, StackIndex{ 1 }, "isdst") == LuaType::BOOLEAN) ? lua_toboolean(L_, -1) : -1 };
    lua_pop(L_, 1);
    STACK_CHECK(L_, 0);

    std::tm _t{};
    _t.tm_year = _year - 1900;
    _t.tm_mon = _month - 1; // 0..11
    _t.tm_mday = _day;      // 1..31
    _t.tm_hour = _hour;     // 0..23
    _t.tm_min = _min;       // 0..59
    _t.tm_sec = _sec;       // 0..60
    _t.tm_isdst = _isdst;   // 0/1/negative

    lua_pushnumber(L_, static_cast<lua_Number>(std::mktime(&_t))); // resolution: 1 second
    return 1;
}

// #################################################################################################
// ######################################## Module linkage #########################################
// #################################################################################################

extern LUAG_FUNC(linda);

namespace {
    namespace local {
        static struct luaL_Reg const sLanesFunctions[] = {
            { "collectgarbage", LG_collectgarbage }, 
            { Universe::kFinally, Universe::InitializeFinalizer },
            { "linda", LG_linda },
            { "nameof", LG_nameof },
            { "thread_priority_range", LG_thread_priority_range },
            { "now_secs", LG_now_secs },
            { "register", lanes_register },
            { "set_singlethreaded", LG_set_singlethreaded },
            { "set_thread_priority", LG_set_thread_priority },
            { "set_thread_affinity", LG_set_thread_affinity },
            { "sleep", LG_sleep },
            { "supported_libs", state::LG_supported_libs },
            { "wakeup_conv", LG_wakeup_conv },
            { nullptr, nullptr }
        };
    } // namespace local
} // namespace

// #################################################################################################

// upvalue 1: module name
// upvalue 2: module table
// param 1: settings table
LUAG_FUNC(configure)
{
    // start with one-time initializations.
    {
        // C++ guarantees that the static variable initialization is threadsafe.
        [[maybe_unused]] static auto _ = std::invoke(
            []() {
#if (defined PLATFORM_OSX) && (defined _UTILBINDTHREADTOCPU)
                chudInitialize();
#endif
                return false;
            });
    }

    Universe* _U{ Universe::Get(L_) };
    bool const _from_master_state{ _U == nullptr };
    std::string_view const _name{ luaW_checkstring(L_, StackIndex{ lua_upvalueindex(1) }) };
    LUA_ASSERT(L_, luaW_type(L_, StackIndex{ 1 }) == LuaType::TABLE);

    STACK_GROW(L_, 4);
    STACK_CHECK_START_ABS(L_, 1);                                                                  // L_: settings

    DEBUGSPEW_CODE(DebugSpew(_U) << L_ << ": lanes.configure() BEGIN" << std::endl);
    DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });

    if (_U == nullptr) {
        // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global...
        kLaneNameRegKey.setValue(L_, [](lua_State* L_) { luaW_pushstring(L_, "main"); });

        // create the universe
        _U = Universe::Create(L_);                                                                 // L_: settings universe
    }
    STACK_CHECK(L_, 1);

    // Serialize calls to 'require' from now on, also in the primary state
    tools::SerializeRequire(L_);

    // Retrieve main module interface table
    lua_pushvalue(L_, lua_upvalueindex(2));                                                        // L_: settings M
    // remove configure() (this function) from the module interface
    lua_pushnil(L_);                                                                               // L_: settings M nil
    lua_setfield(L_, -2, "configure");                                                             // L_: settings M
    // add functions to the module's table
    luaW_registerlibfuncs(L_, local::sLanesFunctions);

    // register core.threads() only if settings say it should be available
    if (_U->tracker.isActive()) {
        lua_pushcfunction(L_, LG_threads);                                                         // L_: settings M LG_threads()
        lua_setfield(L_, -2, "threads");                                                           // L_: settings M
    }

    STACK_CHECK(L_, 2);
    UserValueCount const _nuv{ 0 }; // no uservalue in the linda
    DeepFactory::PushDeepProxy(DestState{ L_ }, _U->timerLinda, _nuv, LookupMode::LaneBody, L_);   // L_: settings M timerLinda
    lua_setfield(L_, -2, "timerLinda");                                                            // L_: settings M
    STACK_CHECK(L_, 2);

    // prepare the metatable for threads
    // contains keys: { __gc, __index, cancel, join, get_threadname }
    Lane::PushMetatable(L_);                                                                       // L_: settings M {lane_mt}
    lua_pushcclosure(L_, LG_lane_new, 1);                                                          // L_: settings M lane_new
    lua_setfield(L_, -2, "lane_new");                                                              // L_: settings M

    // we can't register 'lanes.require' normally because we want to create an upvalued closure
    lua_getglobal(L_, "require");                                                                  // L_: settings M require
    lua_pushcclosure(L_, LG_require, 1);                                                           // L_: settings M lanes.require
    lua_setfield(L_, -2, "require");                                                               // L_: settings M

    luaW_pushstring(
        L_,
        "%d.%d.%d",
        LANES_VERSION_MAJOR,
        LANES_VERSION_MINOR,
        LANES_VERSION_PATCH
    );                                                                                             // L_: settings M VERSION
    lua_setfield(L_, -2, "version");                                                               // L_: settings M

    kCancelError.pushKey(L_);                                                                      // L_: settings M kCancelError
    lua_setfield(L_, -2, "cancel_error");                                                          // L_: settings M

    kNilSentinel.pushKey(L_);                                                                      // L_: settings M kNilSentinel
    lua_setfield(L_, -2, "null");                                                                  // L_: settings M

    STACK_CHECK(L_, 2); // reference stack contains only the function argument 'settings'
    // we'll need this every time we transfer some C function from/to this state
    kLookupRegKey.setValue(L_, [](lua_State* L_) { lua_newtable(L_); });                           // L_: settings M
    STACK_CHECK(L_, 2);

    // register all native functions found in that module in the transferable functions database
    // we process it before _G because we don't want to find the module when scanning _G (this would generate longer names)
    // for example in package.loaded["lanes_core"].*
    tools::PopulateFuncLookupTable(L_, kIdxTop, _name);
    STACK_CHECK(L_, 2);

    // record all existing C/JIT-fast functions
    // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack
    if (_from_master_state) {
        // don't do this when called during the initialization of a new lane,
        // because we will do it after on_state_create() is called,
        // and we don't want to skip _G because of caching in case globals are created then
        luaW_pushglobaltable(L_);                                                                  // L_: settings M _G
        tools::PopulateFuncLookupTable(L_, kIdxTop, {});
        lua_pop(L_, 1);                                                                            // L_: settings M
    }
    lua_pop(L_, 1);                                                                                // L_: settings

    // set _R[kConfigRegKey] = settings
    kConfigRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); });
    STACK_CHECK(L_, 1);
    DEBUGSPEW_CODE(DebugSpew(_U) << L_ << ": lanes.configure() END" << std::endl);
    // Return the settings table
    return 1;
}

// #################################################################################################

#if defined PLATFORM_WIN32 && !defined NDEBUG
#include <signal.h>
#include <conio.h>

void signal_handler(int signal_)
{
    if (signal_ == SIGABRT) {
        _cprintf("caught abnormal termination!");
        abort();
    }
}

// #################################################################################################

// helper to have correct callstacks when crashing a Win32 running on 64 bits Windows
// don't forget to toggle Debug/Exceptions/Win32 in visual Studio too!
static void EnableCrashingOnCrashes(void)
{
    static std::atomic_flag s_ecoc_initDone;
    static std::atomic_flag s_ecoc_go_ahead;

    if (!s_ecoc_initDone.test_and_set(std::memory_order_acquire)) {
        using GetPolicy_t = BOOL(WINAPI *)(LPDWORD lpFlags);
        using SetPolicy_t = BOOL(WINAPI *)(DWORD dwFlags);
        const DWORD EXCEPTION_SWALLOWING = 0x1;

        HMODULE _kernel32 = LoadLibraryA("kernel32.dll");
        if (_kernel32) {
            auto pGetPolicy{ (GetPolicy_t) (void*) GetProcAddress(_kernel32, "GetProcessUserModeExceptionPolicy") };
            auto pSetPolicy{ (SetPolicy_t) (void*) GetProcAddress(_kernel32, "SetProcessUserModeExceptionPolicy") };
            if (pGetPolicy && pSetPolicy) {
                DWORD _dwFlags;
                if (pGetPolicy(&_dwFlags)) {
                    // Turn off the filter
                    pSetPolicy(_dwFlags & ~EXCEPTION_SWALLOWING);
                }
            }
            FreeLibrary(_kernel32);
        }
        // typedef void (* SignalHandlerPointer)( int);
        /*SignalHandlerPointer previousHandler =*/signal(SIGABRT, signal_handler);

        // we are done, other threads waiting to initialize lanes can proceed
        std::ignore = s_ecoc_go_ahead.test_and_set(std::memory_order_relaxed);
        s_ecoc_go_ahead.notify_all();
    } else {
        // wait until flag becomes true
        s_ecoc_go_ahead.wait(false, std::memory_order_relaxed);
    }
}
#endif // PLATFORM_WIN32 && !defined NDEBUG

// #################################################################################################

LANES_API int luaopen_lanes_core(lua_State* const L_)
{
#if defined PLATFORM_WIN32 && !defined NDEBUG
    EnableCrashingOnCrashes();
#endif // defined PLATFORM_WIN32 && !defined NDEBUG

    STACK_GROW(L_, 4);
    STACK_CHECK_START_REL(L_, 0);

    // Prevent PUC-Lua/LuaJIT mismatch. Hopefully this works for MoonJIT too
    if constexpr (LUAJIT_FLAVOR() == 0) {
        if (luaW_getmodule(L_, LUA_JITLIBNAME) != LuaType::NIL)
            raise_luaL_error(L_, "Lanes is built for PUC-Lua, don't run from LuaJIT");
    } else {
        if (luaW_getmodule(L_, LUA_JITLIBNAME) == LuaType::NIL)
            raise_luaL_error(L_, "Lanes is built for LuaJIT, don't run from PUC-Lua");
    }
    lua_pop(L_, 1);                                                                                // L_:
    STACK_CHECK(L_, 0);

    // Create main module interface table
    // we only have 1 closure, which must be called to configure Lanes
    lua_newtable(L_);                                                                              // L_: M
    lua_pushvalue(L_, 1);                                                                          // L_: M "lanes_core"
    lua_pushvalue(L_, -2);                                                                         // L_: M "lanes_core" M
    lua_pushcclosure(L_, LG_configure, 2);                                                         // L_: M LG_configure()
    kConfigRegKey.pushValue(L_);                                                                   // L_: M LG_configure() settings
    if (!lua_isnil(L_, -1)) { // this is not the first require "lanes_core": call configure() immediately
        lua_pushvalue(L_, -1);                                                                     // L_: M LG_configure() settings settings
        lua_setfield(L_, -4, "settings");                                                          // L_: M LG_configure() settings
        lua_call(L_, 1, 0);                                                                        // L_: M
    } else {
        // will do nothing on first invocation, as we haven't stored settings in the registry yet
        lua_setfield(L_, -3, "settings");                                                          // L_: M LG_configure()
        lua_setfield(L_, -2, "configure");                                                         // L_: M
        // lanes.null can be used for some configure settings, expose it now
        kNilSentinel.pushKey(L_);                                                                  // L_: M kNilSentinel
        lua_setfield(L_, -2, "null");                                                              // L_: M
    }

    STACK_CHECK(L_, 1);
    return 1;
}

// #################################################################################################

[[nodiscard]]
static int default_luaopen_lanes(lua_State* const L_)
{
    LuaError const _rc{ luaL_loadfile(L_, "lanes.lua") || lua_pcall(L_, 0, 1, 0) };
    if (_rc != LuaError::OK) {
        raise_luaL_error(L_, "failed to initialize embedded Lanes");
    }
    return 1;
}

// #################################################################################################

// call this instead of luaopen_lanes_core() when embedding Lua and Lanes in a custom application
LANES_API void luaopen_lanes_embedded(lua_State* const L_, lua_CFunction const luaopen_lanes_)
{
    STACK_CHECK_START_REL(L_, 0);
    // pre-require lanes_core so that when lanes.lua calls require "lanes_core" it finds it is already loaded
    luaL_requiref(L_, kLanesCoreLibName, luaopen_lanes_core, 0);                                   // L_: ... lanes_core
    lua_pop(L_, 1);                                                                                // L_: ...
    STACK_CHECK(L_, 0);
    // call user-provided function that runs the chunk "lanes.lua" from wherever they stored it
    luaL_requiref(L_, kLanesLibName, luaopen_lanes_ ? luaopen_lanes_ : default_luaopen_lanes, 0);  // L_: ... lanes
    STACK_CHECK(L_, 1);
}
