view libgui/src/thread-manager.cc @ 17958:1adf3710bb68

Working CTRL-C handling implementation for Win32. * libgui/src/thread-manager.cc (sighandlers.h): New include. (windows_thread_manager::interrupt): Call w32_raise_sigint instead of ::raise. * libinterp/corefcn/sighandlers.h (w32_raise_sigint): New declatation. * libinterp/cirefcn/sighandlers.cc (user_abort): Forward declare. (class w32_interrupt_manager): New singleton helper class. (w32_raise_sigint): New function. (user_abort): Call w32_interrupt_manager::octave_jump_to_enclosing_context instead of octave_jump_to_enclosing_context on Win32 platform. (sigint_handler): Call w32_interrupt_manager::user_abort instead of user_abort on Win32 platform. (octave_catch_interrupts, octave_ignore_interrupts, octave_set_interrupt_handler): Call w32_interrupt_manager::init on Win32 platform. * liboctave/util/oct-rl-edit.c (octave_rl_initialize): Set rl_catch_signals to 0 on Win32 platform.
author Michael Goffioul <michael.goffioul@gmail.com>
date Tue, 19 Nov 2013 14:21:38 -0500
parents b5bf26a054bd
children 54784619350b
line wrap: on
line source

/*

Copyright (C) 2013 John W. Eaton

This file is part of Octave.

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, see
<http://www.gnu.org/licenses/>.

*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#if defined (__WIN32__) && ! defined (__CYGWIN__)
#include <windows.h>
#else
#include <pthread.h>
#endif

#include <sys/types.h>
#include <signal.h>

#include "sighandlers.h"
#include "thread-manager.h"

#if defined (__WIN32__) && ! defined (__CYGWIN__)

class windows_thread_manager : public octave_base_thread_manager
{
public:

  windows_thread_manager (void) : octave_base_thread_manager () { }

  void register_current_thread (void) { }

  void interrupt (void)
  {
    w32_raise_sigint ();
  }
};

#else

class pthread_thread_manager : public octave_base_thread_manager
{
public:

  pthread_thread_manager (void)
    : octave_base_thread_manager (), my_thread (), initialized (false)
  { }

  void register_current_thread (void)
  {
    my_thread = pthread_self ();
    initialized = true;
  }

  void interrupt (void)
  {
    if (initialized)
      pthread_kill (my_thread, SIGINT);
  }

private:

  pthread_t my_thread;

  bool initialized;
};

#endif

octave_thread_manager::octave_thread_manager (void)
  : rep (octave_thread_manager::create_rep ())
{ }

static void
block_or_unblock_signal (int how, int sig)
{
  sigset_t signal_mask;

  sigemptyset (&signal_mask);

  sigaddset (&signal_mask, sig);

#if defined (__WIN32__) && ! defined (__CYGWIN__)
  sigprocmask (how, &signal_mask, 0);
#else
  pthread_sigmask (how, &signal_mask, 0);
#endif
}

void
octave_thread_manager::block_interrupt_signal (void)
{
  block_or_unblock_signal (SIG_BLOCK, SIGINT);
}

void
octave_thread_manager::unblock_interrupt_signal (void)
{
  block_or_unblock_signal (SIG_UNBLOCK, SIGINT);
}

octave_base_thread_manager *
octave_thread_manager::create_rep (void)
{
#if defined (__WIN32__) && ! defined (__CYGWIN__)
  return new windows_thread_manager ();
#else
  return new pthread_thread_manager ();
#endif
}