When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

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

When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

孙世龙 sunshilong
Hi, list

    In general, the macro named lua_lock is expanded as a
no-operation(i.e (void)0).

    Here is the related code snippet in Lua-5.3.5:
/*
** macros that are executed whenever program enters the Lua core
** ('lua_lock') and leaves the core ('lua_unlock')
*/
#if !defined(lua_lock)
#define lua_lock(L) ((void) 0)
#define lua_unlock(L) ((void) 0)
#endif
I wonder when I need to provide a self-defined macro (e.g:
 #define lua_lock(L) pthread_lock(&mutex))?

I would appreciate it if you could shed some light on this question.

Best regards
sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

Victor Bombi

> On January 28, 2021 at 10:17 AM 孙世龙 sunshilong <[hidden email]> wrote:
>
>
> Hi, list
>
>     In general, the macro named lua_lock is expanded as a
> no-operation(i.e (void)0).
>
>     Here is the related code snippet in Lua-5.3.5:
> /*
> ** macros that are executed whenever program enters the Lua core
> ** ('lua_lock') and leaves the core ('lua_unlock')
> */
> #if !defined(lua_lock)
> #define lua_lock(L) ((void) 0)
> #define lua_unlock(L) ((void) 0)
> #endif
> I wonder when I need to provide a self-defined macro (e.g:
>  #define lua_lock(L) pthread_lock(&mutex))?
>
> I would appreciate it if you could shed some light on this question.
>
> Best regards

Lua is not multithread ready unless you define lua_lock. But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
The best way to use Lua in a multithread environment is to create a different lua_State in each thread and let all different lua_States to inter-communicate. I would recomend using LuaLanes for achieving this althought there are a lot of alternatives.

Best
Victor Bombí
> sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

孙世龙 sunshilong
>But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
Did you ever encounter deadlock problems when using vanilla Lua?

Best regards
sunshilong

On Thu, Jan 28, 2021 at 6:22 PM Victor Bombi <[hidden email]> wrote:

>
>
> > On January 28, 2021 at 10:17 AM 孙世龙 sunshilong <[hidden email]> wrote:
> >
> >
> > Hi, list
> >
> >     In general, the macro named lua_lock is expanded as a
> > no-operation(i.e (void)0).
> >
> >     Here is the related code snippet in Lua-5.3.5:
> > /*
> > ** macros that are executed whenever program enters the Lua core
> > ** ('lua_lock') and leaves the core ('lua_unlock')
> > */
> > #if !defined(lua_lock)
> > #define lua_lock(L) ((void) 0)
> > #define lua_unlock(L) ((void) 0)
> > #endif
> > I wonder when I need to provide a self-defined macro (e.g:
> >  #define lua_lock(L) pthread_lock(&mutex))?
> >
> > I would appreciate it if you could shed some light on this question.
> >
> > Best regards
>
> Lua is not multithread ready unless you define lua_lock. But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
> The best way to use Lua in a multithread environment is to create a different lua_State in each thread and let all different lua_States to inter-communicate. I would recomend using LuaLanes for achieving this althought there are a lot of alternatives.
>
> Best
> Victor Bombí
> > sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

Spar
Any infinite loop in lua code will lock your mutex forever
On 28 Jan 2021, 14:28 +0300, 孙世龙 sunshilong <[hidden email]>, wrote:
But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
Did you ever encounter deadlock problems when using vanilla Lua?

Best regards
sunshilong

On Thu, Jan 28, 2021 at 6:22 PM Victor Bombi <[hidden email]> wrote:


On January 28, 2021 at 10:17 AM 孙世龙 sunshilong <[hidden email]> wrote:


Hi, list

In general, the macro named lua_lock is expanded as a
no-operation(i.e (void)0).

Here is the related code snippet in Lua-5.3.5:
/*
** macros that are executed whenever program enters the Lua core
** ('lua_lock') and leaves the core ('lua_unlock')
*/
#if !defined(lua_lock)
#define lua_lock(L) ((void) 0)
#define lua_unlock(L) ((void) 0)
#endif
I wonder when I need to provide a self-defined macro (e.g:
#define lua_lock(L) pthread_lock(&mutex))?

I would appreciate it if you could shed some light on this question.

Best regards

Lua is not multithread ready unless you define lua_lock. But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
The best way to use Lua in a multithread environment is to create a different lua_State in each thread and let all different lua_States to inter-communicate. I would recomend using LuaLanes for achieving this althought there are a lot of alternatives.

Best
Victor Bombí
sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

孙世龙 sunshilong
Spar > Any infinite loop in lua code will lock your mutex forever
I am shocked to hear that indeed.
How come it is? Once there is a lua_lock, there will be a lua_unlock.
Could you please give me a simple example?

On Thu, Jan 28, 2021 at 7:30 PM Spar <[hidden email]> wrote:

>
> Any infinite loop in lua code will lock your mutex forever
> On 28 Jan 2021, 14:28 +0300, 孙世龙 sunshilong <[hidden email]>, wrote:
>
> But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
>
> Did you ever encounter deadlock problems when using vanilla Lua?
>
> Best regards
> sunshilong
>
> On Thu, Jan 28, 2021 at 6:22 PM Victor Bombi <[hidden email]> wrote:
>
>
>
> On January 28, 2021 at 10:17 AM 孙世龙 sunshilong <[hidden email]> wrote:
>
>
> Hi, list
>
> In general, the macro named lua_lock is expanded as a
> no-operation(i.e (void)0).
>
> Here is the related code snippet in Lua-5.3.5:
> /*
> ** macros that are executed whenever program enters the Lua core
> ** ('lua_lock') and leaves the core ('lua_unlock')
> */
> #if !defined(lua_lock)
> #define lua_lock(L) ((void) 0)
> #define lua_unlock(L) ((void) 0)
> #endif
> I wonder when I need to provide a self-defined macro (e.g:
> #define lua_lock(L) pthread_lock(&mutex))?
>
> I would appreciate it if you could shed some light on this question.
>
> Best regards
>
>
> Lua is not multithread ready unless you define lua_lock. But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
> The best way to use Lua in a multithread environment is to create a different lua_State in each thread and let all different lua_States to inter-communicate. I would recomend using LuaLanes for achieving this althought there are a lot of alternatives.
>
> Best
> Victor Bombí
>
> sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

孙世龙 sunshilong
>Let's say you call a lua function, this locked lua state and now nothing can unlock it unless the functions finishes. if that infinite loop calls a C function or >triggers any lua api they call lock function again which lock make it wait itself
>If you want multithreading try something like https://github.com/effil/eiffel
Do you have any idea about how eiffel avoid the deadlocks?
Can I draw the conclusion that the eiffel drops some lock and unlock
operation (comparing to vanilla Lua) to avoid deadlocks?

On Thu, Jan 28, 2021 at 7:40 PM Spar <[hidden email]> wrote:

>
> If you want multithreading try something like https://github.com/effil/effil
> On 28 Jan 2021, 14:35 +0300, 孙世龙 sunshilong <[hidden email]>, wrote:
>
> Spar > Any infinite loop in lua code will lock your mutex forever
> I am shocked to hear that indeed.
> How come it is? Once there is a lua_lock, there will be a lua_unlock.
> Could you please give me a simple example?
>
> On Thu, Jan 28, 2021 at 7:30 PM Spar <[hidden email]> wrote:
>
>
> Any infinite loop in lua code will lock your mutex forever
> On 28 Jan 2021, 14:28 +0300, 孙世龙 sunshilong <[hidden email]>, wrote:
>
> But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
>
> Did you ever encounter deadlock problems when using vanilla Lua?
>
> Best regards
> sunshilong
>
> On Thu, Jan 28, 2021 at 6:22 PM Victor Bombi <[hidden email]> wrote:
>
>
>
> On January 28, 2021 at 10:17 AM 孙世龙 sunshilong <[hidden email]> wrote:
>
>
> Hi, list
>
> In general, the macro named lua_lock is expanded as a
> no-operation(i.e (void)0).
>
> Here is the related code snippet in Lua-5.3.5:
> /*
> ** macros that are executed whenever program enters the Lua core
> ** ('lua_lock') and leaves the core ('lua_unlock')
> */
> #if !defined(lua_lock)
> #define lua_lock(L) ((void) 0)
> #define lua_unlock(L) ((void) 0)
> #endif
> I wonder when I need to provide a self-defined macro (e.g:
> #define lua_lock(L) pthread_lock(&mutex))?
>
> I would appreciate it if you could shed some light on this question.
>
> Best regards
>
>
> Lua is not multithread ready unless you define lua_lock. But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
> The best way to use Lua in a multithread environment is to create a different lua_State in each thread and let all different lua_States to inter-communicate. I would recomend using LuaLanes for achieving this althought there are a lot of alternatives.
>
> Best
> Victor Bombí
>
> sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

Diego Nehab-4
What deadlocks?

On Thu, Jan 28, 2021 at 08:57 孙世龙 sunshilong <[hidden email]> wrote:
>Let's say you call a lua function, this locked lua state and now nothing can unlock it unless the functions finishes. if that infinite loop calls a C function or >triggers any lua api they call lock function again which lock make it wait itself
>If you want multithreading try something like https://github.com/effil/eiffel
Do you have any idea about how eiffel avoid the deadlocks?
Can I draw the conclusion that the eiffel drops some lock and unlock
operation (comparing to vanilla Lua) to avoid deadlocks?

On Thu, Jan 28, 2021 at 7:40 PM Spar <[hidden email]> wrote:
>
> If you want multithreading try something like https://github.com/effil/effil
> On 28 Jan 2021, 14:35 +0300, 孙世龙 sunshilong <[hidden email]>, wrote:
>
> Spar > Any infinite loop in lua code will lock your mutex forever
> I am shocked to hear that indeed.
> How come it is? Once there is a lua_lock, there will be a lua_unlock.
> Could you please give me a simple example?
>
> On Thu, Jan 28, 2021 at 7:30 PM Spar <[hidden email]> wrote:
>
>
> Any infinite loop in lua code will lock your mutex forever
> On 28 Jan 2021, 14:28 +0300, 孙世龙 sunshilong <[hidden email]>, wrote:
>
> But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
>
> Did you ever encounter deadlock problems when using vanilla Lua?
>
> Best regards
> sunshilong
>
> On Thu, Jan 28, 2021 at 6:22 PM Victor Bombi <[hidden email]> wrote:
>
>
>
> On January 28, 2021 at 10:17 AM 孙世龙 sunshilong <[hidden email]> wrote:
>
>
> Hi, list
>
> In general, the macro named lua_lock is expanded as a
> no-operation(i.e (void)0).
>
> Here is the related code snippet in Lua-5.3.5:
> /*
> ** macros that are executed whenever program enters the Lua core
> ** ('lua_lock') and leaves the core ('lua_unlock')
> */
> #if !defined(lua_lock)
> #define lua_lock(L) ((void) 0)
> #define lua_unlock(L) ((void) 0)
> #endif
> I wonder when I need to provide a self-defined macro (e.g:
> #define lua_lock(L) pthread_lock(&mutex))?
>
> I would appreciate it if you could shed some light on this question.
>
> Best regards
>
>
> Lua is not multithread ready unless you define lua_lock. But providing lua_lock you will get a multithreaded Lua very difficult to use, there will be deadlock problems difficult to solve.
> The best way to use Lua in a multithread environment is to create a different lua_State in each thread and let all different lua_States to inter-communicate. I would recomend using LuaLanes for achieving this althought there are a lot of alternatives.
>
> Best
> Victor Bombí
>
> sunshilong
--
Sent from my phone
Reply | Threaded
Open this post in threaded view
|

Re:When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

suote127
In reply to this post by 孙世龙 sunshilong
When you want to use Lua in muilt-thread,you need to define it.
But this also bring lots of problems,deadlock is one of them,and the performance is,too.
But,in fact,using Lua in muilt-thread itself is just a *BAD* idea

Suote127
Reply | Threaded
Open this post in threaded view
|

Re: When must I provide a self-defined lua_lock macro(e.g: #define lua_lock(L) pthread_lock(&mutex))?

云风 Cloud Wu
In reply to this post by 孙世龙 sunshilong
孙世龙 sunshilong <[hidden email]> 于2021年1月28日周四 下午5:17写道:

>
> Hi, list
>
>     In general, the macro named lua_lock is expanded as a
> no-operation(i.e (void)0).
>
>     Here is the related code snippet in Lua-5.3.5:
> /*
> ** macros that are executed whenever program enters the Lua core
> ** ('lua_lock') and leaves the core ('lua_unlock')
> */
> #if !defined(lua_lock)
> #define lua_lock(L) ((void) 0)
> #define lua_unlock(L) ((void) 0)
> #endif
> I wonder when I need to provide a self-defined macro (e.g:
>  #define lua_lock(L) pthread_lock(&mutex))?
>
> I would appreciate it if you could shed some light on this question.
>

If you want to Lua in multi-threads, you'd better create multiple lua
states, one state per one thread.
I'm working on a small lib these days. It implements an N:M scheduler
in lua.  So that you can run M lua states on N OS threads.
All the states use message channels to inter-communicate.

https://github.com/cloudwu/ltask   (Work in process)

--
http://blog.codingnow.com