Changeset 08e9842


Ignore:
Timestamp:
May 23, 2011, 8:57:46 PM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.8, release-1.9
Children:
dc1edbd
Parents:
6bd485e
git-author:
David Benjamin <davidben@mit.edu> (03/02/11 13:34:33)
git-committer:
David Benjamin <davidben@mit.edu> (05/23/11 20:57:46)
Message:
Cleanly destroy the signal thread on shutdown

This isn't strictly necessary here, but we should probably be in the
habit of safely cleaning up our helper threads.

Switch the implementation from GThread to pthreads directly. The
cleanest way to kill a signaling thread is to send it SIGTERM with
pthread_kill, but GThread doesn't expose that and gives no way to get at
the pthread_t.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • owl.c

    r6bd485e r08e9842  
    620620  /* Shut down everything. */
    621621  owl_zephyr_shutdown();
     622  owl_signal_shutdown();
    622623  owl_shutdown_curses();
    623624  return 0;
  • signal.c

    r6bd485e r08e9842  
    1 #include <glib.h>
    21#include <errno.h>
    32#include <pthread.h>
     
    65#include <stdlib.h>
    76
    8 static GThread *signal_thread;
     7static pthread_t signal_thread;
    98static sigset_t signal_set;
    109
     
    1211static void *signal_cbdata;
    1312
    14 static gpointer signal_thread_func(gpointer data);
     13static void *signal_thread_func(void *data);
    1514
    1615/* Initializes the signal thread to listen for 'set' on a dedicated
     
    2120 * created. (Otherwise the signals will not get blocked correctly.) */
    2221void owl_signal_init(const sigset_t *set, void (*callback)(int, void*), void *data) {
    23   GError *error = NULL;
    2422  int ret;
    2523
     
    3331  }
    3432  /* Spawn a dedicated thread to sigwait. */
    35   signal_thread = g_thread_create(signal_thread_func, NULL, FALSE, &error);
    36   if (signal_thread == NULL) {
    37     fprintf(stderr, "Failed to create signal thread: %s\n", error->message);
     33  if ((ret = pthread_create(&signal_thread, NULL,
     34                            signal_thread_func, NULL)) != 0) {
     35    errno = ret;
     36    perror("pthread_create");
    3837    exit(1);
    3938  }
    4039}
    4140
    42 static gpointer signal_thread_func(gpointer data) {
     41static void *signal_thread_func(void *data) {
    4342  while (1) {
    4443     int signal;
     
    5150
    5251    signal_cb(signal, signal_cbdata);
     52    /* Die on SIGTERM. */
     53    if (signal == SIGTERM)
     54      break;
    5355  }
    5456  return NULL;
    5557}
     58
     59void owl_signal_shutdown(void) {
     60  pthread_kill(signal_thread, SIGTERM);
     61  pthread_join(signal_thread, NULL);
     62}
Note: See TracChangeset for help on using the changeset viewer.