]>
Commit | Line | Data |
---|---|---|
54da5be3 | 1 | /* Thread cancellation support. |
04277e02 | 2 | Copyright (C) 1995-2019 Free Software Foundation, Inc. |
c84142e8 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
c84142e8 UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
c84142e8 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
54da5be3 RM |
18 | |
19 | #include <hurd/signal.h> | |
20 | #include <hurd/interrupt.h> | |
21 | #include <assert.h> | |
22 | #include <thread_state.h> | |
23 | ||
24 | ||
25 | /* See hurdsig.c. */ | |
26 | extern mach_port_t _hurdsig_abort_rpcs (struct hurd_sigstate *ss, | |
8f0c527e | 27 | int signo, int sigthread, |
54da5be3 RM |
28 | struct machine_thread_all_state *, |
29 | int *state_change, | |
30 | mach_port_t *reply_port, | |
31 | mach_msg_type_name_t reply_port_type, | |
32 | int untraced); | |
33 | ||
34 | error_t | |
35 | hurd_thread_cancel (thread_t thread) | |
36 | { | |
37 | struct hurd_sigstate *ss = _hurd_thread_sigstate (thread); | |
38 | struct machine_thread_all_state state; | |
39 | int state_change; | |
40 | error_t err; | |
41 | ||
42 | if (! ss) | |
43 | return EINVAL; | |
7752137a | 44 | if (ss == _hurd_self_sigstate ()) |
2d3d740b MK |
45 | { |
46 | /* We are cancelling ourselves, so it is easy to succeed | |
47 | quickly. Since this function is not a cancellation point, we | |
48 | just leave the flag set pending the next cancellation point | |
49 | (hurd_check_cancel or RPC) and return success. */ | |
50 | ss->cancel = 1; | |
51 | return 0; | |
52 | } | |
54da5be3 | 53 | |
8f0c527e RM |
54 | assert (! __spin_lock_locked (&ss->critical_section_lock)); |
55 | __spin_lock (&ss->critical_section_lock); | |
54da5be3 | 56 | __spin_lock (&ss->lock); |
54da5be3 RM |
57 | err = __thread_suspend (thread); |
58 | __spin_unlock (&ss->lock); | |
59 | ||
60 | if (! err) | |
61 | { | |
62 | /* Set the flag telling the thread its operation is being cancelled. */ | |
63 | ss->cancel = 1; | |
64 | ||
65 | /* Interrupt any interruptible RPC now in progress. */ | |
66 | state.set = 0; | |
67 | _hurdsig_abort_rpcs (ss, 0, 0, &state, &state_change, NULL, 0, 0); | |
8f0c527e | 68 | if (state_change) |
54da5be3 RM |
69 | err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR, |
70 | (natural_t *) &state.basic, | |
71 | MACHINE_THREAD_STATE_COUNT); | |
72 | ||
3cf595e5 RM |
73 | if (ss->cancel_hook) |
74 | /* The code being cancelled has a special wakeup function. | |
75 | Calling this should make the thread wake up and check the | |
76 | cancellation flag. */ | |
77 | (*ss->cancel_hook) (); | |
78 | ||
54da5be3 RM |
79 | __thread_resume (thread); |
80 | } | |
81 | ||
82 | _hurd_critical_section_unlock (ss); | |
83 | return err; | |
84 | } | |
85 | ||
86 | ||
87 | int | |
88 | hurd_check_cancel (void) | |
89 | { | |
90 | struct hurd_sigstate *ss = _hurd_self_sigstate (); | |
91 | int cancel; | |
92 | ||
93 | __spin_lock (&ss->lock); | |
8f0c527e | 94 | assert (! __spin_lock_locked (&ss->critical_section_lock)); |
54da5be3 RM |
95 | cancel = ss->cancel; |
96 | ss->cancel = 0; | |
97 | __spin_unlock (&ss->lock); | |
98 | ||
99 | return cancel; | |
100 | } |