Calling __cxa_thread_atexit_impl directly, from C code

Florian Weimer fweimer@redhat.com
Mon Aug 29 19:21:24 GMT 2022


* Adhemerval Zanella Netto:

> Although it seems to be used solely by C++ and the interface is generic enough
> so any runtime/language might use it as well for any thread exit deallocation
> cleanup, I am not sure it would be feasible to export or support calling it
> from C code.

It's an exported symbol, and usually we treat those as part of the ABI.

> The interface also have two annoying peculiarities where calling from C code
> is not straightforward: 
>
>   1. Any memory failure aborts the process, which is far from ideal to a
>      generic interface.
>
>   2. User need to correctly declare __dso_handle; using NULL (or any invalid
>      value) will bound the to callback to main program (which is not correct
>      if use within a shared library).
>
> So I think it would be better to provide a different interface.

We could add this:

static inline int
thread_atexit (void (*__callback) (void *__data), void *__data)
{
  extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
  extern int __cxa_thread_atexit_impl (void (*__callback) (void *__data),
                                        void *__caller);
  return __cxa_thread_atexit_impl (__callback, __data, &__dso_handle);
}

to <stdlib.h> with some approriate preprocessor conditionals.

Maybe libgcc_s should do the error checking in its __cxa_thread_atexit
function?  Then we could simply use the existing implementation in the
function above, remove our abort, and defer the problem to the
thread_atexit caller.

Thanks,
Florian



More information about the Libc-alpha mailing list