Thread: Advice needed concerning Win32 signals

Advice needed concerning Win32 signals

From
Thomas Hallgren
Date:
Hi,
I'm in the process of securing the PL/Java signal handling routines. 
Since a multi-threaded JVM is running and a signal can occur at any time 
in any thread, I need to trap some of them and make sure that they are 
executed in the main thread. Using Posix signals, this is easy. Here's a 
sample handler:

static void pljavaStatementCancelHandler(int signum)
{   if(pthread_self() == mainThread)      /*       * Call original handler       */
StatementCancelHandler(signum);  else      /*       * Dispatch to main thread.       */       pthread_kill(mainThread,
signum);/* Send it to main thread */
 
}

The mainThread was initialized when the handler was first registered.
From looking at the PostgreSQL signal handling routines for Win32, I 
realize that several threads are involved already. Perhaps all signals 
are indeed dispatched to the main thread already? I couldn't really 
figure it out. I need help from someone who knows more about this.

Regards,
Thomas Hallgren



Re: Advice needed concerning Win32 signals

From
"Magnus Hagander"
Date:
> Hi,
> I'm in the process of securing the PL/Java signal handling routines.
> Since a multi-threaded JVM is running and a signal can occur
> at any time in any thread, I need to trap some of them and
> make sure that they are executed in the main thread. Using
> Posix signals, this is easy. Here's a sample handler:
>
> static void pljavaStatementCancelHandler(int signum) {
>     if(pthread_self() == mainThread)
>        /*
>         * Call original handler
>         */
>         StatementCancelHandler(signum);
>     else
>        /*
>         * Dispatch to main thread.
>         */
>         pthread_kill(mainThread, signum); /* Send it to main
> thread */ }
>
> The mainThread was initialized when the handler was first registered.
>
>  From looking at the PostgreSQL signal handling routines for
> Win32, I realize that several threads are involved already.
> Perhaps all signals are indeed dispatched to the main thread
> already? I couldn't really figure it out. I need help from
> someone who knows more about this.

The win32 system itself doesn't have the concept of signals. Therefor,
the only signals being delivered are those delivered internally between
the postgresql processes (including those sent manually through pg_ctl).

These "fake signals" are dispatched on the main thread. They are
received by a worker thread, which will queue them up and set a flag,
and the main thread then polls this queue and delivers the actual
signal.

I have *no idea* how pthreads does this. pthread_kill() probably does
not know about *our* signal emulation layer, but somehow has it's own.
(as pthreads definitly aren't used in the backend, but I assume you're
somehow using them for pl/java anyway). Or perhaps that's just not
necessary and the example was for unix only?


//Magnus


Re: Advice needed concerning Win32 signals

From
Thomas Hallgren
Date:
mha@sollentuna.net wrote:
>> Hi,
>> I'm in the process of securing the PL/Java signal handling routines. 
>> Since a multi-threaded JVM is running and a signal can occur 
>> at any time in any thread, I need to trap some of them and 
>> make sure that they are executed in the main thread. Using 
>> Posix signals, this is easy. Here's a sample handler:
>>
>> static void pljavaStatementCancelHandler(int signum) {
>>     if(pthread_self() == mainThread)
>>        /*
>>         * Call original handler
>>         */
>>         StatementCancelHandler(signum);
>>     else
>>        /*
>>         * Dispatch to main thread.
>>         */
>>         pthread_kill(mainThread, signum); /* Send it to main 
>> thread */ }
>>
>> The mainThread was initialized when the handler was first registered.
>>
>>  From looking at the PostgreSQL signal handling routines for 
>> Win32, I realize that several threads are involved already. 
>> Perhaps all signals are indeed dispatched to the main thread 
>> already? I couldn't really figure it out. I need help from 
>> someone who knows more about this.
>>     
>
> The win32 system itself doesn't have the concept of signals. Therefor,
> the only signals being delivered are those delivered internally between
> the postgresql processes (including those sent manually through pg_ctl).
>
> These "fake signals" are dispatched on the main thread. They are
> received by a worker thread, which will queue them up and set a flag,
> and the main thread then polls this queue and delivers the actual
> signal.
>
> I have *no idea* how pthreads does this. pthread_kill() probably does
> not know about *our* signal emulation layer, but somehow has it's own.
> (as pthreads definitly aren't used in the backend, but I assume you're
> somehow using them for pl/java anyway). Or perhaps that's just not
> necessary and the example was for unix only?
>   
Yes, the example is for Posix compliant systems only and it assumes that 
the JVM itself base its threading on pthreads.

How is a PostgreSQL process terminated on Windows? Or rather, how is the 
concept of die() and quickdie() implemented if it's not based on signals?

Regards,
Thomas Hallgren



Re: Advice needed concerning Win32 signals

From
Thomas Hallgren
Date:
mha@sollentuna.net wrote:
> These "fake signals" are dispatched on the main thread. They are
> received by a worker thread, which will queue them up and set a flag,
> and the main thread then polls this queue and delivers the actual
> signal.
>   
OK. Now I understand. On Windows I don't need to do anything at all. 
There's no risk of signals in other threads then main.

- thomas