public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Writing a thread-safe library without pulling in pthreads
@ 2008-07-18 12:56 Florian Weimer
  2008-07-18 15:23 ` Ian Lance Taylor
  0 siblings, 1 reply; 5+ messages in thread
From: Florian Weimer @ 2008-07-18 12:56 UTC (permalink / raw)
  To: gcc-help

I'd like to write a library which only uses locking if the application
is linked against pthreads.  Is this possible?

(The implementation requires global state, so it's not possible to
expose a thread-safe API explicitly passing objects containing state.)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Writing a thread-safe library without pulling in pthreads
  2008-07-18 12:56 Writing a thread-safe library without pulling in pthreads Florian Weimer
@ 2008-07-18 15:23 ` Ian Lance Taylor
  2008-07-18 16:05   ` Florian Weimer
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Lance Taylor @ 2008-07-18 15:23 UTC (permalink / raw)
  To: Florian Weimer; +Cc: gcc-help

Florian Weimer <fw@deneb.enyo.de> writes:

> I'd like to write a library which only uses locking if the application
> is linked against pthreads.  Is this possible?

Yes, and in fact libsupc++ in gcc does it.  You can declare the
pthread functions as weak references.  Then you only call them if the
address of the function is not NULL.

Ian

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Writing a thread-safe library without pulling in pthreads
  2008-07-18 15:23 ` Ian Lance Taylor
@ 2008-07-18 16:05   ` Florian Weimer
  2008-07-18 20:41     ` Ian Lance Taylor
  0 siblings, 1 reply; 5+ messages in thread
From: Florian Weimer @ 2008-07-18 16:05 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

* Ian Lance Taylor:

> Florian Weimer <fw@deneb.enyo.de> writes:
>
>> I'd like to write a library which only uses locking if the application
>> is linked against pthreads.  Is this possible?
>
> Yes, and in fact libsupc++ in gcc does it.  You can declare the
> pthread functions as weak references.  Then you only call them if the
> address of the function is not NULL.

Thanks.

But I'm hosed if a library is loaded later (via dlopen) that pulls in
threads, right?  This looks unsupported, so it should be okay if I can't
deal with this situation.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Writing a thread-safe library without pulling in pthreads
  2008-07-18 16:05   ` Florian Weimer
@ 2008-07-18 20:41     ` Ian Lance Taylor
  2008-07-18 21:12       ` Florian Weimer
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Lance Taylor @ 2008-07-18 20:41 UTC (permalink / raw)
  To: Florian Weimer; +Cc: gcc-help

Florian Weimer <fw@deneb.enyo.de> writes:

> * Ian Lance Taylor:
>
>> Florian Weimer <fw@deneb.enyo.de> writes:
>>
>>> I'd like to write a library which only uses locking if the application
>>> is linked against pthreads.  Is this possible?
>>
>> Yes, and in fact libsupc++ in gcc does it.  You can declare the
>> pthread functions as weak references.  Then you only call them if the
>> address of the function is not NULL.
>
> Thanks.
>
> But I'm hosed if a library is loaded later (via dlopen) that pulls in
> threads, right?  This looks unsupported, so it should be okay if I can't
> deal with this situation.

Yes, you are probably hosed in that case.  Offhand I can't think of
any reasonable way to make that work correctly, given that the dlopen
might occur when a lock would nominally be held.

Ian

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Writing a thread-safe library without pulling in pthreads
  2008-07-18 20:41     ` Ian Lance Taylor
@ 2008-07-18 21:12       ` Florian Weimer
  0 siblings, 0 replies; 5+ messages in thread
From: Florian Weimer @ 2008-07-18 21:12 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

* Ian Lance Taylor:

>> But I'm hosed if a library is loaded later (via dlopen) that pulls in
>> threads, right?  This looks unsupported, so it should be okay if I can't
>> deal with this situation.
>
> Yes, you are probably hosed in that case.  Offhand I can't think of
> any reasonable way to make that work correctly, given that the dlopen
> might occur when a lock would nominally be held.

Hmm, but that could only happen if dlopen is called from a signal
handler.  I suppose calling an signal-handler-safe function cannot
trigger arbitrary DSO loading (otherwise, this would be a bug).

I don't plan to hold the lock during callbacks or returns to the caller,
so I should be fine.  I just want to protect the state of a PRNG.

Sorry for posting to the wrong mailing list, BTW.  I intended to post to
libc-help. 8-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-07-18 20:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-18 12:56 Writing a thread-safe library without pulling in pthreads Florian Weimer
2008-07-18 15:23 ` Ian Lance Taylor
2008-07-18 16:05   ` Florian Weimer
2008-07-18 20:41     ` Ian Lance Taylor
2008-07-18 21:12       ` Florian Weimer

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).