Re: Mixing threaded and non-threaded - Mailing list pgsql-hackers

From Scott Lamb
Subject Re: Mixing threaded and non-threaded
Date
Msg-id 401AB740.70605@slamb.org
Whole thread Raw
In response to Re: Mixing threaded and non-threaded  (Bruce Momjian <pgman@candle.pha.pa.us>)
Responses Re: Mixing threaded and non-threaded
Re: Mixing threaded and non-threaded
List pgsql-hackers
Bruce Momjian wrote:
> Scott Lamb wrote:
> 
>>Speaking of async signal-safe functions, pthread_getspecific() isn't 
>>specified to be (and thus PQinSend() and thus 
>>sigpipe_handler_ignore_send()). It's probably okay, but libpq is 
>>technically using undefined behavior according to SUSv3.
> 
> 
> Yikes.  I never suspected pthread_getspecific() would not be signal safe
> because it is already thread safe, but I see the point that it is called
> in the current thread.  Any ideas how to fix this?
> 

A few idea.

When I ran a similar situation in my own code, my approach was to just 
add a comment to make the assumption explicit. It's quite possible the 
standard is just overly conservative. Some specific platforms - 
<http://www.qnx.com/developer/docs/qnx_6.1_docs/neutrino/lib_ref/p/pthread_getspecific.html> 
- mark it as being async signal-safe.

Searching for "pthread_getspecific signal" on google groups turns up a 
bunch of other people who have run into this same problem. One person 
notes that it's definitely not safe on LinuxThreads if you use 
sigaltstack().

If your platform has SA_SIGINFO, you could - in theory - use the 
ucontext argument to see if that thread is in a PostgreSQL operation. 
But I doubt that's portable.

You could just do a pthread_sigmask() before and after the 
pthread_setspecific() to guarantee that no SIGPIPE will arrive on that 
thread in that time. I think it's pretty safe to assume that as long as 
you're not doing a pthread_[gs]etspecific() on that same pthread_key_t, 
it's safe.

There's one thread function that is guaranteed to be async signal-safe - 
sem_post(). (Though apparently older LinuxThreads on x86 fails to meet 
this assumption.) I'm not quite sure what you could do with that, but 
apparently there's something or they wouldn't have gone to the effort of 
making it so.

Scott



pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: Why is it so easy to override -fno-strict-aliasing?
Next
From: Scott Lamb
Date:
Subject: Re: Mixing threaded and non-threaded