Skip to content

Conversation

@MrNavaStar
Copy link

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.

@aarzilli
Copy link
Owner

aarzilli commented Nov 11, 2024 via email

@MrNavaStar
Copy link
Author

MrNavaStar commented Nov 11, 2024

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

@aarzilli
Copy link
Owner

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

@MrNavaStar
Copy link
Author

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

@aarzilli
Copy link
Owner

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

@MrNavaStar
Copy link
Author

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

@aarzilli
Copy link
Owner

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

@MrNavaStar
Copy link
Author

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
Copy link
Author

MrNavaStar commented Nov 15, 2024

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.

@aarzilli
Copy link
Owner

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

What does falling apart means?

@MrNavaStar
Copy link
Author

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

@aarzilli
Copy link
Owner

Sounds like memory corruption, tbh.

@MrNavaStar
Copy link
Author

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

@MrNavaStar
Copy link
Author

MrNavaStar commented Nov 26, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants