From: Ivan Maidanski Date: Sun, 19 Jul 2015 14:14:16 +0000 (+0300) Subject: Code refactoring of 'finalization extension API' X-Git-Tag: gc7_6_0~168 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8daa3d241fa9dab5067281f90292692d74ebcb87;p=gc Code refactoring of 'finalization extension API' * finalize.c (GC_object_finalized_proc, GC_set_finalizer_notify_proc): Use GC_await_finalize_proc type. * finalize.c (GC_object_finalized_proc): Use STATIC instead of "static". * finalize.c (GC_set_finalizer_notify_proc): Rename to GC_set_await_finalize_proc. * include/gc.h (GC_set_finalizer_notify_proc): Likewise. * finalize.c (GC_set_await_finalize_proc): Add LOCK/UNLOCK. * finalize.c (GC_set_await_finalize_proc): Decorate with GC_CALL. * include/gc.h (GC_set_await_finalize_proc): Likewise. * finalize.c (GC_get_await_finalize_proc): New API function. * include/gc.h (GC_await_finalize_proc): New type. * include/gc.h (GC_set_await_finalize_proc): Document. * include/gc.h (GC_get_await_finalize_proc): New API function declaration. --- diff --git a/finalize.c b/finalize.c index 33bac249..26adb11c 100644 --- a/finalize.c +++ b/finalize.c @@ -263,13 +263,27 @@ GC_API int GC_CALL GC_unregister_disappearing_link(void * * link) return 1; } -/* Finalizer proc support */ -static void (*GC_object_finalized_proc) (GC_PTR obj); +/* Finalizer callback support. */ +STATIC GC_await_finalize_proc GC_object_finalized_proc = 0; -void -GC_set_finalizer_notify_proc (void (*proc) (GC_PTR obj)) +GC_API void GC_CALL GC_set_await_finalize_proc(GC_await_finalize_proc fn) { - GC_object_finalized_proc = proc; + DCL_LOCK_STATE; + + LOCK(); + GC_object_finalized_proc = fn; + UNLOCK(); +} + +GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void) +{ + GC_await_finalize_proc fn; + DCL_LOCK_STATE; + + LOCK(); + fn = GC_object_finalized_proc; + UNLOCK(); + return fn; } #ifndef GC_LONG_REFS_NOT_NEEDED @@ -797,9 +811,8 @@ GC_INNER void GC_finalize(void) fo_set_next(prev_fo, next_fo); } GC_fo_entries--; - if (GC_object_finalized_proc) - GC_object_finalized_proc (real_ptr); + GC_object_finalized_proc(real_ptr); /* Add to list of objects awaiting finalization. */ fo_set_next(curr_fo, GC_finalize_now); diff --git a/include/gc.h b/include/gc.h index 6a6e0edd..a52408af 100644 --- a/include/gc.h +++ b/include/gc.h @@ -1172,8 +1172,14 @@ GC_API int GC_CALL GC_unregister_long_link(void ** /* link */); /* Similar to GC_unregister_disappearing_link but for a */ /* registration by either of the above two routines. */ -/* finalizer callback support */ -GC_API void GC_set_finalizer_notify_proc GC_PROTO((void (*object_finalized) (GC_PTR obj))); +/* Finalizer callback support. Invoked by the collector (with */ +/* the allocation lock held) for each unreachable object */ +/* enqueued for finalization. */ +typedef void (GC_CALLBACK * GC_await_finalize_proc)(void * /* obj */); +GC_API void GC_CALL GC_set_await_finalize_proc(GC_await_finalize_proc); +GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void); + /* Zero means no callback. The setter */ + /* and getter acquire the lock too. */ /* Returns !=0 if GC_invoke_finalizers has something to do. */ GC_API int GC_CALL GC_should_invoke_finalizers(void);