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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 612103973100 for ; Thu, 20 May 2021 20:05:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 612103973100 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-321-jbm782Y8N5CRC1s4GdJ6FA-1; Thu, 20 May 2021 16:05:52 -0400 X-MC-Unique: jbm782Y8N5CRC1s4GdJ6FA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CA1D2803633; Thu, 20 May 2021 20:05:51 +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 F16625D9CC; Thu, 20 May 2021 20:05:50 +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: <87bl95jiqs.fsf@oldenburg.str.redhat.com> <46b68c50-e1d9-1450-d3ea-a302b86e2949@linaro.org> Date: Thu, 20 May 2021 22:05:48 +0200 In-Reply-To: <46b68c50-e1d9-1450-d3ea-a302b86e2949@linaro.org> (Adhemerval Zanella's message of "Thu, 20 May 2021 16:57:19 -0300") Message-ID: <87wnrti31f.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.14 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 20:05:56 -0000 * Adhemerval Zanella: > On 20/05/2021 16:41, Florian Weimer wrote: >> * Adhemerval Zanella: >>=20 >>>> 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_CANCELE= D) >>> 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). >>=20 >> It would be correct, I think. pthread_cancel is not a cancellation >> point. >>=20 >> #include >> #include >>=20 >> int >> main (void) >> { >> pthread_cancel (pthread_self ()); >> puts ("about to exit"); >> } >>=20 >> This should print =E2=80=9Cabout to exit=E2=80=9D. > > Yes, this is essentially sysdeps/pthread/tst-cancel-self.c. But surely this won't work if we call pthread_exit (PTHREAD_CANCELED) because that exits the main thread at that point, and not at the next cancellation point. >>>> +/* 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 i= f >>>> + pthread_cancel has been linked in). If for some reason we canno= t >>>> + 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. >>=20 >> 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)=20 > { > [...] > static int init =3D 0; > if (atomic_load_relaxed (&init) =3D=3D 0) > { > install_sighandler (); > init =3D 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). Okay, I can prepare a version along these lines. But in general, I consider less data and fewer conditionals an improvement. Thanks, Florian