Blame hurd/thread-cancel.c

Packit 6c4009
/* Thread cancellation support.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <hurd/signal.h>
Packit 6c4009
#include <hurd/interrupt.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <thread_state.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* See hurdsig.c.  */
Packit 6c4009
extern mach_port_t _hurdsig_abort_rpcs (struct hurd_sigstate *ss,
Packit 6c4009
					int signo, int sigthread,
Packit 6c4009
					struct machine_thread_all_state *,
Packit 6c4009
					int *state_change,
Packit 6c4009
					mach_port_t *reply_port,
Packit 6c4009
					mach_msg_type_name_t reply_port_type,
Packit 6c4009
					int untraced);
Packit 6c4009
Packit 6c4009
error_t
Packit 6c4009
hurd_thread_cancel (thread_t thread)
Packit 6c4009
{
Packit 6c4009
  struct hurd_sigstate *ss = _hurd_thread_sigstate (thread);
Packit 6c4009
  struct machine_thread_all_state state;
Packit 6c4009
  int state_change;
Packit 6c4009
  error_t err;
Packit 6c4009
Packit 6c4009
  if (! ss)
Packit 6c4009
    return EINVAL;
Packit 6c4009
  if (ss == _hurd_self_sigstate ())
Packit 6c4009
    {
Packit 6c4009
      /* We are cancelling ourselves, so it is easy to succeed
Packit 6c4009
	 quickly.  Since this function is not a cancellation point, we
Packit 6c4009
	 just leave the flag set pending the next cancellation point
Packit 6c4009
	 (hurd_check_cancel or RPC) and return success.  */
Packit 6c4009
      ss->cancel = 1;
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  assert (! __spin_lock_locked (&ss->critical_section_lock));
Packit 6c4009
  __spin_lock (&ss->critical_section_lock);
Packit 6c4009
  __spin_lock (&ss->lock);
Packit 6c4009
  err = __thread_suspend (thread);
Packit 6c4009
  __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
  if (! err)
Packit 6c4009
    {
Packit 6c4009
      /* Set the flag telling the thread its operation is being cancelled.  */
Packit 6c4009
      ss->cancel = 1;
Packit 6c4009
Packit 6c4009
      /* Interrupt any interruptible RPC now in progress.  */
Packit 6c4009
      state.set = 0;
Packit 6c4009
      _hurdsig_abort_rpcs (ss, 0, 0, &state, &state_change, NULL, 0, 0);
Packit 6c4009
      if (state_change)
Packit 6c4009
	err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
Packit 6c4009
				  (natural_t *) &state.basic,
Packit 6c4009
				  MACHINE_THREAD_STATE_COUNT);
Packit 6c4009
Packit 6c4009
      if (ss->cancel_hook)
Packit 6c4009
	/* The code being cancelled has a special wakeup function.
Packit 6c4009
	   Calling this should make the thread wake up and check the
Packit 6c4009
	   cancellation flag.  */
Packit 6c4009
	(*ss->cancel_hook) ();
Packit 6c4009
Packit 6c4009
      __thread_resume (thread);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  _hurd_critical_section_unlock (ss);
Packit 6c4009
  return err;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
hurd_check_cancel (void)
Packit 6c4009
{
Packit 6c4009
  struct hurd_sigstate *ss = _hurd_self_sigstate ();
Packit 6c4009
  int cancel;
Packit 6c4009
Packit 6c4009
  __spin_lock (&ss->lock);
Packit 6c4009
  assert (! __spin_lock_locked (&ss->critical_section_lock));
Packit 6c4009
  cancel = ss->cancel;
Packit 6c4009
  ss->cancel = 0;
Packit 6c4009
  __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
  return cancel;
Packit 6c4009
}