mirror of
https://github.com/luanti-org/luanti.git
synced 2025-09-05 18:41:05 +00:00
Add API to cancel async jobs (#14602)
* Implement API to cancel async jobs Co-authored-by: sfan5 <sfan5@live.de> * update AsyncJob:cancel documentation from review * Use IPC to unblock async * review * review async unblocking * review * Apply suggestions from code review Co-authored-by: sfan5 <sfan5@live.de> * minor licensing --------- Co-authored-by: y5nw <y5nw@protonmail.com> Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
parent
7cbe62fe7b
commit
f390137d6e
12 changed files with 229 additions and 78 deletions
|
@ -3,6 +3,7 @@ file(GLOB common_SCRIPT_LUA_API_HDRS "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
|
|||
set(common_SCRIPT_LUA_API_SRCS
|
||||
${common_SCRIPT_LUA_API_HDRS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_areastore.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_async.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_auth.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_base.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_craft.cpp
|
||||
|
|
64
src/script/lua_api/l_async.cpp
Normal file
64
src/script/lua_api/l_async.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
|
||||
#include "lua_api/l_internal.h"
|
||||
#include "lua_api/l_async.h"
|
||||
#include "cpp_api/s_async.h"
|
||||
|
||||
static std::string get_serialized_function(lua_State *L, int index)
|
||||
{
|
||||
luaL_checktype(L, index, LUA_TFUNCTION);
|
||||
call_string_dump(L, index);
|
||||
size_t func_length;
|
||||
const char *serialized_func_raw = lua_tolstring(L, -1, &func_length);
|
||||
std::string serialized_func(serialized_func_raw, func_length);
|
||||
lua_pop(L, 1);
|
||||
return serialized_func;
|
||||
}
|
||||
|
||||
// do_async_callback(func, params, mod_origin)
|
||||
int ModApiAsync::l_do_async_callback(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
ScriptApiAsync *script = getScriptApi<ScriptApiAsync>(L);
|
||||
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
luaL_checktype(L, 3, LUA_TSTRING);
|
||||
|
||||
auto serialized_func = get_serialized_function(L, 1);
|
||||
PackedValue *param = script_pack(L, 2);
|
||||
std::string mod_origin = readParam<std::string>(L, 3);
|
||||
|
||||
u32 jobId = script->queueAsync(
|
||||
std::move(serialized_func),
|
||||
param, mod_origin);
|
||||
|
||||
lua_pushinteger(L, jobId);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// cancel_async_callback(id)
|
||||
int ModApiAsync::l_cancel_async_callback(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
ScriptApiAsync *script = getScriptApi<ScriptApiAsync>(L);
|
||||
u32 id = luaL_checkinteger(L, 1);
|
||||
lua_pushboolean(L, script->cancelAsync(id));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// get_async_capacity()
|
||||
int ModApiAsync::l_get_async_threading_capacity(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
ScriptApiAsync *script = getScriptApi<ScriptApiAsync>(L);
|
||||
lua_pushinteger(L, script->getThreadingCapacity());
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ModApiAsync::Initialize(lua_State *L, int top)
|
||||
{
|
||||
API_FCT(do_async_callback);
|
||||
API_FCT(cancel_async_callback);
|
||||
API_FCT(get_async_threading_capacity);
|
||||
}
|
19
src/script/lua_api/l_async.h
Normal file
19
src/script/lua_api/l_async.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lua_api/l_base.h"
|
||||
|
||||
class ModApiAsync : public ModApiBase
|
||||
{
|
||||
public:
|
||||
static void Initialize(lua_State *L, int top);
|
||||
private:
|
||||
// do_async_callback(func, params, mod_origin)
|
||||
static int l_do_async_callback(lua_State *L);
|
||||
// cancel_async_callback(id)
|
||||
static int l_cancel_async_callback(lua_State *L);
|
||||
// get_async_threading_capacity()
|
||||
static int l_get_async_threading_capacity(lua_State *L);
|
||||
};
|
|
@ -625,33 +625,6 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
|
|||
return 0;
|
||||
}
|
||||
|
||||
// do_async_callback(func, params, mod_origin)
|
||||
int ModApiServer::l_do_async_callback(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
ServerScripting *script = getScriptApi<ServerScripting>(L);
|
||||
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
luaL_checktype(L, 3, LUA_TSTRING);
|
||||
|
||||
call_string_dump(L, 1);
|
||||
size_t func_length;
|
||||
const char *serialized_func_raw = lua_tolstring(L, -1, &func_length);
|
||||
|
||||
PackedValue *param = script_pack(L, 2);
|
||||
|
||||
std::string mod_origin = readParam<std::string>(L, 3);
|
||||
|
||||
u32 jobId = script->queueAsync(
|
||||
std::string(serialized_func_raw, func_length),
|
||||
param, mod_origin);
|
||||
|
||||
lua_settop(L, 0);
|
||||
lua_pushinteger(L, jobId);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// register_async_dofile(path)
|
||||
int ModApiServer::l_register_async_dofile(lua_State *L)
|
||||
{
|
||||
|
@ -747,7 +720,6 @@ void ModApiServer::Initialize(lua_State *L, int top)
|
|||
API_FCT(unban_player_or_ip);
|
||||
API_FCT(notify_authentication_modified);
|
||||
|
||||
API_FCT(do_async_callback);
|
||||
API_FCT(register_async_dofile);
|
||||
API_FCT(serialize_roundtrip);
|
||||
|
||||
|
|
|
@ -100,9 +100,6 @@ private:
|
|||
// notify_authentication_modified(name)
|
||||
static int l_notify_authentication_modified(lua_State *L);
|
||||
|
||||
// do_async_callback(func, params, mod_origin)
|
||||
static int l_do_async_callback(lua_State *L);
|
||||
|
||||
// register_async_dofile(path)
|
||||
static int l_register_async_dofile(lua_State *L);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue