mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Jens Gustedt <jens.gustedt@inria.fr>
To: musl@lists.openwall.com
Subject: Re: C11 threads
Date: Sat, 26 Jul 2014 09:16:40 +0200	[thread overview]
Message-ID: <1406359000.6438.95.camel@eris.loria.fr> (raw)
In-Reply-To: <20140726022453.GH4038@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 5585 bytes --]

Am Freitag, den 25.07.2014, 22:24 -0400 schrieb Rich Felker:
> > yes, defitively. Not even for the synchronization functions, basically
> > none from pthread and C11 thread should be mixed, I think. I thought
> > of figuring out a way to make this even a link error.
> 
> What do you mean? If you meant that calling C11 thread functions and
> POSIX thread functions in the same program should be an error, I think
> that's very wrong.

Yes, I meant that.

> The next issue of POSIX will be aligned with C11,
> so both sets of interfaces will exist. Even if not for that, though,
> it's just wrong conceptually to exclude the use of both. For example a
> library written to ISO C could be using threads (C11 threads)
> internally as an implementation detail, and this should not break a
> caller which is using POSIX threads.

Ok, didn't occur to me. Thinking of it, even the C library is allowed
to use C threads to accelerate things, as long as it follows the as-if
rule.

> It's about what happens when a thread exits whole holding a recursive
> or errorchecking mutex. If the ownership of that mutex is tracked by a
> thread id stored in the mutex (this is the only practical way to do
> it), a newly created thread could wrongly become the owner of the
> orphaned mutex just by getting the same thread id (by chance). The
> only implementation options to avoid this are to have thread ids so
> large that values never have to be reused, or to track the list of
> mutexes owned by a thread so that it can change the owner to a dummy
> value that will never match when it exits.
> 
> The obvious way to avoid this problem would be to add to the
> specification:
> 
> "If a thread exits while it is the owner of a mutex, the behavior is
> undefined."

noted, I'll watch that something in that sense is included.

Probably this would need a bit more precision about "exits", I would
prefer to use "terminates". A thread terminates not necessarily
immediately after it returns or calls "thrd_exit", the tss destructors
should be called still from the same thread.

We should allow a thread to cleanup its mess with tss destructors.

> > And to my limited experience having well defined atomics that are
> > integrated in the language, often helps to completely avoid mutexes
> > and conditions.
> 
> I'm not sure about that. Atomics are mostly useful for the situations
> where spinlocks would suffice. They don't help anywhere you would
> expect a "wait" operation to happen (e.q. waiting for a queue to
> become non-empty or non-full).

Probably we have complementary experiences. Many uses of mutexes and
conditions in application code are about sharing and updating shared
resources. Often the resource protected is just a counter or other
small data. Especially counters are much better served with atomics.

And you are right mentioning that, in many situations spin locks do
effectively suffice. C11 has atomic_flag for that. Often locks are
just taken for critical sections of code that do a small task, just
some instructions.

> > I only need EBUSY, EINVAL, ENOMEM, and ETIMEDOUT, and effectively only
> > that these are consistent with the rest of the C library, which for
> > this implementation of C threads will always be musl.
> 
> The point of ABI compatibility is that (at this point just some)
> binaries and (more importantly) shared libraries without source that
> were built/linked against glibc can be used with musl. But for this to
> work, the values of the constants need to be the same.

yes, I am aware of that. That is why it is important to have the
thrd-constants and the E-constants in line.

The approach with weak aliases only works if the return codes of the
functions agree. We need

enum {
  thrd_success = 0,
  thrd_busy = EBUSY,
  thrd_error = EINVAL,
  thrd_nomem = ENOMEM,
  thrd_timedout = ETIMEDOUT,
};

and I don't think that there is much of a sensible way to do that
differently. Already the naming of the constants suggest that these
are the values that people (who?) had in mind when designing these
interfaces.

> mtx_init needs to be a wrapper for pthread_mutex_init rather than an
> alias anyway, since mtx_init takes an integer and pthread_mutex_init
> takes a pointer to an attribute object.

sure

> TSS_DTOR_ITERATIONS can just be defined to whatever the right value is
> -- IIRC we use the minimum POSIX requires. It doesn't need to
> magically sync with something else. If we ever need to change it we
> can change both.

ok

> Obviously if the error values are used directly, duplicating them in
> another header is more trouble since they vary per-arch. This is part
> of why I would actually prefer not to use them for the thread function
> result codes, but which we do will depend on which way glibc does it.
> I can check in with them and see if they have a plan yet.

yes, that would be good

I, on my site, will try to have something added to the C specification
that threads.h also includes errno.h. For the moment it is only
specified that it does so with time.h.

Perhaps it would even be good to have the thrd-constants also be
exported by errno.h. These are error codes, finally.


Jens

-- 
:: INRIA Nancy Grand Est ::: AlGorille ::: ICube/ICPS :::
:: ::::::::::::::: office Strasbourg : +33 368854536   ::
:: :::::::::::::::::::::: gsm France : +33 651400183   ::
:: ::::::::::::::: gsm international : +49 15737185122 ::
:: http://icube-icps.unistra.fr/index.php/Jens_Gustedt ::



[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  reply	other threads:[~2014-07-26  7:16 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-25 10:00 Jens Gustedt
2014-07-25 10:40 ` Szabolcs Nagy
2014-07-25 11:06   ` Jens Gustedt
2014-07-25 13:04     ` Szabolcs Nagy
2014-07-25 13:38       ` Jens Gustedt
2014-07-25 13:42       ` Morten Welinder
2014-07-25 14:15         ` Szabolcs Nagy
2014-07-25 16:14       ` Rich Felker
2014-07-25 21:59         ` Jens Gustedt
2014-07-25 22:25           ` Rich Felker
2014-07-25 15:41     ` Rich Felker
2014-07-25 17:10       ` Jens Gustedt
2014-07-25 22:19         ` Rich Felker
2014-07-25 23:26           ` Jens Gustedt
2014-07-26  2:24             ` Rich Felker
2014-07-26  7:16               ` Jens Gustedt [this message]
2014-07-26  7:35                 ` Rich Felker
2014-07-26  8:32                   ` Jens Gustedt
2014-07-26  9:03                     ` Rich Felker
2014-07-26 11:15                       ` Jens Gustedt
2014-07-26 15:48                 ` Jens Gustedt

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=1406359000.6438.95.camel@eris.loria.fr \
    --to=jens.gustedt@inria.fr \
    --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).