c/lua:thread

This commit is contained in:
luoliwoshang
2024-09-07 18:54:09 +08:00
parent 3c588e67b8
commit 743ddf83c1
2 changed files with 56 additions and 5 deletions

View File

@@ -0,0 +1,44 @@
package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/lua"
)
func pushThread(state *lua.State, name string) {
isMain := state.Pushthread()
if isMain != 0 {
c.Printf(c.Str("%s Thread is main\n"), c.AllocaCStr(name))
} else {
c.Printf(c.Str("%s Thread is not main\n"), c.AllocaCStr(name))
}
}
func main() {
L := lua.Newstate()
defer L.Close()
L.Openlibs()
pushThread(L, "main")
L.Pop(1)
newThread := L.Newthread()
pushThread(newThread, "newthread")
state := newThread.Tothread(-1)
if newThread == state {
c.Printf(c.Str("Successfully retrieved thread from stack\n"))
}
status := state.Status()
c.Printf(c.Str("New thread status: %d"), status)
if L.Closethread(newThread) != lua.OK {
println("Failed to close thread status %d", state.Status())
}
}
/* Expected output:
main Thread is main
newthread Thread is not main
Successfully retrieved thread from stack
New thread status: 0
*/

View File

@@ -159,13 +159,17 @@ func (L *State) Close() {}
// llgo:link (*State).Newthread C.lua_newthread
func (L *State) Newthread() *State { return nil }
// int (lua_closethread) (State *L, State *from);
// int (lua_resetthread) (State *L); /* Deprecated! */
// llgo:link (*State).Closethread C.lua_closethread
func (L *State) Closethread(from *State) c.Int { return 0 }
// llgo:link (*State).Resetthread C.lua_resetthread
func (L *State) Resetthread(from *State) c.Int { return 0 }
// llgo:link (*State).Atpanic C.lua_atpanic
func (L *State) Atpanic(panicf CFunction) CFunction { return nil }
// lua_Number (lua_version) (State *L);
// llgo:link (*State).Version C.lua_version
func (L *State) Version() Number { return 0 }
// /*
// ** basic stack manipulation
@@ -240,7 +244,9 @@ func (L *State) Tocfunction(idx c.Int) CFunction { return nil }
// llgo:link (*State).Touserdata C.lua_touserdata
func (L *State) Touserdata(idx c.Int) c.Pointer { return nil }
// LUA_API State *(lua_tothread) (State *L, int idx);
// llgo:link (*State).Tothread C.lua_tothread
func (L *State) Tothread(idx c.Int) *State { return nil }
// LUA_API const void *(lua_topointer) (State *L, int idx);
// /*
@@ -277,7 +283,8 @@ func (L *State) Pushboolean(b c.Int) {}
// llgo:link (*State).Pushlightuserdata C.lua_pushlightuserdata
func (L *State) Pushlightuserdata(p c.Pointer) {}
//int (lua_pushthread) (State *L);
// llgo:link (*State).Pushthread C.lua_pushthread
func (L *State) Pushthread() c.Int { return 0 }
// /*
// ** get functions (Lua -> stack)