public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Mark Wielaard <mark@klomp.org>
Cc: libcurl development <curl-library@lists.haxx.se>,
	 Catalin Raceanu <cra@mega.nz>,
	 elfutils-devel@sourceware.org
Subject: Re: Using libcurl in another library, when/if to call curl_global_init?
Date: Tue, 05 Apr 2022 17:24:41 +0200	[thread overview]
Message-ID: <874k37y2me.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <YkWqdxmRuMHE2XnH@wildebeest.org> (Mark Wielaard's message of "Thu, 31 Mar 2022 15:19:51 +0200")

* Mark Wielaard:

> Hi,
>
> On Thu, Mar 31, 2022 at 04:00:16PM +0300, Catalin Raceanu via curl-library wrote:
>> On 31-Mar-22 15:04, Mark Wielaard wrote:
>> > whether there is a thread-safe way to call
>> > curl_global_init at a later time (to get rid of the library constructor
>> > init function).
>> 
>> I believe that this is an exact fit for C==11's std::call_once(). Boost also
>> has an equivalent, that most likely predates the other, in case older c++
>> standard is used.
>
> Thanks. Our library is pure C, but we can probably rely on
> pthread_once if it is allowable to call curl_global_init at a later
> time when multiple threads are already running.

The problem is that every caller of pthread_once needs to use the same
pthread_once_t synchronization object, so you still have the same
problem.

I strongly believe that library-safe code needs to perform lazy
initialization, that is, initialize the library on first use, and do
that in a thread-safe manner.  It solves the synchronization issue
between different users of the API, and it's the only way to report
errors properly (for example, a failure in an ELF constructor can only
be reported via process termination).

At least on Linux, the need to support multiple different threading
libraries is a thing of the past.

Thanks,
Florian


  reply	other threads:[~2022-04-05 15:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-31 12:04 Mark Wielaard
     [not found] ` <b424b435-482d-7d24-4337-8ef7437ab05c@mega.nz>
2022-03-31 13:19   ` Mark Wielaard
2022-04-05 15:24     ` Florian Weimer [this message]
2022-04-22 22:14     ` Mark Wielaard
2022-04-05 15:36 ` Daniel Stenberg
2022-04-05 16:19   ` Mark Wielaard

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=874k37y2me.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=cra@mega.nz \
    --cc=curl-library@lists.haxx.se \
    --cc=elfutils-devel@sourceware.org \
    --cc=mark@klomp.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).