Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / tst-cancel10.c
blob43a0d13b4e16b3bc4cf5c0473728fe7c0976b348
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
25 static void
26 cleanup (void *arg)
28 /* Just for fun. */
29 if (pthread_cancel (pthread_self ()) != 0)
31 puts ("cleanup: cancel failed");
32 exit (1);
35 printf ("cleanup for %ld\n", (long int) arg);
39 static void *
40 tf (void *arg)
42 long int n = (long int) arg;
44 pthread_cleanup_push (cleanup, arg);
46 if (pthread_setcanceltype ((n & 1) == 0
47 ? PTHREAD_CANCEL_DEFERRED
48 : PTHREAD_CANCEL_ASYNCHRONOUS, NULL) != 0)
50 puts ("setcanceltype failed");
51 exit (1);
54 if (pthread_cancel (pthread_self ()) != 0)
56 puts ("cancel failed");
57 exit (1);
60 pthread_testcancel ();
62 /* We should never come here. */
64 pthread_cleanup_pop (0);
66 return NULL;
70 static int
71 do_test (void)
73 pthread_attr_t at;
75 if (pthread_attr_init (&at) != 0)
77 puts ("attr_init failed");
78 return 1;
81 if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
83 puts ("attr_setstacksize failed");
84 return 1;
87 #define N 20
88 int i;
89 pthread_t th[N];
91 for (i = 0; i < N; ++i)
92 if (pthread_create (&th[i], &at, tf, (void *) (long int) i) != 0)
94 puts ("create failed");
95 exit (1);
98 if (pthread_attr_destroy (&at) != 0)
100 puts ("attr_destroy failed");
101 return 1;
104 for (i = 0; i < N; ++i)
106 void *r;
107 if (pthread_join (th[i], &r) != 0)
109 puts ("join failed");
110 exit (1);
113 if (r != PTHREAD_CANCELED)
115 puts ("thread not canceled");
116 exit (1);
120 return 0;
124 #define TEST_FUNCTION do_test ()
125 #include "../test-skeleton.c"