Update.
[glibc.git] / hurd / thread-cancel.c
blobc8b6cf52cdd3dcb9b8b004981587e9bb9e0e6f5b
1 /* Thread cancellation support.
2 Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <hurd/signal.h>
21 #include <hurd/interrupt.h>
22 #include <assert.h>
23 #include <thread_state.h>
26 /* See hurdsig.c. */
27 extern mach_port_t _hurdsig_abort_rpcs (struct hurd_sigstate *ss,
28 int signo, int sigthread,
29 struct machine_thread_all_state *,
30 int *state_change,
31 mach_port_t *reply_port,
32 mach_msg_type_name_t reply_port_type,
33 int untraced);
35 error_t
36 hurd_thread_cancel (thread_t thread)
38 struct hurd_sigstate *ss = _hurd_thread_sigstate (thread);
39 struct machine_thread_all_state state;
40 int state_change;
41 error_t err;
43 if (! ss)
44 return EINVAL;
45 if (ss == _hurd_self_sigstate ())
47 /* We are cancelling ourselves, so it is easy to succeed
48 quickly. Since this function is not a cancellation point, we
49 just leave the flag set pending the next cancellation point
50 (hurd_check_cancel or RPC) and return success. */
51 ss->cancel = 1;
52 return 0;
55 assert (! __spin_lock_locked (&ss->critical_section_lock));
56 __spin_lock (&ss->critical_section_lock);
57 __spin_lock (&ss->lock);
58 err = __thread_suspend (thread);
59 __spin_unlock (&ss->lock);
61 if (! err)
63 /* Set the flag telling the thread its operation is being cancelled. */
64 ss->cancel = 1;
66 /* Interrupt any interruptible RPC now in progress. */
67 state.set = 0;
68 _hurdsig_abort_rpcs (ss, 0, 0, &state, &state_change, NULL, 0, 0);
69 if (state_change)
70 err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
71 (natural_t *) &state.basic,
72 MACHINE_THREAD_STATE_COUNT);
74 if (ss->cancel_hook)
75 /* The code being cancelled has a special wakeup function.
76 Calling this should make the thread wake up and check the
77 cancellation flag. */
78 (*ss->cancel_hook) ();
80 __thread_resume (thread);
83 _hurd_critical_section_unlock (ss);
84 return err;
88 int
89 hurd_check_cancel (void)
91 struct hurd_sigstate *ss = _hurd_self_sigstate ();
92 int cancel;
94 __spin_lock (&ss->lock);
95 assert (! __spin_lock_locked (&ss->critical_section_lock));
96 cancel = ss->cancel;
97 ss->cancel = 0;
98 __spin_unlock (&ss->lock);
100 return cancel;