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: C11 threads ABI - mtx_t and cnd_t types


On Sun, Aug 31, 2014 at 01:48:35AM -0700, pinskia@gmail.com wrote:
> 
> 
> > On Aug 30, 2014, at 7:52 PM, Rich Felker <dalias@libc.org> wrote:
> > 
> > Another issue I have on the ABI for C11 threads pertains to the types
> > for mtx_t and cnd_t. My understanding, and I agree with this, is that
> > it was already decided to use the same underlying sizes/alignment, and
> > for now representations, as the corresponding POSIX types. However,
> > it's impossible to use the existing types directly due to namespace
> > issues. If the following were used:
> > 
> > typedef pthread_mutex_t mtx_t;
> > typedef pthread_cond_t cnd_t;
> > 
> > this would require pthread_mutex_t and pthread_cond_t to be visible
> > when threads.h is included.
> > 
> > It's possible to consider adding union tags (the underlying types of
> > pthread_mutex_t and pthread_cond_t are both unions without tags) in a
> > protected namespace, so that you could have:
> > 
> > union __mutex { ... };
> > typedef union __mutex pthread_mutex_t;
> > typedef union __mutex mtx_t;
> 
> Why not do:
> union __mutex {....}
> Struct pthread_mutex_t {__mutex d; };
> typedef union __mutex mtx_t;
> 
> That won't change the name mangling for pthread_mutex_t and allow for mtx_t not be a tagged one. 

This is potentially also legal (modulo some minor details you messed
up, like changing the definition of pthread_mutex_t from a tagless
union to a tagged structure), but unlike what I recommended it
requires internal changes to the implementation to avoid illegal
aliasing. If you have an object of type T and an aggregate type A
containing an object of type T as its only member, you cannot access
the object of type T as if it had type A. You would instead have to
cast the A* to T* before dereferencing it.

Note that what I proposed (tagless unions for mtx_t and cnd_t with the
same definitions as the pthread types) also supports an implementation
like the above where you cast to a pointer to the contained type
(e.g., currently, struct __pthread_mutex_s), but it's not necessary
since the original types are compatible due to beging tagless and
having the same members.

Rich


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