Loading a Lua module in two different states which run in two different threads with a mutex in place

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading a Lua module in two different states which run in two different threads with a mutex in place

Abhijit Nandy
Hi,

I have a Lua module(DLL) which I want to use in two different states which run in two different threads. There is a mutex to prevent access to the module by both threads simultaneously.

If I load the module in the first state, then can I load it in the second thread and use it(given that there is a mutex preventing use from both threads and therefore states, simultaneously). Could there be any issues if a module is loaded in two different states in the same application?

The reason is that there is a long running operation that involves function calls into the module. I cannot have my main(UI) thread blocked due to this. So I want to do it in a separate thread, which necessitates using a separate state due to thread safety.

After the separate thread has done its work, the module is required in the main thread. Until then, the main thread will not access the module.

Thanks,
Abhi
Reply | Threaded
Open this post in threaded view
|

Re: Loading a Lua module in two different states which run in two different threads with a mutex in place

Andrew Gierth
>>>>> "Abhijit" == Abhijit Nandy <[hidden email]> writes:

 Abhijit> Hi,

 Abhijit> I have a Lua module(DLL) which I want to use in two different
 Abhijit> states which run in two different threads. There is a mutex to
 Abhijit> prevent access to the module by both threads simultaneously.

 Abhijit> If I load the module in the first state, then can I load it in
 Abhijit> the second thread and use it(given that there is a mutex
 Abhijit> preventing use from both threads and therefore states,
 Abhijit> simultaneously). Could there be any issues if a module is
 Abhijit> loaded in two different states in the same application?

This should work in principle (assuming the module itself doesn't do
anything wrong).

Each lua state maintains its own independent table of loaded modules, so
dlopen/LoadLibrary will be called twice; for both functions, this is
specifically allowed, and a reference count of the number of opens is
kept by the system so that the module isn't unmapped until it has been
closed as many times as it was loaded.

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: Loading a Lua module in two different states which run in two different threads with a mutex in place

Thijs Schreijer


On 16 Jun 2019, at 15:44, Andrew Gierth <[hidden email]> wrote:

"Abhijit" == Abhijit Nandy <[hidden email]> writes:

Abhijit> Hi,

Abhijit> I have a Lua module(DLL) which I want to use in two different
Abhijit> states which run in two different threads. There is a mutex to
Abhijit> prevent access to the module by both threads simultaneously.

Abhijit> If I load the module in the first state, then can I load it in
Abhijit> the second thread and use it(given that there is a mutex
Abhijit> preventing use from both threads and therefore states,
Abhijit> simultaneously). Could there be any issues if a module is
Abhijit> loaded in two different states in the same application?

This should work in principle (assuming the module itself doesn't do
anything wrong).

Each lua state maintains its own independent table of loaded modules, so
dlopen/LoadLibrary will be called twice; for both functions, this is
specifically allowed, and a reference count of the number of opens is
kept by the system so that the module isn't unmapped until it has been
closed as many times as it was loaded.

--
Andrew.


Andrew is right, but there is a caveat; if the loaded module does not store its state in a the Lua state (but in its own global data somewhere), then it might actually share that data across multiple Lua states and hence cause problems.

Old post, but still valid: http://www.thijsschreijer.nl/blog/?p=693

So check the dll code to make sure it only stores state in the Lua state, and you should be good.

Thijs