Skip to content

Commit 7c7fd1e

Browse files
committed
free more thread state in jl_delete_thread and GC
1 parent b1c8e12 commit 7c7fd1e

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/gc-stacks.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ void sweep_stack_pools(void)
223223
void *stk = small_arraylist_pop(al);
224224
free_stack(stk, pool_sizes[p]);
225225
}
226+
if (jl_atomic_load_relaxed(&ptls2->current_task) == NULL) {
227+
small_arraylist_free(al);
228+
}
226229
}
227230

228231
small_arraylist_t *live_tasks = &ptls2->heap.live_tasks;

src/gc.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,6 +3499,22 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
34993499
else {
35003500
ptls2->heap.remset->len = 0;
35013501
}
3502+
// free empty GC state for threads that have exited
3503+
if (ptls2->current_task == NULL) {
3504+
jl_thread_heap_t *heap = &ptls2->heap;
3505+
if (heap->weak_refs.len == 0)
3506+
small_arraylist_free(&heap->weak_refs);
3507+
if (heap->live_tasks.len == 0)
3508+
small_arraylist_free(&heap->live_tasks);
3509+
if (heap->remset->len == 0)
3510+
arraylist_free(heap->remset);
3511+
if (heap->last_remset->len == 0)
3512+
arraylist_free(heap->last_remset);
3513+
if (ptls2->finalizers.len == 0)
3514+
arraylist_free(&ptls->finalizers);
3515+
if (ptls2->sweep_objs.len == 0)
3516+
arraylist_free(&ptls->sweep_objs);
3517+
}
35023518
}
35033519

35043520
#ifdef __GLIBC__
@@ -3708,6 +3724,18 @@ void jl_init_thread_heap(jl_ptls_t ptls)
37083724
jl_atomic_store_relaxed(&ptls->gc_num.allocd, -(int64_t)gc_num.interval);
37093725
}
37103726

3727+
void jl_free_thread_gc_state(jl_ptls_t ptls)
3728+
{
3729+
jl_gc_markqueue_t *mq = &ptls->mark_queue;
3730+
ws_queue_t *cq = &mq->chunk_queue;
3731+
free_ws_array(jl_atomic_load_relaxed(&cq->array));
3732+
jl_atomic_store_relaxed(&cq->array, NULL);
3733+
ws_queue_t *q = &mq->ptr_queue;
3734+
free_ws_array(jl_atomic_load_relaxed(&q->array));
3735+
jl_atomic_store_relaxed(&q->array, NULL);
3736+
arraylist_free(&mq->reclaim_set);
3737+
}
3738+
37113739
// System-wide initializations
37123740
void jl_gc_init(void)
37133741
{

src/threading.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@ static void jl_delete_thread(void *value) JL_NOTSAFEPOINT_ENTER
510510
#else
511511
pthread_mutex_unlock(&in_signal_lock);
512512
#endif
513+
free(ptls->bt_data);
514+
// allow the page root_task is on to be freed
515+
ptls->root_task = NULL;
516+
void jl_free_thread_gc_state(jl_ptls_t ptls);
517+
jl_free_thread_gc_state(ptls);
513518
// then park in safe-region
514519
(void)jl_gc_safe_enter(ptls);
515520
}

src/work-stealing-queue.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ static inline ws_array_t *create_ws_array(size_t capacity, int32_t eltsz) JL_NOT
3434
return a;
3535
}
3636

37+
static inline void free_ws_array(ws_array_t *a)
38+
{
39+
free(a->buffer);
40+
free(a);
41+
}
42+
3743
typedef struct {
3844
_Atomic(int64_t) top;
3945
_Atomic(int64_t) bottom;

0 commit comments

Comments
 (0)