public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Florian Weimer <fweimer@redhat.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 01/10] nptl: Perform signal initialization upon pthread_create
Date: Thu, 20 May 2021 16:57:19 -0300	[thread overview]
Message-ID: <46b68c50-e1d9-1450-d3ea-a302b86e2949@linaro.org> (raw)
In-Reply-To: <87bl95jiqs.fsf@oldenburg.str.redhat.com>



On 20/05/2021 16:41, Florian Weimer wrote:
> * Adhemerval Zanella:
> 
>>>  int
>>>  __pthread_cancel (pthread_t th)
>>> @@ -72,14 +129,23 @@ __pthread_cancel (pthread_t th)
>>>  						    oldval))
>>>  	    goto again;
>>>  
>>> -	  /* The cancellation handler will take care of marking the
>>> -	     thread as canceled.  */
>>> -	  pid_t pid = __getpid ();
>>> -
>>> -	  int val = INTERNAL_SYSCALL_CALL (tgkill, pid, pd->tid,
>>> -					   SIGCANCEL);
>>> -	  if (INTERNAL_SYSCALL_ERROR_P (val))
>>> -	    result = INTERNAL_SYSCALL_ERRNO (val);
>>> +	  if (pd == THREAD_SELF)
>>> +	    /* This is not merely an optimization: An application may
>>> +	       call pthread_cancel (pthread_self ()) without calling
>>> +	       pthread_create, so the signal handler may not have been
>>> +	       set up for a self-cancel.  */
>>> +	    sigcancel_handler ();
>>
>> I think it would be simple to just call __pthread_exit (PTHREAD_CANCELED)
>> here, it won't require to split the cancellation handler, it already
>> unwind if cancel state is enabled and asynchronous, and it does not
>> require add another PTHREAD_STATIC_FN_REQUIRE hack. 
>>
>> It would require an extra __libc_unwind_link_get call, but I think we
>> can optimize it later (I am working on a patch to simplify it).
> 
> It would be correct, I think.  pthread_cancel is not a cancellation
> point.
> 
> #include <stdio.h>
> #include <pthread.h>
> 
> int
> main (void)
> {
>   pthread_cancel (pthread_self ());
>   puts ("about to exit");
> }
> 
> This should print “about to exit”.

Yes, this is essentially sysdeps/pthread/tst-cancel-self.c.

> 
>>> +/* This performs the initialization necessary when going from
>>> +   single-threaded to multi-threaded mode for the first time.  */
>>> +static void
>>> +late_init (void)
>>> +{
>>> +  struct sigaction sa;
>>> +  __sigemptyset (&sa.sa_mask);
>>> +
>>> +  /* Install the cancellation signal handler (in static builds only if
>>> +     pthread_cancel has been linked in).  If for some reason we cannot
>>> +     install the handler we do not abort.  Maybe we should, but it is
>>> +     only asynchronous cancellation which is affected.  */
>>> +#ifndef SHARED
>>> +  extern __typeof (__nptl_sigcancel_handler) __nptl_sigcancel_handler
>>> +    __attribute__ ((weak));
>>> +  if (__nptl_sigcancel_handler != NULL)
>>> +#endif
>>
>> This weak symbol can be avoided if we move the cancellation setup
>> on pthread_cancel instead.  I still think this is best approach,
>> it disentangle the cancellation handling.
> 
> But then we either have to introduce yet another global flag or install
> the signal handler unconditionally before every cancel operation.  I do
> not think this results in a simplification.

The flag will be just a static bool or int only define on pthread_cancel,
something like:

  int
  __pthread_cancel (pthread_t th) 
  {
    [...]
    static int init = 0;
    if (atomic_load_relaxed (&init) == 0)
      {
        install_sighandler ();
        init = 1;
      }
    [...]
  }

Bu the main advantage is to move the cancellation code logically when
it is actually used, and it is small improvement on both static
linking (since the static code will be used solely is cancellation is
used) and on runtime (since sigaction will be set only if pthread_cancel
is called).

  reply	other threads:[~2021-05-20 19:57 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-18 14:24 [PATCH 00/10] nptl: Complete libpthread removal Florian Weimer
2021-05-18 14:24 ` [PATCH 01/10] nptl: Perform signal initialization upon pthread_create Florian Weimer
2021-05-20 19:15   ` Adhemerval Zanella
2021-05-20 19:41     ` Florian Weimer
2021-05-20 19:57       ` Adhemerval Zanella [this message]
2021-05-20 20:05         ` Florian Weimer
2021-05-20 20:32           ` Adhemerval Zanella
2021-05-21  9:58             ` Florian Weimer
2021-05-21 11:31               ` Adhemerval Zanella
2021-05-21 12:40                 ` Adhemerval Zanella
2021-05-18 14:24 ` [PATCH 02/10] nptl: Eliminate the __static_tls_size, __static_tls_align_m1 variables Florian Weimer
2021-05-18 14:25 ` [PATCH 03/10] nptl: Move semi-public __pthread_get_minstack symbol into libc Florian Weimer
2021-05-18 14:25 ` [PATCH 04/10] elf: Use custom NODELETE DSO for tst-dlopenfail, tst-dlopenfail-2 Florian Weimer
2021-05-20 20:36   ` Adhemerval Zanella
2021-05-18 14:25 ` [PATCH 05/10] nptl: Move pthread_create, thrd_create into libc Florian Weimer
2021-05-20 20:44   ` Adhemerval Zanella
2021-05-18 14:25 ` [PATCH 06/10] nptl: Remove unused __libc_pthread_init function Florian Weimer
2021-05-18 14:49   ` Andreas Schwab
2021-05-18 14:25 ` [PATCH 07/10] nptl: Remove remaining code from libpthread Florian Weimer
2021-05-20 20:49   ` Adhemerval Zanella
2021-05-18 14:25 ` [PATCH 08/10] elf: Do not load libpthread for PTHREAD_IN_LIBC Florian Weimer
2021-05-20 20:53   ` Adhemerval Zanella
2021-05-21 19:15     ` Florian Weimer
2021-05-18 14:25 ` [PATCH 09/10] elf: Add facility to create stub DSOs in elf/stub-dsos Florian Weimer
2021-05-24 18:24   ` Adhemerval Zanella
2021-05-24 18:25     ` Adhemerval Zanella
2021-05-18 14:25 ` [PATCH 10/10] nptl: Stop building libpthread Florian Weimer
2021-05-18 14:56 ` [PATCH 00/10] nptl: Complete libpthread removal Andreas Schwab
2021-05-18 15:04   ` Florian Weimer
2021-05-18 15:26     ` Andreas Schwab
2021-05-18 15:51       ` Florian Weimer
2021-05-18 16:27         ` Andreas Schwab
2021-05-18 16:31           ` Florian Weimer
2021-05-18 16:47             ` Andreas Schwab
2021-05-20 13:27               ` Florian Weimer
2021-05-20 13:50                 ` Andreas Schwab
2021-05-20 13:54                   ` Florian Weimer
2021-05-20 14:01                     ` Andreas Schwab
2021-05-20 15:09                     ` H.J. Lu
2021-05-20 15:13                       ` Florian Weimer
2021-05-20 15:17                       ` Andreas Schwab
2021-05-20 15:35                         ` H.J. Lu
2021-05-20 15:39                           ` Florian Weimer
2021-05-20 15:57                             ` H.J. Lu
2021-05-19 11:57 ` Szabolcs Nagy
2021-05-19 12:35   ` Florian Weimer
2021-05-19 13:14     ` Szabolcs Nagy

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=46b68c50-e1d9-1450-d3ea-a302b86e2949@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.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).