]>
Commit | Line | Data |
---|---|---|
1 | /* Copyright (C) 2002-2019 Free Software Foundation, Inc. | |
2 | This file is part of the GNU C Library. | |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
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 <pthread.h> | |
20 | #include <signal.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | ||
25 | ||
26 | static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER; | |
27 | static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER; | |
28 | ||
29 | static int cntr; | |
30 | ||
31 | ||
32 | static void | |
33 | cleanup (void *arg) | |
34 | { | |
35 | if (arg != (void *) 42l) | |
36 | cntr = 42; | |
37 | else | |
38 | cntr = 1; | |
39 | } | |
40 | ||
41 | ||
42 | static void * | |
43 | tf (void *arg) | |
44 | { | |
45 | /* Ignore all signals. This must not have any effect on delivering | |
46 | the cancellation signal. */ | |
47 | sigset_t ss; | |
48 | ||
49 | sigfillset (&ss); | |
50 | ||
51 | if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0) | |
52 | { | |
53 | puts ("pthread_sigmask failed"); | |
54 | exit (1); | |
55 | } | |
56 | ||
57 | pthread_cleanup_push (cleanup, (void *) 42l); | |
58 | ||
59 | int err = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL); | |
60 | if (err != 0) | |
61 | { | |
62 | printf ("setcanceltype failed: %s\n", strerror (err)); | |
63 | exit (1); | |
64 | } | |
65 | /* The following code is not standard compliant: the mutex functions | |
66 | must not be called with asynchronous cancellation enabled. */ | |
67 | ||
68 | err = pthread_mutex_unlock (&m2); | |
69 | if (err != 0) | |
70 | { | |
71 | printf ("child: mutex_unlock failed: %s\n", strerror (err)); | |
72 | exit (1); | |
73 | } | |
74 | ||
75 | err = pthread_mutex_lock (&m1); | |
76 | if (err != 0) | |
77 | { | |
78 | printf ("child: 1st mutex_lock failed: %s\n", strerror (err)); | |
79 | exit (1); | |
80 | } | |
81 | ||
82 | /* We should never come here. */ | |
83 | ||
84 | pthread_cleanup_pop (0); | |
85 | ||
86 | return NULL; | |
87 | } | |
88 | ||
89 | ||
90 | static int | |
91 | do_test (void) | |
92 | { | |
93 | int err; | |
94 | pthread_t th; | |
95 | int result = 0; | |
96 | void *retval; | |
97 | ||
98 | /* Get the mutexes. */ | |
99 | err = pthread_mutex_lock (&m1); | |
100 | if (err != 0) | |
101 | { | |
102 | printf ("parent: 1st mutex_lock failed: %s\n", strerror (err)); | |
103 | return 1; | |
104 | } | |
105 | err = pthread_mutex_lock (&m2); | |
106 | if (err != 0) | |
107 | { | |
108 | printf ("parent: 2nd mutex_lock failed: %s\n", strerror (err)); | |
109 | return 1; | |
110 | } | |
111 | ||
112 | err = pthread_create (&th, NULL, tf, NULL); | |
113 | if (err != 0) | |
114 | { | |
115 | printf ("create failed: %s\n", strerror (err)); | |
116 | return 1; | |
117 | } | |
118 | ||
119 | err = pthread_mutex_lock (&m2); | |
120 | if (err != 0) | |
121 | { | |
122 | printf ("parent: 3rd mutex_lock failed: %s\n", strerror (err)); | |
123 | return 1; | |
124 | } | |
125 | ||
126 | err = pthread_cancel (th); | |
127 | if (err != 0) | |
128 | { | |
129 | printf ("cancel failed: %s\n", strerror (err)); | |
130 | return 1; | |
131 | } | |
132 | ||
133 | err = pthread_join (th, &retval); | |
134 | if (err != 0) | |
135 | { | |
136 | printf ("join failed: %s\n", strerror (err)); | |
137 | return 1; | |
138 | } | |
139 | ||
140 | if (retval != PTHREAD_CANCELED) | |
141 | { | |
142 | printf ("wrong return value: %p\n", retval); | |
143 | result = 1; | |
144 | } | |
145 | ||
146 | if (cntr == 42) | |
147 | { | |
148 | puts ("cleanup handler called with wrong argument"); | |
149 | result = 1; | |
150 | } | |
151 | else if (cntr != 1) | |
152 | { | |
153 | puts ("cleanup handling not called"); | |
154 | result = 1; | |
155 | } | |
156 | ||
157 | return result; | |
158 | } | |
159 | ||
160 | ||
161 | #define TEST_FUNCTION do_test () | |
162 | #include "../test-skeleton.c" |