Back to index

glibc  2.9
thread-cancel.c
Go to the documentation of this file.
00001 /* Thread cancellation support.
00002    Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <hurd/signal.h>
00021 #include <hurd/interrupt.h>
00022 #include <assert.h>
00023 #include <thread_state.h>
00024 
00025 
00026 /* See hurdsig.c.  */
00027 extern mach_port_t _hurdsig_abort_rpcs (struct hurd_sigstate *ss,
00028                                    int signo, int sigthread,
00029                                    struct machine_thread_all_state *,
00030                                    int *state_change,
00031                                    mach_port_t *reply_port,
00032                                    mach_msg_type_name_t reply_port_type,
00033                                    int untraced);
00034 
00035 error_t
00036 hurd_thread_cancel (thread_t thread)
00037 {
00038   struct hurd_sigstate *ss = _hurd_thread_sigstate (thread);
00039   struct machine_thread_all_state state;
00040   int state_change;
00041   error_t err;
00042 
00043   if (! ss)
00044     return EINVAL;
00045   if (ss == _hurd_self_sigstate ())
00046     {
00047       /* We are cancelling ourselves, so it is easy to succeed
00048         quickly.  Since this function is not a cancellation point, we
00049         just leave the flag set pending the next cancellation point
00050         (hurd_check_cancel or RPC) and return success.  */
00051       ss->cancel = 1;
00052       return 0;
00053     }
00054 
00055   assert (! __spin_lock_locked (&ss->critical_section_lock));
00056   __spin_lock (&ss->critical_section_lock);
00057   __spin_lock (&ss->lock);
00058   err = __thread_suspend (thread);
00059   __spin_unlock (&ss->lock);
00060 
00061   if (! err)
00062     {
00063       /* Set the flag telling the thread its operation is being cancelled.  */
00064       ss->cancel = 1;
00065 
00066       /* Interrupt any interruptible RPC now in progress.  */
00067       state.set = 0;
00068       _hurdsig_abort_rpcs (ss, 0, 0, &state, &state_change, NULL, 0, 0);
00069       if (state_change)
00070        err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
00071                               (natural_t *) &state.basic,
00072                               MACHINE_THREAD_STATE_COUNT);
00073 
00074       if (ss->cancel_hook)
00075        /* The code being cancelled has a special wakeup function.
00076           Calling this should make the thread wake up and check the
00077           cancellation flag.  */
00078        (*ss->cancel_hook) ();
00079 
00080       __thread_resume (thread);
00081     }
00082 
00083   _hurd_critical_section_unlock (ss);
00084   return err;
00085 }
00086 
00087 
00088 int
00089 hurd_check_cancel (void)
00090 {
00091   struct hurd_sigstate *ss = _hurd_self_sigstate ();
00092   int cancel;
00093 
00094   __spin_lock (&ss->lock);
00095   assert (! __spin_lock_locked (&ss->critical_section_lock));
00096   cancel = ss->cancel;
00097   ss->cancel = 0;
00098   __spin_unlock (&ss->lock);
00099 
00100   return cancel;
00101 }