Skip to content

Create a threading library #149

@bycruz

Description

@bycruz

Need a threading library for simple multithreading without any lua c api overhead.

local ffi = require("ffi")

local threading = {}

ffi.cdef([[
typedef struct lua_State lua_State;

lua_State* luaL_newstate(void);
void luaL_openlibs(lua_State* L);
void lua_close(lua_State* L);

int luaL_loadbufferx(lua_State* L, const char* buff, size_t sz, const char* name, const char* mode);
int lua_pcallk(lua_State* L, int nargs, int nresults, int errfunc, int ctx, void* k);

int lua_gettop(lua_State* L);
int lua_type(lua_State* L, int idx);
const char* lua_tolstring(lua_State* L, int idx, size_t* len);
lua_State* lua_tothread(lua_State* L, int idx);
lua_State* lua_newthread(lua_State* L);

double lua_tonumberx(lua_State* L, int idx, int* isnum);
lua_Integer lua_tointegerx(lua_State* L, int idx, int* isnum);
int lua_toboolean(lua_State* L, int idx);
]])

---@class threading.Handle
local Handle = {}
Handle.__index = Handle

function Handle.new()
	return setmetatable({}, Handle)
end

function Handle:join()
end

---@param state ffi.cdata*
---@param i number # Index on lua stack
---@return string.buffer
local function serialize(state, i)
	local ty = ffi.C.lua_type(state, i)
	-- only support number, string, boolean for now
end

---@param b string.buffer
---@return any
local function deserialize(b)
end

---@param closure fun()
function threading.spawn(closure)
	local ups = debug.getinfo(closure, "u").nups
	if ups > 0 then
		error("Cannot have upvalues in thread")
	end

	local bc = string.dump(closure)
	-- spawn thread that
end

---@param ms number
function threading.sleep(ms)
end

return threading

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions