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