Skip to content

Commit

Permalink
free more thread state in jl_delete_thread and GC
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Feb 22, 2024
1 parent a96726b commit 848ae2c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/gc-stacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ void sweep_stack_pools(void)
void *stk = small_arraylist_pop(al);
free_stack(stk, pool_sizes[p]);
}
if (jl_atomic_load_relaxed(&ptls2->current_task) == NULL) {
small_arraylist_free(al);
}
}

small_arraylist_t *live_tasks = &ptls2->heap.live_tasks;
Expand Down
28 changes: 28 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3775,6 +3775,22 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
else {
ptls2->heap.remset->len = 0;
}
// free empty GC state for threads that have exited
if (ptls2->current_task == NULL) {
jl_thread_heap_t *heap = &ptls2->heap;
if (heap->weak_refs.len == 0)
small_arraylist_free(&heap->weak_refs);
if (heap->live_tasks.len == 0)
small_arraylist_free(&heap->live_tasks);
if (heap->remset->len == 0)
arraylist_free(heap->remset);
if (heap->last_remset->len == 0)
arraylist_free(heap->last_remset);
if (ptls2->finalizers.len == 0)
arraylist_free(&ptls->finalizers);
if (ptls2->sweep_objs.len == 0)
arraylist_free(&ptls->sweep_objs);
}
}

#ifdef __GLIBC__
Expand Down Expand Up @@ -3993,6 +4009,18 @@ void jl_init_thread_heap(jl_ptls_t ptls)
jl_atomic_store_relaxed(&ptls->gc_num.allocd, -(int64_t)gc_num.interval);
}

void jl_free_thread_gc_state(jl_ptls_t ptls)
{
jl_gc_markqueue_t *mq = &ptls->mark_queue;
ws_queue_t *cq = &mq->chunk_queue;
free_ws_array(jl_atomic_load_relaxed(&cq->array));
jl_atomic_store_relaxed(&cq->array, NULL);
ws_queue_t *q = &mq->ptr_queue;
free_ws_array(jl_atomic_load_relaxed(&q->array));
jl_atomic_store_relaxed(&q->array, NULL);
arraylist_free(&mq->reclaim_set);
}

// System-wide initializations
void jl_gc_init(void)
{
Expand Down
5 changes: 5 additions & 0 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ static void jl_delete_thread(void *value) JL_NOTSAFEPOINT_ENTER
#else
pthread_mutex_unlock(&in_signal_lock);
#endif
free(ptls->bt_data);
// allow the page root_task is on to be freed
ptls->root_task = NULL;
void jl_free_thread_gc_state(jl_ptls_t ptls);
jl_free_thread_gc_state(ptls);
// then park in safe-region
(void)jl_gc_safe_enter(ptls);
}
Expand Down
6 changes: 6 additions & 0 deletions src/work-stealing-queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ static inline ws_array_t *create_ws_array(size_t capacity, int32_t eltsz) JL_NOT
return a;
}

static inline void free_ws_array(ws_array_t *a)
{
free(a->buffer);
free(a);
}

typedef struct {
_Atomic(int64_t) top;
char _padding[JL_CACHE_BYTE_ALIGNMENT - sizeof(_Atomic(int64_t))];
Expand Down

0 comments on commit 848ae2c

Please sign in to comment.