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: Fwd: local equivalent for pthread_once() in glibc?



On 26/04/2017 05:35, Florian Weimer wrote:
> On 04/25/2017 11:19 PM, Chris Aoki wrote:
> 
>> p.s.  The secondary question, which is specific to glibc malloc, is
>> whether ptmalloc_init() can be called concurrently by multiple threads.
> 
> No, but the reason is really subtle: pthread_create calls calloc before creating the new thread. :)
> 
> #0  ptmalloc_init () at arena.c:255
> #1  0x00007ffff787a7ad in ptmalloc_init () at malloc.c:2935
> #2  malloc_hook_ini (sz=272, caller=<optimized out>) at hooks.c:31
> #3  0x00007ffff7879d8a in __libc_calloc (n=<optimized out>,
>     elem_size=<optimized out>) at malloc.c:3234
> #4  0x00007ffff7deac2b in allocate_dtv (result=0x7ffff77f2700) at dl-tls.c:322
> #5  __GI__dl_allocate_tls (mem=mem@entry=0x7ffff77f2700) at dl-tls.c:539
> #6  0x00007ffff7bc128a in allocate_stack (stack=<synthetic pointer>,
>     pdp=<synthetic pointer>, attr=0x7fffffffe520) at allocatestack.c:586
> #7  __pthread_create_2_1 (newthread=0x7fffffffe5a8, attr=0x0,
>     start_routine=0x4005c6 <f>, arg=0x0) at pthread_create.c:539
> 
>>> I have a structure that is frequently accessed after initialization
>>> so putting a lock around initialization and a check for initialization
>>> would add considerable overhead.   Normally one would use pthread_once()
>>> in this situation, but my colleagues tell me that adding a reference to an
>>> external function is prohibited within libc.so.   I see a macro __libc_once()
>>> but it does not appear to synchronize:
>>>
>>> /* Define once control variable.  */
>>> #define __libc_once_define(CLASS, NAME) CLASS int NAME = 0
>>>
>>> /* Call handler iff the first call.  */
>>> #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
>>>   do {                                                                        \
>>>     if ((ONCE_CONTROL) == 0) {                                                \
>>>       INIT_FUNCTION ();                                                       \
>>>       (ONCE_CONTROL) = 1;                                                     \
>>>     }                                                                         \
>>>   } while (0)
>>>
>>> Any clues appreciated.  Thanks
> 
> There is an override for the generic definition in sysdeps/nptl/libc-lockP.h, which is used on Linux.  In general, you have to examine the source tree carefully to discover such overrides.
> 
> Thanks,
> Florian

Now that we are on the subject, shouldn't we use __libc_once on __malloc_initialized?


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