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

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

patch without pthread_atfork

  • headers/private/libroot/libroot_private.h

    diff --git a/headers/private/libroot/libroot_private.h b/headers/private/libroot/libroot_private.h
    index d0676a6..79ec524 100644
    a b void __init_env(const struct user_space_program_args *args);  
    3636void __init_env_post_heap(void);
    3737status_t __init_heap(void);
    3838void __heap_terminate_after(void);
     39void __heap_before_fork(void);
     40void __heap_after_fork_child(void);
     41void __heap_after_fork_parent(void);
    3942
    4043void __init_time(addr_t commPageTable);
    4144void __arch_init_time(struct real_time_data *data, bool setDefaults);
  • 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..f3e1384 100644
    a b getAllocator(void)  
    256256}
    257257
    258258
     259extern "C" void
     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
     268extern "C" void
     269__heap_after_fork_child(void)
     270{
     271    static processHeap *pHeap = getAllocator();
     272    for (int i = 0; i < pHeap->getMaxThreadHeaps(); i++)
     273        pHeap->getHeap(i).initLock();
     274}
     275
     276
     277extern "C" void
     278__heap_after_fork_parent(void)
     279{
     280    static processHeap *pHeap = getAllocator();
     281    for (int i = 0; i < pHeap->getMaxThreadHeaps(); i++)
     282        pHeap->getHeap(i).unlock();
     283}
     284
     285
    259286//  #pragma mark - public functions
    260287
    261288
  • src/system/libroot/posix/malloc_debug/malloc_debug_api.cpp

    diff --git a/src/system/libroot/posix/malloc_debug/malloc_debug_api.cpp b/src/system/libroot/posix/malloc_debug/malloc_debug_api.cpp
    index 3d3b062..bf38d4c 100644
    a b __heap_terminate_after()  
    202202}
    203203
    204204
     205extern "C" void
     206__heap_before_fork(void)
     207{
     208}
     209
     210
     211extern "C" void
     212__heap_after_fork_child(void)
     213{
     214}
     215
     216
     217extern "C" void
     218__heap_after_fork_parent(void)
     219{
     220}
     221
     222
    205223// #pragma mark - Public API
    206224
    207225
  • src/system/libroot/posix/unistd/fork.c

    diff --git a/src/system/libroot/posix/unistd/fork.c b/src/system/libroot/posix/unistd/fork.c
    index 8d8a963..8b4ae8e 100644
    a b fork(void)  
    135135
    136136    // call preparation hooks
    137137    call_fork_hooks(sPrepareHooks);
     138    __heap_before_fork();
    138139
    139140    thread = _kern_fork();
    140141    if (thread < 0) {
    fork(void)  
    156157            // calling the kernel.
    157158        __gRuntimeLoader->reinit_after_fork();
    158159        __reinit_pwd_backend_after_fork();
     160        __heap_after_fork_child();
    159161
    160162        call_fork_hooks(sChildHooks);
    161163    } else {
    162164        // we are the parent
     165        __heap_after_fork_parent();
    163166        call_fork_hooks(sParentHooks);
    164167        mutex_unlock(&sForkLock);
    165168    }