Skip to content

Commit 895f36b

Browse files
authored
flambda-backend: Reinitialize IO mutexes in systhreads5 (#2160)
1 parent c04a4c8 commit 895f36b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

otherlibs/systhreads/st_stubs.c

+11
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ static void caml_thread_remove_and_free(caml_thread_t th)
463463

464464
static void caml_thread_reinitialize(void)
465465
{
466+
struct channel * chan;
466467
caml_thread_t th, next;
467468

468469
th = Active_thread->next;
@@ -484,6 +485,16 @@ static void caml_thread_reinitialize(void)
484485
s->lock (busy = 1) */
485486
struct caml_locking_scheme *s = atomic_load(&Locking_scheme(Caml_state->id));
486487
s->reinitialize_after_fork(s->context);
488+
489+
/* Reinitialize IO mutexes, in case the fork happened while another thread
490+
had locked the channel. If so, we're likely in an inconsistent state,
491+
but we may be able to proceed anyway. */
492+
caml_plat_mutex_init(&caml_all_opened_channels_mutex);
493+
for (chan = caml_all_opened_channels;
494+
chan != NULL;
495+
chan = chan->next) {
496+
caml_plat_mutex_init(&chan->mutex);
497+
}
487498
}
488499

489500
CAMLprim value caml_thread_join(value th);

runtime/caml/io.h

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ CAMLextern void (*caml_channel_mutex_unlock) (struct channel *);
105105
CAMLextern void (*caml_channel_mutex_unlock_exn) (void);
106106

107107
CAMLextern struct channel * caml_all_opened_channels;
108+
CAMLextern caml_plat_mutex caml_all_opened_channels_mutex;
108109

109110
#define Lock(channel) \
110111
if (caml_channel_mutex_lock != NULL) (*caml_channel_mutex_lock)(channel)

0 commit comments

Comments
 (0)