Skip to content

Commit

Permalink
Leave blocking section on callback while polling (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
btj authored Dec 3, 2021
1 parent 34a6d4f commit 6f21f44
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/ml_glib.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,15 @@ ML_1 (g_source_remove, Int_val, Unit)

static GPollFunc poll_func = NULL;

int polling = 0;

static gint ml_poll (GPollFD *ufds, guint nfsd, gint timeout)
{
gint res;
caml_enter_blocking_section();
polling = 1;
res = poll_func(ufds, nfsd, timeout);
polling = 0;
caml_leave_blocking_section();
return res;
}
Expand Down
2 changes: 2 additions & 0 deletions src/ml_glib.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ CAMLexport GSList *GSList_val (value list, value_out);

CAMLexport void ml_register_exn_map (GQuark domain, char *caml_name);
CAMLexport void ml_raise_gerror(GError *) Noreturn;

extern int polling;
17 changes: 16 additions & 1 deletion src/ml_gobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <caml/memory.h>
#include <caml/callback.h>
#include <caml/fail.h>
#include <caml/threads.h>

#include "wrappers.h"
#include "ml_glib.h"
Expand Down Expand Up @@ -178,7 +179,7 @@ static void notify_destroy(gpointer unit, GClosure *c)
remove_global_root((value*)&c->data);
}

static void marshal (GClosure *closure, GValue *ret,
static void marshal_core (GClosure *closure, GValue *ret,
guint nargs, const GValue *args,
gpointer hint, gpointer marshall_data)
{
Expand All @@ -194,6 +195,20 @@ static void marshal (GClosure *closure, GValue *ret,
CAMLreturn0;
}

static void marshal (GClosure *closure, GValue *ret,
guint nargs, const GValue *args,
gpointer hint, gpointer marshall_data)
{
if (polling) { // https://github.com/garrigue/lablgtk/issues/141
caml_leave_blocking_section();
polling = 0;
marshal_core(closure, ret, nargs, args, hint, marshall_data);
polling = 1;
caml_enter_blocking_section();
} else
marshal_core(closure, ret, nargs, args, hint, marshall_data);
}

CAMLprim value ml_g_closure_new (value clos)
{
GClosure* closure = g_closure_new_simple(sizeof(GClosure), (gpointer)clos);
Expand Down

0 comments on commit 6f21f44

Please sign in to comment.