Changeset 111850c for select.c


Ignore:
Timestamp:
Mar 25, 2011, 3:55:41 AM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Children:
25f7c57
Parents:
6eaafb0
git-author:
David Benjamin <davidben@mit.edu> (03/02/11 13:47:21)
git-committer:
David Benjamin <davidben@mit.edu> (03/25/11 03:55:41)
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

    r567de81 r111850c  
    22
    33static GMainLoop *loop = NULL;
     4static GMainContext *context;
    45static int dispatch_active = 0;
    56
     
    310311void owl_select_run_loop(void)
    311312{
    312   loop = g_main_loop_new(NULL, FALSE);
     313  context = g_main_context_default();
     314  loop = g_main_loop_new(context, FALSE);
    313315  g_main_loop_run(loop);
    314316}
     
    321323  }
    322324}
     325
     326typedef struct _owl_task { /*noproto*/
     327  void (*cb)(void *);
     328  void *cbdata;
     329  void (*destroy_cbdata)(void *);
     330} owl_task;
     331
     332static gboolean _run_task(gpointer data)
     333{
     334  owl_task *t = data;
     335  if (t->cb)
     336    t->cb(t->cbdata);
     337  return FALSE;
     338}
     339
     340static void _destroy_task(void *data)
     341{
     342  owl_task *t = data;
     343  if (t->destroy_cbdata)
     344    t->destroy_cbdata(t->cbdata);
     345  g_free(t);
     346}
     347
     348void owl_select_post_task(void (*cb)(void*), void *cbdata, void (*destroy_cbdata)(void*))
     349{
     350  GSource *source = g_idle_source_new();
     351  owl_task *t = g_new0(owl_task, 1);
     352  t->cb = cb;
     353  t->cbdata = cbdata;
     354  t->destroy_cbdata = destroy_cbdata;
     355  g_source_set_priority(source, G_PRIORITY_DEFAULT);
     356  g_source_set_callback(source, _run_task, t, _destroy_task);
     357  g_source_attach(source, context);
     358  g_source_unref(source);
     359}
Note: See TracChangeset for help on using the changeset viewer.