golua icon indicating copy to clipboard operation
golua copied to clipboard

WIP - Working on new thread TODO items

Open MrNavaStar opened this issue 1 year ago • 14 comments

I hope I'm on the right track with these TODO items, the main reason I am working on this is because I need new thread to keep its context (should have just added context in the other pr, oh well). Figured I would try and take a crack at completing the other items of it while I'm here.

I'm not sure how to handle the go registry list. do we need to deep copy all the items in the list? or is a reference ok? Idk, lmk what your thoughts/plan for this is.

MrNavaStar avatar Nov 11 '24 10:11 MrNavaStar

I never looked into what lua_newthread does in detail but wouldn't this imply that all changes we make to our state object need to be under a mutex (with all the performance consequences that entails)?

Il lun 11 nov 2024, 11:09 MrNavaStar @.***> ha scritto:

I hope I'm on the right track with these TODO items, the main reason I am working on this is because I need new thread to keep its context (should have just added context in the other pr, oh well). Figured I would try and take a crack at completing the other items of it while I'm here.

I'm not sure how to handle the go registry list. do we need to deep copy all the items in the list? or is a reference ok? Idk, lmk what your thoughts/plan for this is.

You can view, comment on, or merge this pull request online at:

https://github.com/aarzilli/golua/pull/119 Commit Summary

File Changes

(1 file https://github.com/aarzilli/golua/pull/119/files)

Patch Links:

  • https://github.com/aarzilli/golua/pull/119.patch
  • https://github.com/aarzilli/golua/pull/119.diff

— Reply to this email directly, view it on GitHub https://github.com/aarzilli/golua/pull/119, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACKEBCFYKRNFCUPGWBR4AD2AB65DAVCNFSM6AAAAABRRPM2B6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGY2DQOBSHA4DEOA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

aarzilli avatar Nov 11 '24 13:11 aarzilli

From what I gather, lua_newThread creates a brand new state with the same environment (globals) but a new stack. This would suggest to me that we don't need a mutex for state.

This stack overflow post has some good information: https://stackoverflow.com/questions/26574705/lua-newstate-vs-lua-newthread

however, if we decide to keep the same registry list (and maybe free indices list?) without copying, we probably need a mutex for that

MrNavaStar avatar Nov 11 '24 20:11 MrNavaStar

The lua documentation says this:

Creates a new thread, pushes it on the stack, and returns a pointer to a lua_State that represents this new thread. The new thread returned by this function shares with the original thread its global environment, but has an independent execution stack.

https://www.lua.org/manual/5.3/manual.html#lua_newthread

aarzilli avatar Nov 12 '24 09:11 aarzilli

Right, so it's a new state object entirely with its own stack. Shouldn't require a mutex right?

There is also the Lua lock and unlock functions to be implemented that handle the mutex for the shared environment

MrNavaStar avatar Nov 12 '24 10:11 MrNavaStar

Suppose something new is registered in a thread and placed in a global variable, does it become available to other threads?

aarzilli avatar Nov 12 '24 10:11 aarzilli

I believe it does, however that is handled by the lua runtime (assuming we implement the lock and unlock functions if they haven't been already). We shouldn't need our own mutex for that

MrNavaStar avatar Nov 12 '24 10:11 MrNavaStar

If that is the case the registry needs to be shared between the threads and probably also need synchronization.

aarzilli avatar Nov 12 '24 11:11 aarzilli

Okay cool, that was the main thing I was unsure about. I'll take a crack at that tomorrow.

Does the free indices list need to be treated the same way, or is a copy okay?

MrNavaStar avatar Nov 12 '24 11:11 MrNavaStar

Threading mostly works, however past creating 40+ threads things start to fall apart. I suspect I am hitting some sort of stack limit. This isn't really an issue for me, but perhaps its still something we want to look into?

lmk if there is anything else concerning you see or something you don't like the implementation of.

I also noticed that the ToThread() function is not implemented, so ill probably work on that. Ill also look into the lua lock/unlock functions and how to implement them.

MrNavaStar avatar Nov 15 '24 20:11 MrNavaStar

Threading mostly works, however past creating 40+ threads things start to fall apart

What does falling apart means?

aarzilli avatar Nov 21 '24 13:11 aarzilli

It crashes - the errors mention gc issues. I can't make much sense of it

MrNavaStar avatar Nov 21 '24 16:11 MrNavaStar

Sounds like memory corruption, tbh.

aarzilli avatar Nov 21 '24 16:11 aarzilli

What would cause that? Some buffer overflowing or something similar?

MrNavaStar avatar Nov 21 '24 16:11 MrNavaStar

Having trouble debugging something else. Very rarely, I get this: image

The error is coming from the L.Call() inside the goroutine in the threads.go example. I've been unable to track down where its coming from inside it. Any thoughts?

MrNavaStar avatar Nov 26 '24 05:11 MrNavaStar