public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Richard Weinberger <richard.weinberger@gmail.com>
To: Florian Weimer <fw@deneb.enyo.de>
Cc: Richard Weinberger via Libc-help <libc-help@sourceware.org>,
	linux-man <linux-man@vger.kernel.org>
Subject: Re: Clarification of pthread_cleanup_push() needed
Date: Wed, 22 Apr 2020 00:48:28 +0200	[thread overview]
Message-ID: <CAFLxGvzGyerUxinHxL=Rpqnm6wb3uTq+gjvL=_af4QzkOx3GHQ@mail.gmail.com> (raw)
In-Reply-To: <87v9lso5d1.fsf@mid.deneb.enyo.de>

On Wed, Apr 22, 2020 at 12:12 AM Florian Weimer <fw@deneb.enyo.de> wrote:
>
> * Richard Weinberger:
>
> > On Tue, Apr 21, 2020 at 11:41 PM Florian Weimer <fw@deneb.enyo.de> wrote:
> >>
> >> * Richard Weinberger via Libc-help:
> >>
> >> > But NPTL implements thread cancellation with signals, if I'm not
> >> > completely mistaken the cleanup routine will run in signal context
> >> > then.
> >>
> >> Deferred cancellation only occurs in signal context if the
> >> cancellation is acted upon from within a signal handler.  For the
> >> signal handler case, whether cancellation handlers are restricted to
> >> async-signal-safe function calls depends on the type of signal (some
> >> are synchronous, not asynchronous) and what is interrupted by the
> >> signal (for asynchronous signals).
> >>
> >> Asynchronous cancellation has even more constraints than asynchronous
> >> signal safety, but it is rarely used.
> >
> > I should have noted that I'm using asynchronous cancellation.
> > Which constraints are these?
>
> See pthread_setcanceltype(3):
>
>   Functions that can be safely asynchronously canceled are called
>   async-cancel-safe functions.  POSIX.1-2001 and POSIX.1-2008 require
>   only that pthread_cancel(3), pthread_setcancelstate(), and
>   pthread_setcanceltype() be async-cancel-safe.  In general, other
>   library functions can't be safely called from an asynchronously
>   cancelable thread.
>
> The manual pages and the glibc manual also contain information about
> AC-safety, but you cannot rely on them.  They depend on implementation
> details which may change within release branches.

Well, this manpage does not talk much about the cleanup routine, except for
"Consequently, clean-up handlers cease to be useful", which is clear as mud.

I'm well aware that async cancel is not nice at all and my code can be
interrupted
at any time.

Back to the cleanup routine, with asynchronous cancellation:
- it must not use any function which might got interrupted by cancel,
- it may only use async-signal-safe function, except long jumps.

Is this correct?

To give you some background, I'm porting an application to NPTL which comes
from an operating system where cancelling threads and automatic
cleanup is the way to go.
Completely rewriting the application is not an option, so I need to
figure how far I get with
pthread_cancel() and friends.

-- 
Thanks,
//richard

  reply	other threads:[~2020-04-21 22:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-21 20:35 Richard Weinberger
2020-04-21 21:41 ` Florian Weimer
2020-04-21 22:04   ` Richard Weinberger
2020-04-21 22:12     ` Florian Weimer
2020-04-21 22:48       ` Richard Weinberger [this message]
2020-04-21 23:43         ` Florian Weimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFLxGvzGyerUxinHxL=Rpqnm6wb3uTq+gjvL=_af4QzkOx3GHQ@mail.gmail.com' \
    --to=richard.weinberger@gmail.com \
    --cc=fw@deneb.enyo.de \
    --cc=libc-help@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).