From owner-freebsd-threads@FreeBSD.ORG Sat Aug 14 14:47:19 2010 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id DCB3D1065695; Sat, 14 Aug 2010 14:47:19 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.zoral.com.ua (mx0.zoral.com.ua [91.193.166.200]) by mx1.freebsd.org (Postfix) with ESMTP id 763D68FC1A; Sat, 14 Aug 2010 14:47:19 +0000 (UTC) Received: from deviant.kiev.zoral.com.ua (root@deviant.kiev.zoral.com.ua [10.1.1.148]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id o7EElFLB067541 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 14 Aug 2010 17:47:15 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.4/8.14.4) with ESMTP id o7EElFZn016556; Sat, 14 Aug 2010 17:47:15 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.4/8.14.4/Submit) id o7EElFWx016555; Sat, 14 Aug 2010 17:47:15 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Sat, 14 Aug 2010 17:47:15 +0300 From: Kostik Belousov To: David Xu Message-ID: <20100814144715.GB2396@deviant.kiev.zoral.com.ua> References: <20100811204758.GQ2396@deviant.kiev.zoral.com.ua> <4C63D42D.8040606@freebsd.org> <20100812083006.GR2396@deviant.kiev.zoral.com.ua> <4C642E9B.8000300@freebsd.org> <20100812093353.GS2396@deviant.kiev.zoral.com.ua> <4C650D0F.9060905@freebsd.org> <4C650F27.1000305@freebsd.org> <20100813141402.GW2396@deviant.kiev.zoral.com.ua> <4C65E0FE.2030803@freebsd.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="j+D14l8Ki1YJdzYp" Content-Disposition: inline In-Reply-To: <4C65E0FE.2030803@freebsd.org> User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: clamav-milter 0.95.2 at skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-2.2 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_50, DNS_FROM_OPENWHOIS autolearn=no version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua Cc: freebsd-threads@freebsd.org Subject: Re: PTHREAD_CANCEL_DEFERRED X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 14 Aug 2010 14:47:19 -0000 --j+D14l8Ki1YJdzYp Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Aug 14, 2010 at 08:19:10AM +0800, David Xu wrote: > Kostik Belousov wrote: > >On Fri, Aug 13, 2010 at 09:23:51AM +0000, David Xu wrote: > > =20 > >>David Xu wrote: > >> > >> =20 > >>>>>if you don't call testcancel() in close() stub like current libthr d= id, > >>>>>B won't response to the cancel request, you lost the race. > >>>>> =20 > >>>>This situation should be handled by my proposal, since SIGCANCEL is > >>>>delivered only > >>>>- at the syscall entry point > >>>>- at the syscall premature return > >>>>Userspace would not get SIGCANCEL at time of [1], instead, signal will > >>>>be delivered at [2]. > >>>> =20 > >>>kernel may don't know if the syscall is cancelable, because it depends > >>>on usage, if the close() syscall is used by fclose(), then the syscall > >>>is not cancellation point, libc avoids this by using _close(), > >>>and libthr does not override it. if kernel knows when a thread is at > >>>cancellation point, then it needs another syscall to set and unset > >>>the flag, but that's too expensive and in practical it is not > >>>acceptable. > >>> =20 > >The kernel only decides whether to process SIGCANCEL specially. The > >decision about the cancel point is still at the hands of the threading > >library. The delivered SIGCANCEL goes through the same checks of > >eligibility for cancellation as before. But it is only delivered now at > >potential cancellation points for deferred case. > > > >Please see the patch at > >http://people.freebsd.org/~kib//misc/cancel_defer.1.patch > >for the proof of concept prototype. > > > > =20 > >>a bit out of topic, I also think that thread cancellation is not > >>better than a simple signal, because it does not return to caller > >>and force you to push and pop somethings, it may also be incompatible > >>with some language's exception handling, why does not just use > >>signal to interrupt syscall and let caller to check if the thread > >>should exit, the UNIX is quite good at this. > >> =20 > > > >This still does not give the answer of whether the syscall was executed. > >I have to check for %pc to see when the signal was delivered. Also, to > >be able to use signal in the way you suggested, I need a signal handler > >installed. This is very inconvenient from the library. > > > >BTW, I looked at the Solaris cancellation(5) man page, and it seems that > >Solaris implements the proper (from my POV) deferred cancellation: > >=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > >When cancellation is deferred (the default case), cancellation occurs > >only within the scope of a function defined as a cancellation point > >(after the function is called and before the function returns). > >=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > =20 > while the above patch may work, there could have exceptions: >=20 > just before syscall close() enters kernel, a user signal is received,=20 > and the user > signal handler calls a syscall which happens to be cancellation point, I= =20 > saw some > programs print a log in signal handler, and thread is canceled, > e.g write(2, msg, strlen(msg)) to log a message, > close() syscall is still not called, though it is arguable that if this= =20 > signal handler > code is legal. Sorry, I cannot fully understand the point. Do you mean that close() could not be called if signal handler calls function that is a cancelation point ? This seems to be ok, my rush is against the situation where the state-changing call is done, but I cannot determine was it actually done or not, due to cancellation. >=20 > thread suspending in libthr uses SIGCANCEL to check suspending state, > the code needs to be adjusted, it seems I have to use another signal. Missed this, thank you for pointing it out. Updated patch is at http://people.freebsd.org/~kib//misc/cancel_defer.2.patch --j+D14l8Ki1YJdzYp Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (FreeBSD) iEYEARECAAYFAkxmrHMACgkQC3+MBN1Mb4h/rwCg0/JGSkbPHyP21GvRu79gx2jX 6j4Anj4HhGPMWdh3KPIf4gWTKxGhd6OP =c8Xf -----END PGP SIGNATURE----- --j+D14l8Ki1YJdzYp--