Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Wed, 2 Mar 2016 18:26:36 -0500
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: [PATCH] add sched_getcpu

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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.