Lua: Multithreading, Precompiled Chunks

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

Lua: Multithreading, Precompiled Chunks

Ashley Fryer
Hello all,

A few questions...

Does anyone have experience using Lua in a multithreaded environment?  I 
would like to run several low-priority threads simultaneously, each with 
their own Lua script.  Is there anything in Lua that would break?  I don't 
have any need for interprocess communication, just that nothing Lua-global 
breaks.  Suggestions?

Second, is there some way to run a pre-compiled binary chunk directly from 
memory rather than from a file?  I would like to link some pre-compiled 
chunks directly into my executable, and then have Lua execute them. 
 Possible?

Lastly, is there an archive of the messages from this mailing list 
somewhere?  I'd love to read the old traffic.

Thanks in advance,
Ashley Fryer


Reply | Threaded
Open this post in threaded view
|

Re: Lua: Multithreading, Precompiled Chunks

Luiz Henrique de Figueiredo
>From [hidden email] Sat Mar  7 06:20:30 1998
>
>Does anyone have experience using Lua in a multithreaded environment?  I 
>would like to run several low-priority threads simultaneously, each with 
>their own Lua script.  Is there anything in Lua that would break?  I don't 
>have any need for interprocess communication, just that nothing Lua-global 
>breaks.  Suggestions?

Some people have changed Lua to work in a multithreaded environment.
Lua 3.1 has support for multiple environments, with a single global variable
pointing to the current environment. This should help using Lua in 
a multithreaded environment.

>Second, is there some way to run a pre-compiled binary chunk directly from 
>memory rather than from a file?  I would like to link some pre-compiled 
>chunks directly into my executable, and then have Lua execute them. 
> Possible?

Lua 3.1 has an undocumented function lua_dobuffer that does this.
I think we'll make lua_dobuffer part of the official API when 3.1 is released.
Below, is a simple program that converts binary files to byte arrays that I
think will be useful to you.

>Lastly, is there an archive of the messages from this mailing list 
>somewhere?  I'd love to read the old traffic.

Everything is saved here but I don't know whether the list server is putting
out archives. Try sending 'help' to [hidden email].
If we cannot do this, I'll consider putting a link to it in the web pages.
--lhf

/*
* bin2c.c
* convert binary files to byte arrays
* Luiz Henrique de Figueiredo ([hidden email])
* 12 Jan 98 10:55:22
*/

#include <stdio.h>

void dump(FILE* f, char* name)
{
 int n;
 printf("/* %s */\nstatic unsigned char B_",name);
 for (;*name; name++)
 {
  if (isalnum(*name)) putchar(*name); else putchar('_');
 }
 printf("[]={\n");
 for (n=1;;n++)
 {
  int c=getc(f); 
  if (c==EOF) break;
#if 0
  printf("0x%02x,",c);
#else
  printf("%3u,",c);
#endif
  if (n==20) { putchar('\n'); n=0; }
 }
 printf("\n};\n\n");
}

void fdump(char* fn)
{
 FILE* f=fopen(fn,"rb");			/* must open in binary mode */
 if (f==NULL)
 {
  fprintf(stderr,"bin2c: cannot open ");
  perror(fn);
  exit(1);
 }
 else
 {
  dump(f,fn);
  fclose(f);
 }
}

void main(int argc, char* argv[])
{
 if (argc<2)
 {
  dump(stdin,"stdin");
 }
 else
 {
  while (--argc>0)
   fdump(*++argv);
 }
}

Reply | Threaded
Open this post in threaded view
|

Re: Lua: Multithreading, Precompiled Chunks

DASWorks
In reply to this post by Ashley Fryer
Hello Everyone:

In a message dated 3/7/98 8:50:05 AM, you wrote:

>>Does anyone have experience using Lua in a multithreaded environment?  I 
>>would like to run several low-priority threads simultaneously, each with 
>>their own Lua script.  Is there anything in Lua that would break?  I don't 
>>have any need for interprocess communication, just that nothing Lua-global 
>>breaks.  Suggestions?
>
>Some people have changed Lua to work in a multithreaded environment.
>Lua 3.1 has support for multiple environments, with a single global variable
>pointing to the current environment. This should help using Lua in 
>a multithreaded environment.

If anyone made the multithreaded changes,  would they be willing to discuss
them here or privately?  Perhaps share some code?

Regards,

David

Reply | Threaded
Open this post in threaded view
|

Re: Lua: Multithreading, Precompiled Chunks

Carlos Cassino
In reply to this post by Ashley Fryer
Hi.

I made the multithreaded changes in Lua. I just splited the
main data structure that Lua uses to save the state in two
parts: one global (common to all threads) and one specific to
each thread (stack, basically).

The major change is in the garbage collector: mark&sweep must
traverse all stacks and I decided to stop all threads at this
moment. This synchronization implies that all threads must
check for GC to enable it to occur. It's really a drawback
but I haven't found a better way...

It's implemented using pthreads library and tested in Linux
and Solaris platforms only. I can make the source available.

The following functions were added to the C API:

void lua_openthread(void)
- to be called from a new thread, creates a new LuaThread
  and associates it to the calling thread.

void lua_threadfunction(lua_Object f, lua_Object p)
- starts a new LuaThread by executing function f with
  parameter p.

In Lua the following functions were added:

threadfunction
- behaves like the C function lua_threadfunction.

createmutex
- receives nothing and returns a new mutex.

lockmutex
- receives a mutex and locks it. Returns nothing.

unlockmutex
- receives a mutex an unlocks it. Returns nothing.

destroymutex
- receives a mutex and destroys it. Returns nothing.

Regards,
-- cassino

Reply | Threaded
Open this post in threaded view
|

Re: Lua: Multithreading, Precompiled Chunks

Steve Dekorte-2
>I made the multithreaded changes in Lua. I just splited the
>main data structure that Lua uses to save the state in two
>parts: one global (common to all threads) and one specific to
>each thread (stack, basically).
>
>The major change is in the garbage collector: mark&sweep must
>traverse all stacks and I decided to stop all threads at this
>moment. This synchronization implies that all threads must
>check for GC to enable it to occur. It's really a drawback
>but I haven't found a better way...
>
>It's implemented using pthreads library and tested in Linux
>and Solaris platforms only. I can make the source available.

Cool. Is this available via ftp? I'd like to try it on Rhapsody.
Maybe someone could add a link to it on the Lua pages.

Steve

Reply | Threaded
Open this post in threaded view
|

Re: Lua: Multithreading, Precompiled Chunks

Carlos Cassino
In reply to this post by Ashley Fryer
Hi Steve.

You can download LuaMT from www.tecgraf.puc-rio.br/~cassino
I changed the names of the executables and the libraries
to distribute it. I hope it can be useful.

Regards,
-- cassino