Some questions about lua and threadsafety.

Forum for general chit-chat or off-topic discussion.

Moderators: CEGUI MVP, CEGUI Team

User avatar
Tinyterror
Just popping in
Just popping in
Posts: 3
Joined: Thu Nov 17, 2005 17:37

Some questions about lua and threadsafety.

Postby Tinyterror » Thu Nov 17, 2005 18:11

I've been looking at cegui for a few days now, and am strongly considering adding it to my project. To put the rest of my post in context, a brief description of what I am working on in order.

My project is called Lunar. It is a lua based scripting engine for final fantasy 11, with the goal of allowing users to be able to create thier own UI elements similar to world of warcraft's scripting features. Currently I provide my users with some very basic primitive based drawing functions that can be used to draw simple displays on the screen that display game data. Lunar allows multiple scripts to be run at once, and is fairly stable in the most current release. I would like to integrate cegui into lunar in order to provide advanced gui functions to script writers. Naturally, I have some questions for the developers.

1. Can cegui load it's lua bindings into a preexisting lua state without executing the state? I already have a threaded scriptrunner object that sets up lua states and then runs them. I would like to be able to keep the lua_pcall call there, and have something like luaL_openlib that prepares the lua state to call cegui stuff without actually executing it, I've looked at the lua scripting module code for cegui, and it looks like this could be done with a small modification to the LuaScriptModule::LuaScriptModule(lua_State* state) function (mostly removing its lua_pcall call). Would this work, or am I completely confused?

2. Is cegui threadsafe? As previously mentioned, my project can run multiple lua scripts at once, and if these threads are making concurrent calls to cegui, threadsafety becomes an issue. My main concern is for the static functions in the lua binding code. I didn't see any sign of locking mechanisms there, so I have a few concerns. Has anyone run into threading issues with cegui? Any advice on this subject would be greatly appreciated.

Sorry if some of my questions may sound somewhat uninformed. I only just recently found out about cegui, and have not had as much time as I would like to pour through it's source code. Thanks in advance for any help that you guys can offer.

User avatar
lindquist
CEGUI Team (Retired)
Posts: 770
Joined: Mon Jan 24, 2005 21:20
Location: Copenhagen, Denmark

Re: Some questions about lua and threadsafety.

Postby lindquist » Fri Nov 18, 2005 15:30

CEGUI is not thread safe. And no measures has been taking to make the Lua module thread safe either...

The constructor you mention only stores a copy of the lua_State pointer. It does not call it.
The bindings however are registered during initialisation of the library.

With the 0.4 Lua module, you'll have to make some "major" modifications, as it's really not geared towards using multiple Lua states. Event handlers can only be registered by name, and it will only use the state it got during construction (that's either your's or it's own).

In the next CEGUI release the Lua module will support binding anonymous functions and when the registration is done from inside Lua it will use the state that is making the registration. The bindings are still only registered in the first lua_State though.

I guess I could add a overload of LuaScriptModule::createBindings to take the state to register to as a parameter, though I'm not sure how usefull it would be without thread safety.

I'm not really sure what the future plan (if there is one) for CEGUI and multithreading is, but I can say it not something being worked on AFAIK.

HTH

User avatar
Tinyterror
Just popping in
Just popping in
Posts: 3
Joined: Thu Nov 17, 2005 17:37

Re: Some questions about lua and threadsafety.

Postby Tinyterror » Fri Nov 18, 2005 19:16

Thanks for the reply.

I came up with a solution that should work for my project. I'm going to have all of the cegui stuff running in it's own thread. If one of my users wants to use the GUI in thier script, they will have to make a section that sets up the GUI, registers callback functions, and any other GUI related tasks. That section will be given to the cegui thread to process. The body of thier script will be in its own thread, running within the system I already have in place. Communication between the GUI script and the main script will take place through a simple IPC message passing mechanism that I'm going to put together. Because the point of contact between the cegui thread and the lua script threads will be small, it will be really easy to make the whole thing threadsafe.

Obviously this is not quite as nice as direct access to cegui from scripts, but it could be a whole lot worse. When I get it all working, I would be willing to post the code for anyone who is interested.

User avatar
lindquist
CEGUI Team (Retired)
Posts: 770
Joined: Mon Jan 24, 2005 21:20
Location: Copenhagen, Denmark

Re: Some questions about lua and threadsafety.

Postby lindquist » Sat Nov 19, 2005 00:19

If you make modifications to the Lua module that eases this up, yet does not break the current usage pattern I would definitely like a patch...

The more flexible the better ;)

User avatar
Tinyterror
Just popping in
Just popping in
Posts: 3
Joined: Thu Nov 17, 2005 17:37

Re: Some questions about lua and threadsafety.

Postby Tinyterror » Sat Nov 19, 2005 18:38

Heh, if I could do it, I would. Unfortunately the changes wouldn't just be for the lua module. They would need to be system wide in cegui, and also in the tolua generated bindings. Thats a whole hell of a lot of code. It would really be up to the developers to impliment this kind of thing. If you don't impliment locking mechanisms correctly, you can run into some serious problems.


Return to “Offtopic Discussion”

Who is online

Users browsing this forum: No registered users and 8 guests