Skip to content

Commit

Permalink
Reinitialize IO mutexes in systhreads5 (#2160)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNumbat authored Dec 14, 2023
1 parent 13a5316 commit 4a0ab74
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ocaml/otherlibs/systhreads/st_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ static void caml_thread_remove_and_free(caml_thread_t th)

static void caml_thread_reinitialize(void)
{
struct channel * chan;
caml_thread_t th, next;

th = Active_thread->next;
Expand All @@ -484,6 +485,16 @@ static void caml_thread_reinitialize(void)
s->lock (busy = 1) */
struct caml_locking_scheme *s = atomic_load(&Locking_scheme(Caml_state->id));
s->reinitialize_after_fork(s->context);

/* Reinitialize IO mutexes, in case the fork happened while another thread
had locked the channel. If so, we're likely in an inconsistent state,
but we may be able to proceed anyway. */
caml_plat_mutex_init(&caml_all_opened_channels_mutex);
for (chan = caml_all_opened_channels;
chan != NULL;
chan = chan->next) {
caml_plat_mutex_init(&chan->mutex);
}
}

CAMLprim value caml_thread_join(value th);
Expand Down
1 change: 1 addition & 0 deletions ocaml/runtime/caml/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ CAMLextern void (*caml_channel_mutex_unlock) (struct channel *);
CAMLextern void (*caml_channel_mutex_unlock_exn) (void);

CAMLextern struct channel * caml_all_opened_channels;
CAMLextern caml_plat_mutex caml_all_opened_channels_mutex;

#define Lock(channel) \
if (caml_channel_mutex_lock != NULL) (*caml_channel_mutex_lock)(channel)
Expand Down

0 comments on commit 4a0ab74

Please sign in to comment.