Skip to content
Discussion options

You must be logged in to vote

Here is the concrete runtime breakdown regarding mono_gc_reference_queue_* concurrency and the weak reference alternatives:

1. Is mono_gc_reference_queue_add Thread-Safe?

No, calling mono_gc_reference_queue_add concurrently with Mono's background finalizer processing is not safe.

In src/mono/mono/metadata/gc.c:

  • mono_gc_reference_queue_add_internal enqueues items using ref_list_push(&queue->queue, entry).
  • Meanwhile, the runtime finalizer thread (finalizer_thread -> reference_queue_process_all -> reference_queue_process) iterates through queue->queue and unlinks entries via ref_list_remove_element(iter, entry) before invoking g_free(entry).
  • Although ref_list_push uses mono_atomic_cas_ptr a…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by ffbh123456
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants