From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 65A423854816 for ; Thu, 20 May 2021 19:41:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 65A423854816 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-440-jPxDtgN6PdCfZlqYXuLkqQ-1; Thu, 20 May 2021 15:41:19 -0400 X-MC-Unique: jPxDtgN6PdCfZlqYXuLkqQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 90576818400; Thu, 20 May 2021 19:41:18 +0000 (UTC) Received: from oldenburg.str.redhat.com (ovpn-112-137.ams2.redhat.com [10.36.112.137]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B64551964B; Thu, 20 May 2021 19:41:17 +0000 (UTC) From: Florian Weimer To: Adhemerval Zanella Cc: libc-alpha@sourceware.org Subject: Re: [PATCH 01/10] nptl: Perform signal initialization upon pthread_create References: Date: Thu, 20 May 2021 21:41:15 +0200 In-Reply-To: (Adhemerval Zanella's message of "Thu, 20 May 2021 16:15:32 -0300") Message-ID: <87bl95jiqs.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 May 2021 19:41:22 -0000 * Adhemerval Zanella: >> int >> __pthread_cancel (pthread_t th) >> @@ -72,14 +129,23 @@ __pthread_cancel (pthread_t th) >> =09=09=09=09=09=09 oldval)) >> =09 goto again; >> =20 >> -=09 /* The cancellation handler will take care of marking the >> -=09 thread as canceled. */ >> -=09 pid_t pid =3D __getpid (); >> - >> -=09 int val =3D INTERNAL_SYSCALL_CALL (tgkill, pid, pd->tid, >> -=09=09=09=09=09 SIGCANCEL); >> -=09 if (INTERNAL_SYSCALL_ERROR_P (val)) >> -=09 result =3D INTERNAL_SYSCALL_ERRNO (val); >> +=09 if (pd =3D=3D THREAD_SELF) >> +=09 /* This is not merely an optimization: An application may >> +=09 call pthread_cancel (pthread_self ()) without calling >> +=09 pthread_create, so the signal handler may not have been >> +=09 set up for a self-cancel. */ >> +=09 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.=20 > > 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 #include int main (void) { pthread_cancel (pthread_self ()); puts ("about to exit"); } This should print =E2=80=9Cabout to exit=E2=80=9D. >> +/* 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 !=3D 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. Thanks, Florian