public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Libc thread cancelation safety?
@ 2009-03-20 18:52 Dean Anderson
  2009-03-20 20:10 ` Florian Weimer
  2009-03-20 21:06 ` Ian Lance Taylor
  0 siblings, 2 replies; 3+ messages in thread
From: Dean Anderson @ 2009-03-20 18:52 UTC (permalink / raw)
  To: gcc-help

I have noticed that printf in glibc (glibc 2.3.5, i386) is not pthread
cancellation safe, though printf on Solaris is cancellation safe.  
Specifically, canceling a thread in printf leads to deadlock.

What should one expect regarding cancelation safety in standard
libraries?  Is this a bug in glibc?

I haven't done a lot of testing of other functions in the standard
library, so I don't know if this affects more than just printf and
friends.

Working around it is easy, but I'm not sure how many functions one needs
to do this for...'Everything' might be a bit tedious.

static int safe_printf(char *fmt, ...)
{
    va_list ap;
    int cancelstate;
    int ret, status;

    va_start(ap, fmt);
    status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
    ret = vprintf(fmt, ap);
    status = pthread_setcancelstate(cancelstate, &cancelstate);
    return (ret);
}

#define printf safe_printf

I have a test program if anyone is interested.

		--Dean



-- 
Av8 Internet   Prepared to pay a premium for better service?
www.av8.net         faster, more reliable, better service
617 344 9000   


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Libc thread cancelation safety?
  2009-03-20 18:52 Libc thread cancelation safety? Dean Anderson
@ 2009-03-20 20:10 ` Florian Weimer
  2009-03-20 21:06 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Florian Weimer @ 2009-03-20 20:10 UTC (permalink / raw)
  To: Dean Anderson; +Cc: gcc-help

* Dean Anderson:

> I have noticed that printf in glibc (glibc 2.3.5, i386) is not pthread
> cancellation safe, though printf on Solaris is cancellation safe.  
> Specifically, canceling a thread in printf leads to deadlock.
>
> What should one expect regarding cancelation safety in standard
> libraries?  Is this a bug in glibc?

Asynchronous cancellation is inherently unsafe.  I can't find a full
list of functions which may be called from a thread which is subject
to asynchronous cancellation right now, but it is undoubtly very short
and does not include anything which touches the heap or uses the stdio
I/O facilities.  Most bare system calls (read, write, close, pipe)
should be fine, though.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Libc thread cancelation safety?
  2009-03-20 18:52 Libc thread cancelation safety? Dean Anderson
  2009-03-20 20:10 ` Florian Weimer
@ 2009-03-20 21:06 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2009-03-20 21:06 UTC (permalink / raw)
  To: Dean Anderson; +Cc: gcc-help

Dean Anderson <dean@av8.com> writes:

> I have noticed that printf in glibc (glibc 2.3.5, i386) is not pthread
> cancellation safe, though printf on Solaris is cancellation safe.  
> Specifically, canceling a thread in printf leads to deadlock.
>
> What should one expect regarding cancelation safety in standard
> libraries?  Is this a bug in glibc?

This is probably better directed to a glibc mailing list.  See
http://www.gnu.org/software/libc/ .

That said, I doubt that any standard requires that printf be
async-cancel safe.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-03-20 21:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-20 18:52 Libc thread cancelation safety? Dean Anderson
2009-03-20 20:10 ` Florian Weimer
2009-03-20 21:06 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).