Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sat, 26 Jul 2014 09:16:40 +0200
From: Jens Gustedt <jens.gustedt@...ia.fr>
To: musl@...ts.openwall.com
Subject: Re: C11 threads

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 ::



Download attachment "signature.asc" of type "application/pgp-signature" (199 bytes)

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.