Whitespace fix for __glibc_likely/__glibc_unlikely defns.
[glibc.git] / nptl / tst-cancel1.c
blob39df856116cf1567559f8ceb7e55a8fb9335bc33
1 /* Copyright (C) 2002-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <pthread.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
26 static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
27 static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
29 static int cntr;
32 static void
33 cleanup (void *arg)
35 if (arg != (void *) 42l)
36 cntr = 42;
37 else
38 cntr = 1;
42 static void *
43 tf (void *arg)
45 /* Ignore all signals. This must not have any effect on delivering
46 the cancellation signal. */
47 sigset_t ss;
49 sigfillset (&ss);
51 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
53 puts ("pthread_sigmask failed");
54 exit (1);
57 pthread_cleanup_push (cleanup, (void *) 42l);
59 int err = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
60 if (err != 0)
62 printf ("setcanceltype failed: %s\n", strerror (err));
63 exit (1);
65 /* The following code is not standard compliant: the mutex functions
66 must not be called with asynchronous cancellation enabled. */
68 err = pthread_mutex_unlock (&m2);
69 if (err != 0)
71 printf ("child: mutex_unlock failed: %s\n", strerror (err));
72 exit (1);
75 err = pthread_mutex_lock (&m1);
76 if (err != 0)
78 printf ("child: 1st mutex_lock failed: %s\n", strerror (err));
79 exit (1);
82 /* We should never come here. */
84 pthread_cleanup_pop (0);
86 return NULL;
90 static int
91 do_test (void)
93 int err;
94 pthread_t th;
95 int result = 0;
96 void *retval;
98 /* Get the mutexes. */
99 err = pthread_mutex_lock (&m1);
100 if (err != 0)
102 printf ("parent: 1st mutex_lock failed: %s\n", strerror (err));
103 return 1;
105 err = pthread_mutex_lock (&m2);
106 if (err != 0)
108 printf ("parent: 2nd mutex_lock failed: %s\n", strerror (err));
109 return 1;
112 err = pthread_create (&th, NULL, tf, NULL);
113 if (err != 0)
115 printf ("create failed: %s\n", strerror (err));
116 return 1;
119 err = pthread_mutex_lock (&m2);
120 if (err != 0)
122 printf ("parent: 3rd mutex_lock failed: %s\n", strerror (err));
123 return 1;
126 err = pthread_cancel (th);
127 if (err != 0)
129 printf ("cancel failed: %s\n", strerror (err));
130 return 1;
133 err = pthread_join (th, &retval);
134 if (err != 0)
136 printf ("join failed: %s\n", strerror (err));
137 return 1;
140 if (retval != PTHREAD_CANCELED)
142 printf ("wrong return value: %p\n", retval);
143 result = 1;
146 if (cntr == 42)
148 puts ("cleanup handler called with wrong argument");
149 result = 1;
151 else if (cntr != 1)
153 puts ("cleanup handling not called");
154 result = 1;
157 return result;
161 #define TEST_FUNCTION do_test ()
162 #include "../test-skeleton.c"