Skip to content

Commit

Permalink
Fix lock assertion violation in GC_try_to_collect_inner on OS X
Browse files Browse the repository at this point in the history
(fix of commit b7b1004)

Issue #583 (bdwgc).

This happens only in the multi-threaded collector with assertions on
and malloc redirection if GC_enable_incremental() is called.

* include/private/gc_priv.h [MPROTECT_VDB && DARWIN]: Include pthread.h
(wrapped into EXTERN_C_END/BEGIN).
* include/private/gc_priv.h [MPROTECT_VDB && DARWIN && THREADS]
(GC_inner_pthread_create): Declare (as GC_INNER function).
* include/private/gc_priv.h [MPROTECT_VDB && DARWIN && !THREADS]
(GC_inner_pthread_create): Define as macro (redirecting to
pthread_create).
* os_dep.c [MPROTECT_VDB && DARWIN]: Do not include pthread.h.
* os_dep.c [MPROTECT_VDB && DARWIN] (pthread_create): Do not undefine.
* os_dep.c [MPROTECT_VDB && DARWIN] (GC_dirty_init): Call
GC_inner_pthread_create() instead of pthread_create().
* pthread_support.c [MPROTECT_VDB && DARWIN] (GC_inner_pthread_create):
Define function (call INIT_REAL_SYMS() and REAL_FUNC(pthread_create)).
  • Loading branch information
ivmai committed Nov 14, 2023
1 parent 5569289 commit 40eef90
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
13 changes: 13 additions & 0 deletions include/private/gc_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2581,6 +2581,19 @@ GC_EXTERN GC_bool GC_print_back_height;
# endif
# endif /* CAN_HANDLE_FORK */

# if defined(MPROTECT_VDB) && defined(DARWIN)
EXTERN_C_END
# include <pthread.h>
EXTERN_C_BEGIN
# ifdef THREADS
GC_INNER int GC_inner_pthread_create(pthread_t *t,
GC_PTHREAD_CREATE_CONST pthread_attr_t *a,
void *(*fn)(void *), void *arg);
# else
# define GC_inner_pthread_create pthread_create
# endif
# endif /* MPROTECT_VDB && DARWIN */

GC_INNER GC_bool GC_dirty_init(void);
/* Returns true if dirty bits are maintained (otherwise */
/* it is OK to be called again if the client invokes */
Expand Down
6 changes: 2 additions & 4 deletions os_dep.c
Original file line number Diff line number Diff line change
Expand Up @@ -4352,7 +4352,6 @@ GC_INNER GC_bool GC_dirty_init(void)
#include <mach/mach_error.h>
#include <mach/exception.h>
#include <mach/task.h>
#include <pthread.h>

EXTERN_C_BEGIN

Expand Down Expand Up @@ -4746,9 +4745,8 @@ GC_INNER GC_bool GC_dirty_init(void)
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
ABORT("pthread_attr_setdetachedstate failed");

# undef pthread_create
/* This will call the real pthread function, not our wrapper */
if (pthread_create(&thread, &attr, GC_mprotect_thread, NULL) != 0)
/* This will call the real pthread function, not our wrapper. */
if (GC_inner_pthread_create(&thread, &attr, GC_mprotect_thread, NULL) != 0)
ABORT("pthread_create failed");
(void)pthread_attr_destroy(&attr);

Expand Down
10 changes: 10 additions & 0 deletions pthread_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@
# define INIT_REAL_SYMS() (void)0
#endif /* GC_WIN32_THREADS */

#if defined(MPROTECT_VDB) && defined(DARWIN)
GC_INNER int GC_inner_pthread_create(pthread_t *t,
GC_PTHREAD_CREATE_CONST pthread_attr_t *a,
void *(*fn)(void *), void *arg)
{
INIT_REAL_SYMS();
return REAL_FUNC(pthread_create)(t, a, fn, arg);
}
#endif

#ifndef GC_ALWAYS_MULTITHREADED
GC_INNER GC_bool GC_need_to_lock = FALSE;
#endif
Expand Down

0 comments on commit 40eef90

Please sign in to comment.