This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: threads.h implementation issues


On Mon, Oct 27, 2014 at 10:49:23PM +0100, Juan Manuel Torres Palma wrote:
> Hi guys.
> 
> I'm still working on the implementation of the C11 threads but now i'm
> getting a few doubts on how to do some stuff so I thought that
> feedback from you, more experienced fellas, would be really
> appreciated. Let's start with it.
> 
> - My main problem is with thrd_exit and thrd_join. thrd_exit is
> supposed to return an int value that thrd_join can receive if waiting
> for that thread to terminate. My question is: If a joined thread calls
> thrd_exit, the int value to be returned will be copied in it's
> stack.ant then, once it dies, it's completely impossible for the
> joiner thread to obtain that value. The same happens with
> pthread_exit, but in this case, it returns a void* value, so if the
> value to be returned is allocated in heap, there is no problem to
> access it later with pthread_join and free that piece of memory.
> Anyone can address me or suggest how to solve this?

In the pthread_exit case, the _value_ being returned is not "on the
heap". The value is a pointer of type void*. It may point to to
something on the heap, but it certainly doesn't need to, and what it
points to, or whether it even points to anything at all, is irrelevant
at this level. All that matters is that a value is successfully
returned by value.

For the thrd_exit case, the situation is exactly the same except that
the value you're trying to return has type int. In principle, the
choices for a solution would be:

1. Add storage for an int return value in addition to the existing
   storage for a void* return value, and use one as appropriate.

2. Use a union to store either one in the same storage unit.

3. Cast int->uintptr_t->void*->uintptr_t->int to round-trip the int
   result via a value of type void*. This assumes that any value of
   int can be represented in uintptr_t, and that any value of
   uintptr_t can be represented in void*. I think this is acceptable
   but others may disagree.

There may be other possibilities too.

> - Another one is with the mtx_t and cnd_t. The rest of types could be
> just copied from the Linux Base Specification for pthreads, so there
> won't be namespace issues, but those two are still unclear to me. What
> should I do? Rename pthread_cond_t to __pthread_cond_t and
> pthread_mutex_t to __pthread_mutex_t and use weak aliases?

No, that breaks the C++ ABI. Instead, you should duplicate the exact
definitions of pthread_mutex_t and pthread_cond_t. See this thread in
the archives:

https://www.sourceware.org/ml/libc-alpha/2014-08/msg00494.html

> - To implement thrd_sleep I'm using nanosleep, that is a syscall, is
> that ok to you?

That should be fine, but you need to keep nanosleep out of the
namespace, so either use __nanosleep (if it exists) or an inline
syscall.

Rich


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]