Changeset 87833a8


Ignore:
Timestamp:
Mar 25, 2011, 3:46:46 AM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Children:
37d188f
Parents:
96ade35
git-author:
David Benjamin <davidben@mit.edu> (02/26/11 14:11:26)
git-committer:
David Benjamin <davidben@mit.edu> (03/25/11 03:46:46)
Message:
Make owl_signal_init take a signal-thread callback

Instead of pushing the work directly the main thread. This will allow us
to react to SIGINT earlier.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • owl.c

    rfafb842 r87833a8  
    354354}
    355355
    356 static void sig_handler(int sig, void *data) {
     356static gboolean sig_handler_main_thread(gpointer data) {
     357  int sig = GPOINTER_TO_INT(data);
     358
    357359  owl_function_debugmsg("Got signal %d", sig);
    358360  /* TODO: These don't need to be re-entrant anymore! */
     
    372374    owl_process_input_char(in);
    373375  }
     376  return FALSE;
     377}
     378
     379static void sig_handler(int sig, void *data) {
     380  GMainContext *context = data;
     381  GSource *source;
     382
     383  /* TODO: Special-case SIGINT so that it can interrupt outside the
     384   * event loop. */
     385
     386  /* Send a message to the main thread. */
     387  source = g_idle_source_new();
     388  g_source_set_priority(source, G_PRIORITY_DEFAULT);
     389  g_source_set_callback(source, sig_handler_main_thread,
     390                        GINT_TO_POINTER(sig), NULL);
     391  g_source_attach(source, context);
     392  g_source_unref(source);
    374393}
    375394
     
    385404  sigaddset(&sigset, SIGINT);
    386405
    387   owl_signal_init(&sigset, sig_handler, NULL);
     406  owl_signal_init(&sigset, sig_handler, g_main_context_default());
    388407}
    389408
  • signal.c

    rfafb842 r87833a8  
    1212
    1313static gpointer signal_thread_func(gpointer data);
    14 static gboolean signal_thunk(gpointer data);
    1514
     15/* Initializes the signal thread to listen for 'set' on a dedicated
     16 * thread. 'callback' is called *on the signal thread* when a signal
     17 * is received.
     18 *
     19 * This function /must/ be called before any other threads are
     20 * created. (Otherwise the signals will not get blocked correctly.) */
    1621void owl_signal_init(const sigset_t *set, void (*callback)(int, void*), void *data) {
    1722  GError *error = NULL;
     
    2328  pthread_sigmask(SIG_BLOCK, set, NULL);
    2429  /* Spawn a dedicated thread to sigwait. */
    25   signal_thread = g_thread_create(signal_thread_func, g_main_context_default(),
    26                                   FALSE, &error);
     30  signal_thread = g_thread_create(signal_thread_func, NULL, FALSE, &error);
    2731  if (signal_thread == NULL) {
    2832    fprintf(stderr, "Failed to create signal thread: %s\n", error->message);
     
    3236
    3337static gpointer signal_thread_func(gpointer data) {
    34   GMainContext *context = data;
    35 
    3638  while (1) {
    37     GSource *source;
    38     int signal;
     39     int signal;
    3940    int ret;
    4041
     
    4445      continue;
    4546
    46     /* Send a message to the other main. */
    47     source = g_idle_source_new();
    48     g_source_set_priority(source, G_PRIORITY_DEFAULT);
    49     g_source_set_callback(source, signal_thunk, GINT_TO_POINTER(signal), NULL);
    50     g_source_attach(source, context);
    51     g_source_unref(source);
     47    signal_cb(signal, signal_cbdata);
    5248  }
    5349  return NULL;
    5450}
    55 
    56 static gboolean signal_thunk(gpointer data) {
    57   signal_cb(GPOINTER_TO_INT(data), signal_cbdata);
    58   return FALSE;
    59 }
Note: See TracChangeset for help on using the changeset viewer.