From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9444 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] add sched_getcpu Date: Wed, 2 Mar 2016 18:26:36 -0500 Message-ID: <20160302232635.GP9349@brightrain.aerifal.cx> References: <1456764572-18648-1-git-send-email-nathan@nathan7.eu> <1456780194.12169.25.camel@xiaoka.com> <20160229213057.GE9349@brightrain.aerifal.cx> <1456864521.12169.35.camel@xiaoka.com> <20160301223419.GL9349@brightrain.aerifal.cx> <1456951613.12169.42.camel@xiaoka.com> <20160302211924.GI29662@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1456961214 3394 80.91.229.3 (2 Mar 2016 23:26:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 2 Mar 2016 23:26:54 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-9457-gllmg-musl=m.gmane.org@lists.openwall.com Thu Mar 03 00:26:54 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1abGAM-0004Hg-Ov for gllmg-musl@m.gmane.org; Thu, 03 Mar 2016 00:26:50 +0100 Original-Received: (qmail 28586 invoked by uid 550); 2 Mar 2016 23:26:48 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 28563 invoked from network); 2 Mar 2016 23:26:48 -0000 Content-Disposition: inline In-Reply-To: <20160302211924.GI29662@port70.net> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:9444 Archived-At: On Wed, Mar 02, 2016 at 10:19:25PM +0100, Szabolcs Nagy wrote: > > > > syscall(SYS_gettid > > > For glibc it's been controversial whether to expose tids as a public > > > API, since it pokes through the pthread abstraction and imposes a 1:1 > > > threads implementation. > > > > I am implementing a threading and mutex API that is different to > > pthread. (Still 1:1 though.) > > Using pthread to do this proved to be cumbersome, but using native > > Linux abstractions turned out to be pretty straightforward. > > > > that's not possible in c > > the semantics (memory model, libc internals..) assume > that threads can only be created by the c runtime. > > in theory you can create you own threads, but you have > to know what you are doing (no libc calls, no tls) > but then you are implementing your own libc Indeed, on any modern libc, a thread not created by the standard functions cannot call any standard library function safely. This is because (among other reasons) the thread pointer (used for thread local storage) needs to point to a correctly setup data structure whose definition is not public and which may vary between libc builds/versions. musl is somewhat more conservative about using the thread pointer internally (because it can be slow on some archs, and because historically we supported pre-TLS kernels) but things will break immediately on glibc if you break this rule, and fairly quickly on musl too. The only safe way to make your own threads is to refrain from using libc at all and do your own syscalls. Even calling syscall() may not be safe (on i386/glibc it probably uses the vdso syscall pointer from TLS); you really need to use asm to make the syscall. > > > syscall(SYS_tgkill > > > tgkill also requires tids to be exposed an potentially has other > > > issues, and doesn't seem to offer anything that pthread_kill doesn't. > > > > As above - using pthreads is not the good way to do it in my case. > > what you are doing is undefined behaviour. >From a standards perspective it's just outside the scope of what's defined. From musl's and glibc's perspectives, the behavior is undefined. Is there a reason "pthreads is not a good way to do it"? Rich