Ticket #13111: fork-heap-locks-handling-2.patch

File fork-heap-locks-handling-2.patch, 3.9 KB (added by korli, 7 years ago)

path with pthread_atfork

  • src/system/libroot/posix/malloc/arch-specific.cpp

    diff --git a/src/system/libroot/posix/malloc/arch-specific.cpp b/src/system/libroot/posix/malloc/arch-specific.cpp
    index 4167f04..97db3b8 100644
    a b static size_t sFreeHeapSize, sHeapAreaSize;  
    6767static free_chunk *sFreeChunks;
    6868
    6969
    70 static void
    71 init_after_fork(void)
     70void __heap_before_fork(void);
     71void __heap_after_fork_child(void);
     72void __heap_after_fork_parent(void);
     73
     74
     75void
     76__init_after_fork(void)
    7277{
    7378    // find the heap area
    7479    sHeapArea = area_for((void*)sFreeHeapBase);
    __init_heap(void)  
    110115
    111116    hoardLockInit(sHeapLock, "heap");
    112117
    113     atfork(&init_after_fork);
     118    pthread_atfork(&__heap_before_fork, &__heap_after_fork_parent,
     119        &__heap_after_fork_child);
    114120        // Note: Needs malloc(). Hence we need to be fully initialized.
    115         // TODO: We should actually also install a hook that is called before
    116         // fork() is being executed. In a multithreaded app it would need to
    117         // acquire *all* allocator locks, so that we don't fork() an
    118         // inconsistent state.
    119121
    120122    return B_OK;
    121123}
  • src/system/libroot/posix/malloc/heap.cpp

    diff --git a/src/system/libroot/posix/malloc/heap.cpp b/src/system/libroot/posix/malloc/heap.cpp
    index 99aba54..e2cc553 100644
    a b hoardHeap::hoardHeap(void)  
    132132    , _magic(HEAP_MAGIC)
    133133#endif
    134134{
    135     // Initialize the per-heap lock.
    136     hoardLockInit(_lock, "hoard heap");
     135    initLock();
    137136
    138137    for (int i = 0; i < SUPERBLOCK_FULLNESS_GROUP; i++) {
    139138        for (int j = 0; j < SIZE_CLASSES; j++) {
  • src/system/libroot/posix/malloc/heap.h

    diff --git a/src/system/libroot/posix/malloc/heap.h b/src/system/libroot/posix/malloc/heap.h
    index f42962e..42e9eb4 100644
    a b class hoardHeap {  
    115115        // Unlock this heap.
    116116        inline void unlock(void);
    117117
     118        // Init this heap lock.
     119        inline void initLock(void);
     120
    118121        // Set our index number (which heap we are).
    119122        inline void setIndex(int i);
    120123
    hoardHeap::unlock(void)  
    444447}
    445448
    446449
     450void
     451hoardHeap::initLock(void)
     452{
     453    // Initialize the per-heap lock.
     454    hoardLockInit(_lock, "hoard heap");
     455}
     456
     457
    447458size_t
    448459hoardHeap::align(const size_t sz)
    449460{
  • src/system/libroot/posix/malloc/processheap.h

    diff --git a/src/system/libroot/posix/malloc/processheap.h b/src/system/libroot/posix/malloc/processheap.h
    index f222289..2c94af9 100644
    a b class processHeap : public hoardHeap {  
    6767        // Get a thread heap index.
    6868        inline int getHeapIndex(void);
    6969
     70        // Get thread heap max.
     71        inline int getMaxThreadHeaps(void);
     72
    7073        // Get the thread heap with index i.
    7174        inline HEAPTYPE & getHeap(int i);
    7275
    processHeap::getHeapIndex(void)  
    199202}
    200203
    201204
     205// Return the maximum number of heaps.
     206
     207int
     208processHeap::getMaxThreadHeaps(void)
     209{
     210    return fMaxThreadHeaps;
     211}
     212
     213
    202214superblock *
    203215processHeap::acquire(const int sizeclass, hoardHeap * dest)
    204216{
  • src/system/libroot/posix/malloc/wrapper.cpp

    diff --git a/src/system/libroot/posix/malloc/wrapper.cpp b/src/system/libroot/posix/malloc/wrapper.cpp
    index c83336a..0b882b5 100644
    a b getAllocator(void)  
    256256}
    257257
    258258
     259void
     260__heap_before_fork(void)
     261{
     262    static processHeap *pHeap = getAllocator();
     263    for (int i = 0; i < pHeap->getMaxThreadHeaps(); i++)
     264        pHeap->getHeap(i).lock();
     265}
     266
     267
     268void __init_after_fork(void);
     269
     270void
     271__heap_after_fork_child(void)
     272{
     273    __init_after_fork();
     274    static processHeap *pHeap = getAllocator();
     275    for (int i = 0; i < pHeap->getMaxThreadHeaps(); i++)
     276        pHeap->getHeap(i).initLock();
     277}
     278
     279
     280void
     281__heap_after_fork_parent(void)
     282{
     283    static processHeap *pHeap = getAllocator();
     284    for (int i = 0; i < pHeap->getMaxThreadHeaps(); i++)
     285        pHeap->getHeap(i).unlock();
     286}
     287
     288
    259289//  #pragma mark - public functions
    260290
    261291