]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/uclibc/lib/contrib/uclibc/test/nptl/tst-cancel10.c
Update
[l4.git] / l4 / pkg / l4re-core / uclibc / lib / contrib / uclibc / test / nptl / tst-cancel10.c
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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    <http://www.gnu.org/licenses/>.  */
18
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24
25 static void
26 cleanup (void *arg)
27 {
28   /* Just for fun.  */
29   if (pthread_cancel (pthread_self ()) != 0)
30     {
31       puts ("cleanup: cancel failed");
32       exit (1);
33     }
34
35   printf ("cleanup for %ld\n", (long int) arg);
36 }
37
38
39 static void *
40 tf (void *arg)
41 {
42   long int n = (long int) arg;
43
44   pthread_cleanup_push (cleanup, arg);
45
46   if (pthread_setcanceltype ((n & 1) == 0
47                              ? PTHREAD_CANCEL_DEFERRED
48                              : PTHREAD_CANCEL_ASYNCHRONOUS, NULL) != 0)
49     {
50       puts ("setcanceltype failed");
51       exit (1);
52     }
53
54   if (pthread_cancel (pthread_self ()) != 0)
55     {
56       puts ("cancel failed");
57       exit (1);
58     }
59
60   pthread_testcancel ();
61
62   /* We should never come here.  */
63
64   pthread_cleanup_pop (0);
65
66   return NULL;
67 }
68
69
70 static int
71 do_test (void)
72 {
73   pthread_attr_t at;
74
75   if (pthread_attr_init (&at) != 0)
76     {
77       puts ("attr_init failed");
78       return 1;
79     }
80
81   if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
82     {
83       puts ("attr_setstacksize failed");
84       return 1;
85     }
86
87 #define N 20
88   int i;
89   pthread_t th[N];
90
91   for (i = 0; i < N; ++i)
92     if (pthread_create (&th[i], &at, tf, (void *) (long int) i) != 0)
93       {
94         puts ("create failed");
95         exit (1);
96       }
97
98   if (pthread_attr_destroy (&at) != 0)
99     {
100       puts ("attr_destroy failed");
101       return 1;
102     }
103
104   for (i = 0; i < N; ++i)
105     {
106       void *r;
107       if (pthread_join (th[i], &r) != 0)
108         {
109           puts ("join failed");
110           exit (1);
111         }
112
113       if (r != PTHREAD_CANCELED)
114         {
115           puts ("thread not canceled");
116           exit (1);
117         }
118     }
119
120   return 0;
121 }
122
123
124 #define TEST_FUNCTION do_test ()
125 #include "../test-skeleton.c"