[PATCH] malloc: Ensure that ptmalloc_init runs only once

Siddhesh Poyarekar siddhesh@sourceware.org
Fri Jun 18 02:31:30 GMT 2021


On 6/17/21 8:01 PM, Florian Weimer via Libc-alpha wrote:
> * Siddhesh Poyarekar via Libc-alpha:
> 
>> It is possible that multiple threads simultaneously enter
>> ptmalloc_init and succeed the < 0 check.  Make the comparison and
>> setting of __malloc_initialized atomic so that only one of them goes
>> through.  Additionally, if a thread sees that another thread is
>> running the initialization (i.e. __malloc_initialized == 0) then wait
>> till it is done.
> 
> No, this cannot happen because pthread_create calls malloc before
> creating the new thread.

Yes but I wonder if we should rely on that.  If we decide to rely on 
this semantic then we implicitly specify thread creation through methods 
other than pthread_create that happen to not call malloc as unsupported.

AFAICT it's not just a QOI issue; it may not be safe to call 
malloc_init_state twice on the same arena through different threads.

Siddhesh


More information about the Libc-alpha mailing list