public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Florian Weimer <fweimer@redhat.com>, libc-alpha@sourceware.org
Subject: Re: Calling __cxa_thread_atexit_impl directly, from C code
Date: Mon, 29 Aug 2022 15:59:00 -0300	[thread overview]
Message-ID: <fce79f68-cbbd-b353-8c61-de512f0d5a54@linaro.org> (raw)
In-Reply-To: <87lerbtnh4.fsf@oldenburg.str.redhat.com>



On 26/08/22 05:31, Florian Weimer via Libc-alpha wrote:
> Do we support calling __cxa_thread_atexit_impl directly?  The function
> is not documented, nor is it declared in any installed header.
> 
> I think the answer is yes, because we support different C++
> implementations, and they have to call __cxa_thread_atexit_impl for
> thread-local destructors, either directly or through a wrapper
> (__cxa_thread_atexit for libstdc++, as proposed for the Itanium C++
> ABI).

Not only that, but if the C library does not provide the C++ replacement
will most likely have serious drawbacks such as no handling of dso_symbol 
(so no synchronization between thread_local destructors and
dclose) and no ordering enforcing in main thread thread_local destructors.
The libc++ implementation does document the possible issues with the
fallback.

> 
> There's interest in this because it's much easier to use than
> pthread_key_create if you have to avoid linking against libpthread and
> you target glibc releases between 2.18 and 2.33 (although it is possible
> to use weak symbols, as in the libstdc++ implementation in
> libstdc++-v3/libsupc++/atexit_thread.cc).

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 does make sense to use __cxa_thread_atexit for C++ since it aims to have
interoperability with C, but it is not clear to me how useful it would be for
a language or runtime that does not aim for it (it would be simple to just
do all the required work on the thread wrapper or similar facility).

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.

  reply	other threads:[~2022-08-29 18:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-26  8:31 Florian Weimer
2022-08-29 18:59 ` Adhemerval Zanella Netto [this message]
2022-08-29 19:21   ` Florian Weimer
2022-08-29 19:56     ` Adhemerval Zanella Netto
2022-08-30  7:37       ` Florian Weimer
2022-08-30 12:56         ` Adhemerval Zanella Netto
2022-09-06  6:44           ` Florian Weimer

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=fce79f68-cbbd-b353-8c61-de512f0d5a54@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).