guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Threads with foreign functions


From: Ola Leifler
Subject: Re: Threads with foreign functions
Date: Thu, 30 Oct 2003 07:37:41 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031020 Debian/1.5-1

Joshua Judson Rosen wrote:

On Wed, Oct 29, 2003 at 11:40:13AM +0100, Ola Leifler wrote:
Hi!

I'm trying to control an application written in C (LGeneral by Michael Speck) through Guile by using some SWIG-generated bindings that I load from a dynamic library. This library is created by libtool automagically.

However, when I try to use the main function for starting the game from Guile, call-with-new-thread does not return though I can see that the new thread is actually running. Any ideas?

Last I heard, guile's multithreading was cooperative and
implemented in one of guile's libraries, *not* an interface to
the ambient systems's (posix) multithreading. It looks like this is
still the case.

If you put something uncooperative into one of your cooperative
threads..., uncooperability is contagious--something about C-level
calls being atomic. Maybe if you make your C-routine periodically call
guile's `scm_yield'? Or you could make it into a state-machine....

I thought that in order to share the same global datastuctures you couldn't use Guile in that way that you called Guile directly from the C-code if the interface to C was generated by SWIG and put into a dynamic library?

I thought you had to make a dynamic/shared library of your program and let Guile load the program and call an init-function in the library to start the program. Isn't that so?

However, there's also one other question regarding Guile, which may or may not be coupled to my use of SWIG, I'm not sure.

I've tried two approaches to letting a Guile process change the values of global parameters in a C-program that is running in parallel. Perhaps there's a canonical way of doing this since it seems like a basic need for a scripting language interfacing an existing application but I haven't found any useful informaton on it, at least not if you're using SWIG. First, I tried to start Guile from a C-program in one C-thread while the other C-thread printed some data repeatedly that came from this global variable. Guile could not successfully change this parameter so that the C-thread saw the change. See code snippet below. I thought this was because Guile loaded a shared library in order to get access to the C interface and thus loaded a *copy* of all the functions/variables that were already accessible to the C program.

extern tortoise *t;
void inner_main(int argc, char *argv[]){
   pid_t pid;
   int exit_val;
   t = make_tortoise();
   switch(pid = fork()) {
   case -1:
       perror("fork");  /* something went wrong */
           exit(1);         /* parent exits */
   case 0:
       /* Child process, start Guile */
           /* SCM_init();*/
       gh_eval_file("/home/ola/slask/tortoise-guile/guile_init.ss");
       gh_repl(argc, argv);
       exit(0);
   default:
       /* Print the location */
       while ( t->pen_down ) {
       fprintf(stderr,"t:s location: [%f,%f]\n",
              t->current_x,
              t->current_y);
       fflush(stderr);
       sleep(5);
       }
       wait(&exit_val);
       printf("Bye!\n");
   }
}

The other approach was to let Guile start and then spawn a C-thread by having a C-function which only spawns a thread and exits. The idea was that this thread should be running in the same loaded library that Guile was using, and that the thread would get affected if Guile changed the value of a global variable. This did not happen..

/Ola






reply via email to

[Prev in Thread] Current Thread [Next in Thread]