8000 A way nicer way to resume an async operation by vegorov-rbx · Pull Request #18 · luau-lang/lute · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

A way nicer way to resume an async operation #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

< 8000 /div>
Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cli/queijo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static bool runToCompletion(Runtime& runtime)
auto next = std::move(runtime.runningThreads.front());
runtime.runningThreads.erase(runtime.runningThreads.begin());

next.ref.push(runtime.GL);
next.ref->push(runtime.GL);
lua_State* L = lua_tothread(runtime.GL, -1);

if (L == nullptr)
Expand All @@ -119,7 +119,12 @@ static bool runToCompletion(Runtime& runtime)
// We still have 'next' on stack to hold on to thread we are about to run
lua_pop(runtime.GL, 1);

int status = lua_resume(L, nullptr, next.argumentCount);
int status = LUA_OK;

if (!next.success)
status = lua_resumeerror(L, nullptr);
else
status = lua_resume(L, nullptr, next.argumentCount);

if (status == LUA_YIELD)
{
Expand Down
31 changes: 12 additions & 19 deletions net/src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,24 @@ static int getAsync(lua_State* L)
{
std::string url = luaL_checkstring(L, 1);

lua_pushthread(L);
int refId = lua_ref(L, -1);
lua_pop(L, 1);

auto ref = getRefForThread(L);
Runtime* runtime = getRuntime(L);

// TODO: switch to libuv, add cancellations
std::thread thread = std::thread([=] {
auto [error, data] = requestData(url);

runtime->scheduleLuauContinuation([error = std::move(error), data = std::move(data), refId](lua_State* GL) {
lua_getref(GL, refId);
lua_unref(GL, refId);
lua_State* L = lua_tothread(GL, -1);

if (!error.empty())
{
std::string formatted = "network request failed: " + error;
lua_pushlstring(L, formatted.data(), formatted.size());
return false;
}

lua_pushlstring(L, data.data(), data.size());
return true;
});
if (!error.empty())
{
runtime->scheduleLuauError(ref, "network request failed: " + error);
}
else
{
runtime->scheduleLuauResume(ref, [data = std::move(data)](lua_State* L) {
lua_pushlstring(L, data.data(), data.size());
return 1;
});
}
});

thread.detach();
Expand Down
18 changes: 5 additions & 13 deletions runtime/include/queijo/ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "lua.h"

#include <memory>
#include <utility>

// Only interact with Ref from main thread! (one day it might even get enforced)
Expand All @@ -16,17 +17,8 @@ struct Ref
{
lua_unref(GL, refId);
}
Ref(Ref&& rhs) noexcept
{
std::swap(GL, rhs.GL);
std::swap(refId, rhs.refId);
}
Ref& operator=(Ref&& rhs) noexcept
{
std::swap(GL, rhs.GL);
std::swap(refId, rhs.refId);
return *this;
}
Ref(Ref&& rhs) noexcept = delete;
Ref& operator=(Ref&& rhs) noexcept = delete;
Ref(const Ref& rhs) = delete;
Ref& operator=(const Ref& rhs) = delete;

Expand All @@ -39,10 +31,10 @@ struct Ref
int refId = 0;
};

inline Ref getRefForThread(lua_State* L)
inline std::shared_ptr<Ref> getRefForThread(lua_State* L)
{
lua_pushthread(L);
Ref ref(L, -1);
std::shared_ptr<Ref> ref = std::make_shared<Ref>(L, -1);
lua_pop(L, 1);
return ref;
}
9 changes: 7 additions & 2 deletions runtime/include/queijo/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
struct ThreadToContinue
{
bool success = false;
Ref ref;
std::shared_ptr<Ref> ref;
int argumentCount = 0;
};

struct Runtime
{
bool hasContinuations();
void scheduleLuauContinuation(std::function<bool(lua_State*)> cb);

// Resume thread with the specified error
void scheduleLuauError(std::shared_ptr<Ref> ref, std::string error);

// Resume thread with the results computed by the continuation
void scheduleLuauResume(std::shared_ptr<Ref> ref, std::function<int(lua_State*)> cont);

lua_State* GL = nullptr;

Expand Down
24 changes: 19 additions & 5 deletions runtime/src/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,31 @@ bool Runtime::hasContinuations()
return !continuations.empty();
}

void Runtime::scheduleLuauContinuation(std::function<bool(lua_State*)> cb)
void Runtime::scheduleLuauError(std::shared_ptr<Ref> ref, std::string error)
{
std::unique_lock lock(continuationMutex);

continuations.push_back([this, cb] {
bool success = cb(GL);
continuations.push_back([this, ref, error = std::move(error)]() mutable {
ref->push(GL);
lua_State* L = lua_tothread(GL, -1);
assert(L);
lua_pop(GL, 1);

runningThreads.push_back({ success, Ref(GL, -1), lua_gettop(L) });
lua_pushlstring(L, error.data(), error.size());
runningThreads.push_back({ false, ref, lua_gettop(L) });
});
}

void Runtime::scheduleLuauResume(std::shared_ptr<Ref> ref, std::function<int(lua_State*)> cont)
{
std::unique_lock lock(continuationMutex);

continuations.push_back([this, ref, cont = std::move(cont)]() mutable {
ref->push(GL);
lua_State* L = lua_tothread(GL, -1);
lua_pop(GL, 1);

int results = cont(L);
runningThreads.push_back({ true, ref, results });
});
}

Expand Down
Loading
0