Hi All,

I'm trying to get my head around handling signals in a threaded app.
However, I keep just getting a stack backtrace written to stderr. As far
as I can tell my signal handler never gets a look in.


In main(), before any other threads are created, I have this code to
block all signals and then create the signal-catching thread.


  /* Block all signals */
  sigfillset(&signal_set);
  pthread_sigmask(SIG_BLOCK, &signal_set, NULL);

  /* Signal handler thread */
  if (pthread_create(&app.control_threads.tid2, NULL,
                     &BT_Signal_Catcher, "Signal Thread"))
  {
    BT_Log(log_CRITICAL, "Unable to create Signal Thread - nasty!");
    abort();
  }

The signal-catching thread, which claims the signals, is:

  #define NO_SIGNAL -1
  int signal_handled = NO_SIGNAL;
  pthread_mutex_t signal_mutex = PTHREAD_MUTEX_INITIALIZER;

  void *BT_Signal_Catcher(void *vptr_args)
  {
    sigset_t signal_set;
    int sig;
  
    UNUSED(vptr_args);
  
    BugF("Function:BT_Signal_Catcher");
  
    /* "go" gets set to false at end of BT_Routine_Exit */
    while(app.control_threads.go)
    {
      /* Wait for any signal */
      sigfillset(&signal_set);
      Bug("SigThrd: going to sleep...");
      sigwait(&signal_set, &sig);
      Bug("SigThrd: woken up!");
  
      /* Set value, which is tested in null handler */
      pthread_mutex_lock(&signal_mutex);
      signal_handled = sig;
      pthread_mutex_unlock(&signal_mutex);
    }
  
    BugF("Function(exit):BT_Signal_Catcher");
    pthread_exit(NULL);
  }

Finally, at the beginning of the wimp polling loop I've got:

  BOOL BT_Null_Handler(event_pollblock *event, void *reference)
  {
    UNUSED(event);
    UNUSED(reference);
  
    pthread_yield();
  
    if (signal_handled != NO_SIGNAL)
    {
      Debug_Printf("arglglgey bargle %s", strsignal(signal_handled));
      pthread_mutex_lock(&signal_mutex);
      signal_handled = NO_SIGNAL;
      pthread_mutex_unlock(&signal_mutex);
      abort();
    }
    ...

Everything seems to go OK, and the signal thread goes to sleep as
expected, but it doesn't appear to wake up when I generate a synchronous
signal. What am I doing wrong?

Thanks a lot,
Adam


-- 
Adam Richardson          Carpe Diem
http://www.snowstone.org.uk/riscos/

_______________________________________________
GCCSDK mailing list [email protected]
Bugzilla: http://www.riscos.info/bugzilla/index.cgi
List Info: http://www.riscos.info/mailman/listinfo/gcc
Main Page: http://www.riscos.info/index.php/GCCSDK

Reply via email to