[m-rev.] for review: fix a bug with cloning thread local mutables
Julien Fischer
jfischer at opturion.com
Tue Oct 13 22:18:55 AEDT 2026
For review by Peter.
------------------------------
Fix a bug with cloning thread local mutables.
In low-level C parallel grades, we clone the thread local mutables array when
we spawn a new thread. Access to this array is protected by a mutex. The
runtime code responsible for cloning the array acquires the lock for the
new copy of the array to which values are being written, instead of the old
copy, from which values are being read. It is the latter which needs to
be protected by mutex, not the former. (There are no other references to
the new mutables array at the point it is cloned.)
runtime/mercury_thread.[ch]:
Acquire the mutex for the copy of the thread local mutables we
are reading from, not writing to.
Delete the const qualifier from the argument of
MR_clone_thread_local_mutables(), as acquiring the mutex will
modify it.
Julien.
diff --git a/runtime/mercury_thread.c b/runtime/mercury_thread.c
index 930efab44..cfb3856a7 100644
--- a/runtime/mercury_thread.c
+++ b/runtime/mercury_thread.c
@@ -543,17 +543,17 @@ MR_create_thread_local_mutables(MR_Unsigned numslots)
}
MR_ThreadLocalMuts *
-MR_clone_thread_local_mutables(const MR_ThreadLocalMuts *old_muts)
+MR_clone_thread_local_mutables(MR_ThreadLocalMuts *old_muts)
{
MR_ThreadLocalMuts *new_muts;
new_muts = MR_create_thread_local_mutables(MR_num_thread_local_mutables);
- MR_LOCK(&new_muts->MR_tlm_lock, "MR_clone_thread_local_mutables");
+ MR_LOCK(&old_muts->MR_tlm_lock, "MR_clone_thread_local_mutables");
for (MR_Unsigned i = 0; i < MR_num_thread_local_mutables; i++) {
new_muts->MR_tlm_values[i] = old_muts->MR_tlm_values[i];
}
- MR_UNLOCK(&new_muts->MR_tlm_lock, "MR_clone_thread_local_mutables");
+ MR_UNLOCK(&old_muts->MR_tlm_lock, "MR_clone_thread_local_mutables");
return new_muts;
}
diff --git a/runtime/mercury_thread.h b/runtime/mercury_thread.h
index cf81027e6..00dc2f740 100644
--- a/runtime/mercury_thread.h
+++ b/runtime/mercury_thread.h
@@ -369,7 +369,7 @@ extern MR_ThreadLocalMuts
*MR_create_thread_local_mutables(
// Make a copy of a thread-local mutable array.
extern MR_ThreadLocalMuts *MR_clone_thread_local_mutables(
- const MR_ThreadLocalMuts *old_muts);
+ MR_ThreadLocalMuts *old_muts);
#define MR_THREAD_LOCAL_MUTABLES \
(MR_ENGINE(MR_eng_this_context)->MR_ctxt_thread_local_mutables)
More information about the reviews
mailing list