From owner-freebsd-hackers@FreeBSD.ORG Wed Jan 16 05:12:24 2013 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id D5C00684 for ; Wed, 16 Jan 2013 05:12:24 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) by mx1.freebsd.org (Postfix) with ESMTP id 3187B861 for ; Wed, 16 Jan 2013 05:12:24 +0000 (UTC) Received: from tom.home (kostik@localhost [127.0.0.1]) by kib.kiev.ua (8.14.6/8.14.6) with ESMTP id r0G5CBJm036456; Wed, 16 Jan 2013 07:12:11 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.7.4 kib.kiev.ua r0G5CBJm036456 Received: (from kostik@localhost) by tom.home (8.14.6/8.14.6/Submit) id r0G5CAQc036455; Wed, 16 Jan 2013 07:12:10 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 16 Jan 2013 07:12:10 +0200 From: Konstantin Belousov To: Trent Nelson Subject: Re: Getting the current thread ID without a syscall? Message-ID: <20130116051210.GF2522@kib.kiev.ua> References: <20130115205403.GA52904@snakebite.org> <20130115211641.GC2522@kib.kiev.ua> <20130115213513.GA53047@snakebite.org> <20130115214332.GE2522@kib.kiev.ua> <50F5D82C.7070400@mu.org> <1358289221.32417.129.camel@revolution.hippie.lan> <20130115230330.GA53211@snakebite.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="Il7n/DHsA0sMLmDu" Content-Disposition: inline In-Reply-To: <20130115230330.GA53211@snakebite.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on tom.home Cc: Ian Lepore , Alfred Perlstein , "freebsd-hackers@freebsd.org" X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Jan 2013 05:12:24 -0000 --Il7n/DHsA0sMLmDu Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Jan 15, 2013 at 06:03:30PM -0500, Trent Nelson wrote: > On Tue, Jan 15, 2013 at 02:33:41PM -0800, Ian Lepore wrote: > > On Tue, 2013-01-15 at 14:29 -0800, Alfred Perlstein wrote: > > > On 1/15/13 1:43 PM, Konstantin Belousov wrote: > > > > On Tue, Jan 15, 2013 at 04:35:14PM -0500, Trent Nelson wrote: > > > >> > > > >> Luckily it's for an open source project (Python), so recompil= ation > > > >> isn't a big deal. (I also check the intrinsic result versus = the > > > >> syscall result during startup to verify the same ID is return= ed, > > > >> falling back to the syscall by default.) > > > > For you, may be. For your users, it definitely will be a problem. > > > > And worse, the problem will be blamed on the operating system and n= ot > > > > to the broken application. > > > > > > > Anything we can do to avoid this would be best. > > >=20 > > > The reason is that we are still dealing with an "optimization" that p= erl=20 > > > did, it reached inside of the opaque struct FILE to "do nasty things"= =2E =20 > > > Now it is very difficult for us to fix "struct FILE". > > >=20 > > > We are still paying for this years later. > > >=20 > > > Any way we can make this a supported interface? > > >=20 > > > -Alfred > >=20 > > Re-reading the original question, I've got to ask why pthread_self() > > isn't the right answer? The requirement wasn't "I need to know what the > > OS calls me" it was "I need a unique ID per thread within a process." >=20 > The identity check is performed hundreds of times per second. The > overhead of (Py_MainThreadId =3D=3D __readgsdword(0x48) ? A() : B()) = is > negligible -- I can't say the same for a system/function call. >=20 > (I'm experimenting with an idea I had to parallelize Python such > that it can exploit all cores without impeding the performance > of normal single-threaded execution (like previous-GIL-removal > attempts and STM). It's very promising so far -- presuming we > can get the current thread ID in a couple of instructions. If > not, single-threaded performance suffers too much.) If the only thing you ever need is to get a unique handle for the current thread, without the requirement that it corresponds to any other identifier, everything becomes much easier. On amd64, use 'movq %fs:0,%register', on i386 'movl %gs:0,%register'. This instruction is guaranteed to return the thread-unique address of the tcb. See an article about ELF TLS for more details. Even better, this instruction is portable among all ELF Unixes which support TLS. --Il7n/DHsA0sMLmDu Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (FreeBSD) iQIcBAEBAgAGBQJQ9japAAoJEJDCuSvBvK1BjwkQAJ2oQ+UiLC2i7OpafggR2fob c0ICWGwq3bpiMcQke+VymAh0FakVvoMyfir/+K7ResqhPmSGpC61T3yOhUKLa8PB ZLM7YEfgFhFhBHfWFM2j2ejOn3+TkOv1xPYVZyOo9kQtwoj7LnB9u6qTlDdKfVdo o9Mh5eIBTIQ81UxdY9ow7a2X7mZdm1OK2UlIN/TA908eIKN6CW9FWf5+QhTErQTL zEg33QGPJK1OD3yMRIM1ZatXEvF3RtCUqXbM+8DKeuuErPKNws3OeZGYs1UDsBxG kbzjO9TGhwNDPqBoNUw2lJAYV5F4iiOBpcZbvQMwZF4IDstKxOxQ2CeXr5aSu4Lh qY4kb2Vs4WbQBmoEwMTSaRyCckZOU1sAUjabuEpDNxTlmGMOb6XvbBmdHD3cp1Pf DByV2T505uE/8fZUDyy0pserUPaEDHVlNu/IxD8t40kYGUib+qdgGJ8NG1Uq1kpT b0ywigZdXoU1ZwAHvJoa1L27rH7hQWKlaH2bJLxSJq9lzVNaLDXxoFLzPJQd2it/ fI2apBEw4XKMybYriIIAwMCD4+buK4eVtUlWaaE2AkHdhoZZQeiZXIFum93cH71t eP61V0Cc8d1VrMEQkDt1iIjTYOSVpRugoIPR/edDQ0CHl570bPNBMFstBXs13dBb fqM/thjYLkpHF2UajCkw =Cpnh -----END PGP SIGNATURE----- --Il7n/DHsA0sMLmDu--