mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [PATCH] add sched_getcpu
Date: Wed, 2 Mar 2016 18:26:36 -0500	[thread overview]
Message-ID: <20160302232635.GP9349@brightrain.aerifal.cx> (raw)
In-Reply-To: <20160302211924.GI29662@port70.net>

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


  reply	other threads:[~2016-03-02 23:26 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-29 16:49 Nathan Zadoks
2016-02-29 16:57 ` Nathan Zadoks
2016-02-29 16:57   ` Nathan Zadoks
2016-02-29 17:00   ` Nathan Zadoks
2016-02-29 17:23     ` Alexander Monakov
2016-02-29 17:33       ` Alexander Monakov
2016-03-01 13:45         ` [PATCH] add sched_getcpu, with vDSO support Nathan Zadoks
2016-03-01 15:56           ` Nathan Zadoks
2016-03-02  5:55             ` Rich Felker
2016-03-02 16:26               ` [PATCH 0/2] add sched_getcpu, take n+1 Nathan Zadoks
2016-03-02 16:26                 ` [PATCH 1/2] add sched_getcpu Nathan Zadoks
2016-03-02 16:26                 ` [PATCH 2/2] add sched_getcpu vDSO support Nathan Zadoks
2016-03-03  3:01                 ` [PATCH 0/2] add sched_getcpu, take n+1 Rich Felker
2016-02-29 17:49       ` [PATCH] add sched_getcpu nathan
2016-02-29 17:52         ` nathan
2016-02-29 20:17           ` Alexander Monakov
2016-02-29 20:49             ` Nathan Zadoks
2016-02-29 18:38       ` Rich Felker
2016-02-29 19:59         ` Alexander Monakov
2016-02-29 20:05           ` Rich Felker
2016-02-29 20:10             ` Alexander Monakov
2016-02-29 20:17               ` Rich Felker
2016-02-29 21:09 ` Tomasz Sterna
2016-02-29 21:21   ` Nathan Zadoks
2016-02-29 21:30   ` Rich Felker
2016-03-01 20:35     ` Tomasz Sterna
2016-03-01 22:34       ` Rich Felker
2016-03-02 20:46         ` Tomasz Sterna
2016-03-02 21:19           ` Szabolcs Nagy
2016-03-02 23:26             ` Rich Felker [this message]
2016-03-04 22:21               ` Tomasz Sterna
2016-03-04 23:33                 ` Rich Felker
2016-03-05 11:40                   ` Tomasz Sterna

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160302232635.GP9349@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).