slang-users mailing list

[2001 Date Index] [2001 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: is this safe ?


Richard van Zon <rvanzon@xxxxxxxxxxxx> wrote:
>I am allways calling the main-function in my C-program.
>The timer-function is also called by my C-program, and is actually hooked to
>a timer.

This may not always work.  Currently, the library is not thread safe.
V2 will support multiple threads.

Is 'main' always doing something?  That is, 

    define main ()
    {
       while (1)
         {
	 }
    }

is always doing something, whereas

    define main ()
    {
       while (1)
         {
	     sleep (1);
	 }
    }

is sleeping most of the time.  If your main is always causing the
interpreter to loop, and not sleeping or waiting for the user to do
something, then you can do this:


    #include <slang.h>
    
    static int Interrupt_SLang;
    static void interrupt_hook (void)
    {
       if (Interrupt_SLang == 0)
         return;
       Interrupt_SLang = 0;
       /* call slang timer function */
         .
	 .
    }
    
    static void c_timer_function (int sig)
    {
       Interrupt_SLang = 1;
    }
    
    int main (int argc, char **argv)
    {
       .
       .
       SLang_Interrupt = interrupt_hook;
       .
       .
    }
    
While the interpreter is executing code, it will call the function
specified by SLang_Interrupt, and it will do so in a safe manner.
Of course if the interpreter is sleeping, or calling an intrinsic that
us waiting on something, it will not call the SLang_Interrupt function
until the interpreter starts executing slang code.

In any case, I think that this is a safer approach than what you are
currently using.

--John


[2001 date index] [2001 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]