Fix on-demand initialization race conditions [sgen]. (mono/mono#18170)
authorJay Krell <jaykrell@microsoft.com>
Fri, 13 Dec 2019 03:55:58 +0000 (19:55 -0800)
committermonojenkins <jo.shields+jenkins@xamarin.com>
Fri, 13 Dec 2019 03:55:58 +0000 (04:55 +0100)
Fix on-demand initialization race conditions [sgen].

Extracted from https://github.com/mono/mono/pull/18150 which reviewer said was too big.

Commit migrated from https://github.com/mono/mono/commit/9cefdf59adc59b085ff8cf187fabb17aa9a40737

src/mono/mono/metadata/sgen-toggleref.c

index 3fa53bd..831d0aa 100644 (file)
@@ -213,13 +213,14 @@ mono_gc_toggleref_register_callback (MonoToggleRefStatus (*proccess_toggleref) (
 static MonoToggleRefStatus
 test_toggleref_callback (MonoObject *obj)
 {
-       static MonoClassField *mono_toggleref_test_field;
        MonoToggleRefStatus status = MONO_TOGGLE_REF_DROP;
 
-       if (!mono_toggleref_test_field) {
+       MONO_STATIC_POINTER_INIT (MonoClassField, mono_toggleref_test_field)
+
                mono_toggleref_test_field = mono_class_get_field_from_name_full (mono_object_class (obj), "__test", NULL);
                g_assert (mono_toggleref_test_field);
-       }
+
+       MONO_STATIC_POINTER_INIT_END (MonoClassField, mono_toggleref_test_field)
 
        /* In coop mode, important to not call a helper that will pin obj! */
        mono_field_get_value_internal (obj, mono_toggleref_test_field, &status);