Skip to content

Commit

Permalink
Fix GC_thread_is_registered for finished threads
Browse files Browse the repository at this point in the history
(a cherry-pick of commit 5a8c028 from 'master')

A finished thread may still stay in GC_threads, but it is no longer
considered during a garbage collection phase, thus
GC_thread_is_registered() should return false in such a case.

* include/gc.h [GC_THREADS] (GC_thread_is_registered): Refine
comment (describe the case when the thread is marked as finished).
* pthread_support.c (GC_thread_is_registered): Return FALSE if
me->flags&FINISHED.
* win32_threads.c (GC_thread_is_registered): Return FALSE if
KNOWN_FINISHED(me).
  • Loading branch information
ivmai committed Mar 14, 2023
1 parent 8c40c53 commit 72221f9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,8 @@ GC_API void GC_CALL GC_start_mark_threads(void);

/* Return non-zero (TRUE) if and only if the calling thread is */
/* registered with the garbage collector. */
/* If the thread is finished (e.g. running in a destructor and not */
/* registered manually again), it is considered as not registered. */
GC_API int GC_CALL GC_thread_is_registered(void);

/* Notify the collector about the stack and the alt-stack of the */
Expand Down
2 changes: 1 addition & 1 deletion pthread_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ GC_API int GC_CALL GC_thread_is_registered(void)
LOCK();
me = GC_lookup_thread(self);
UNLOCK();
return me != NULL;
return me != NULL && !(me -> flags & FINISHED);
}

static pthread_t main_pthread_id;
Expand Down
2 changes: 1 addition & 1 deletion win32_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ GC_API int GC_CALL GC_thread_is_registered(void)
LOCK();
me = GC_lookup_thread_inner(thread_id);
UNLOCK();
return me != NULL;
return me != NULL && !KNOWN_FINISHED(me);
}

GC_API void GC_CALL GC_register_altstack(void *stack GC_ATTR_UNUSED,
Expand Down

0 comments on commit 72221f9

Please sign in to comment.