From owner-freebsd-hackers@FreeBSD.ORG Tue Jan 15 21:35:18 2013 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id B4A372EE for ; Tue, 15 Jan 2013 21:35:18 +0000 (UTC) (envelope-from trent@snakebite.org) Received: from exchange.liveoffice.com (exchla3.liveoffice.com [64.70.67.188]) by mx1.freebsd.org (Postfix) with ESMTP id 9B2A4984 for ; Tue, 15 Jan 2013 21:35:17 +0000 (UTC) Received: from EXHUB03.exchhosting.com (192.168.11.104) by exhub05.exchhosting.com (192.168.11.101) with Microsoft SMTP Server (TLS) id 8.3.213.0; Tue, 15 Jan 2013 13:35:17 -0800 Received: from localhost (35.8.247.10) by exchange.liveoffice.com (192.168.11.104) with Microsoft SMTP Server id 8.3.213.0; Tue, 15 Jan 2013 13:35:16 -0800 Date: Tue, 15 Jan 2013 16:35:14 -0500 From: Trent Nelson To: Konstantin Belousov Subject: Re: Getting the current thread ID without a syscall? Message-ID: <20130115213513.GA53047@snakebite.org> References: <20130115205403.GA52904@snakebite.org> <20130115211641.GC2522@kib.kiev.ua> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20130115211641.GC2522@kib.kiev.ua> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: "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: Tue, 15 Jan 2013 21:35:18 -0000 On Tue, Jan 15, 2013 at 01:16:41PM -0800, Konstantin Belousov wrote: > On Tue, Jan 15, 2013 at 03:54:03PM -0500, Trent Nelson wrote: > > Howdy, > > > > I have an unusual requirement: I need to get the current thread ID > > in as few instructions as possible. On Windows, I managed to come > > up with this glorious hack: > > > > #ifdef WITH_INTRINSICS > > # ifdef MS_WINDOWS > > # include > > # if defined(MS_WIN64) > > # pragma intrinsic(__readgsdword) > > # define _Py_get_current_process_id() (__readgsdword(0x40)) > > # define _Py_get_current_thread_id() (__readgsdword(0x48)) > > # elif defined(MS_WIN32) > > # pragma intrinsic(__readfsdword) > > # define _Py_get_current_process_id() (__readfsdword(0x20)) > > # define _Py_get_current_thread_id() (__readfsdword(0x24)) > > > > That exploits the fact that Windows uses the FS/GS registers to > > store thread/process metadata. Could I use a similar approach on > > FreeBSD to get the thread ID without the need for syscalls? > The layout of the per-thread structure used by libthr is private and > is not guaranteed to be stable even on the stable branches. > > Yes, you could obtain the tid this way, but note explicitely that using > it makes your application not binary compatible with any version of > the FreeBSD except the one you compiled on. Luckily it's for an open source project (Python), so recompilation isn't a big deal. (I also check the intrinsic result versus the syscall result during startup to verify the same ID is returned, falling back to the syscall by default.) > You could read the _thread_off_tid integer variable and use the value > as offset from the %fs base to the long containing the unique thread id. > But don't use this in anything except the private code. Ah, thanks, that's what I was interested in knowing. > > > > (I technically don't need the thread ID, I just need to get some > > form of unique identifier for the current thread such that I can > > compare it to a known global value that's been set to the "main > > thread", in order to determine if I'm currently that thread or not. > > As long as it's unique for each thread, and static for the lifetime > > of the thread, that's fine.) > > > > The "am I the main thread?" comparison is made every ~50-100 opcodes, > > which is why it needs to have the lowest overhead possible. > On newer CPUs in amd64 mode, there is getfsbase instruction which reads > the %fs register base. System guarantees that %fs base is unique among > live threads. Interesting. I was aware of those instructions, but never assessed them in detail once I'd figured out the readgsdword approach. I definitely didn't realize they return unique values per thread (although it makes sense now that I think about it). Thanks Konstantin, very helpful. Trent.