Changeset ba12b44 for select.c


Ignore:
Timestamp:
May 23, 2011, 8:57:46 PM (10 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.8, release-1.9
Children:
1d21d9f
Parents:
117b2ba
git-author:
David Benjamin <davidben@mit.edu> (03/02/11 13:47:21)
git-committer:
David Benjamin <davidben@mit.edu> (05/23/11 20:57:46)
Message:
Add owl_select_post_task helper function

Honestly, the only thing you'd ever want to do to a message loop from
another thread is post a one-off task to it. Why Glib makes this so
difficult is beyond me.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • select.c

    r117b2ba rba12b44  
    22
    33static GMainLoop *loop = NULL;
     4static GMainContext *context;
    45static int dispatch_active = 0;
    56
     
    307308void owl_select_run_loop(void)
    308309{
    309   loop = g_main_loop_new(NULL, FALSE);
     310  context = g_main_context_default();
     311  loop = g_main_loop_new(context, FALSE);
    310312  g_main_loop_run(loop);
    311313}
     
    318320  }
    319321}
     322
     323typedef struct _owl_task { /*noproto*/
     324  void (*cb)(void *);
     325  void *cbdata;
     326  void (*destroy_cbdata)(void *);
     327} owl_task;
     328
     329static gboolean _run_task(gpointer data)
     330{
     331  owl_task *t = data;
     332  if (t->cb)
     333    t->cb(t->cbdata);
     334  return FALSE;
     335}
     336
     337static void _destroy_task(void *data)
     338{
     339  owl_task *t = data;
     340  if (t->destroy_cbdata)
     341    t->destroy_cbdata(t->cbdata);
     342  g_free(t);
     343}
     344
     345void owl_select_post_task(void (*cb)(void*), void *cbdata, void (*destroy_cbdata)(void*))
     346{
     347  GSource *source = g_idle_source_new();
     348  owl_task *t = g_new0(owl_task, 1);
     349  t->cb = cb;
     350  t->cbdata = cbdata;
     351  t->destroy_cbdata = destroy_cbdata;
     352  g_source_set_priority(source, G_PRIORITY_DEFAULT);
     353  g_source_set_callback(source, _run_task, t, _destroy_task);
     354  g_source_attach(source, context);
     355  g_source_unref(source);
     356}
Note: See TracChangeset for help on using the changeset viewer.