Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mono] Fix class initialization spurious wakeups #96903

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/mono/mono/metadata/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,21 +457,24 @@ mono_runtime_class_init_full (MonoVTable *vtable, MonoError *error)
* on this cond var.
*/

HANDLE_FUNCTION_ENTER ();

retry_top:
(void)0; // appease C compiler; label must preceed a statement not a var declaration

gboolean ret = FALSE;

mono_type_initialization_lock ();
/* double check... */
if (vtable->initialized) {
mono_type_initialization_unlock ();
return TRUE;
goto return_true;
}

gboolean do_initialization = FALSE;
TypeInitializationLock *lock = NULL;
gboolean pending_tae = FALSE;

gboolean ret = FALSE;

HANDLE_FUNCTION_ENTER ();

if (vtable->init_failed) {
/* The type initialization already failed once, rethrow the same exception */
Expand Down Expand Up @@ -614,8 +617,8 @@ mono_runtime_class_init_full (MonoVTable *vtable, MonoError *error)
if (!lock->done) {
int timeout_ms = 500;
int wait_result = mono_coop_cond_timedwait (&lock->cond, &lock->mutex, timeout_ms);
if (wait_result == -1) {
/* timed out - go around again from the beginning. If we got here
if (wait_result == -1 || (wait_result == 0 && !lock->done)) {
/* timed out or spurious wakeup - go around again from the beginning. If we got here
* from the "is_blocked = FALSE" case, above (another thread was
* blocked on the current thread, but on a lock that was already
* done but it didn't get to wake up yet), then it might still be
Expand Down Expand Up @@ -646,7 +649,7 @@ mono_runtime_class_init_full (MonoVTable *vtable, MonoError *error)
g_hash_table_remove (type_initialization_hash, vtable);
mono_type_initialization_unlock ();
goto retry_top;
} else if (wait_result == 0) {
} else if (wait_result == 0 && lock->done) {
/* Success: we were signaled that the other thread is done. Proceed */
} else {
g_assert_not_reached ();
Expand Down
Loading