lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2003 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, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <pthread.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | |
| 26 | static void * |
| 27 | tf1 (void *arg) |
| 28 | { |
| 29 | pthread_join ((pthread_t) arg, NULL); |
| 30 | |
| 31 | puts ("1st join returned"); |
| 32 | |
| 33 | return (void *) 1l; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | static void * |
| 38 | tf2 (void *arg) |
| 39 | { |
| 40 | int a; |
| 41 | a = pthread_join ((pthread_t) arg, NULL); |
| 42 | |
| 43 | puts ("2nd join returned"); |
| 44 | printf("a = %i\n", a); |
| 45 | |
| 46 | return (void *) 1l; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static int |
| 51 | do_test (void) |
| 52 | { |
| 53 | pthread_t th; |
| 54 | |
| 55 | int err = pthread_join (pthread_self (), NULL); |
| 56 | if (err == 0) |
| 57 | { |
| 58 | puts ("1st circular join succeeded"); |
| 59 | exit (1); |
| 60 | } |
| 61 | if (err != EDEADLK) |
| 62 | { |
| 63 | printf ("1st circular join %d, not EDEADLK\n", err); |
| 64 | exit (1); |
| 65 | } |
| 66 | |
| 67 | if (pthread_create (&th, NULL, tf1, (void *) pthread_self ()) != 0) |
| 68 | { |
| 69 | puts ("1st create failed"); |
| 70 | exit (1); |
| 71 | } |
| 72 | |
| 73 | if (pthread_cancel (th) != 0) |
| 74 | { |
| 75 | puts ("cannot cancel 1st thread"); |
| 76 | exit (1); |
| 77 | } |
| 78 | |
| 79 | void *r; |
| 80 | err = pthread_join (th, &r); |
| 81 | if (err != 0) |
| 82 | { |
| 83 | printf ("cannot join 1st thread: %d\n", err); |
| 84 | exit (1); |
| 85 | } |
| 86 | if (r != PTHREAD_CANCELED) |
| 87 | { |
| 88 | puts ("1st thread not canceled"); |
| 89 | exit (1); |
| 90 | } |
| 91 | |
| 92 | err = pthread_join (pthread_self (), NULL); |
| 93 | if (err == 0) |
| 94 | { |
| 95 | puts ("2nd circular join succeeded"); |
| 96 | exit (1); |
| 97 | } |
| 98 | if (err != EDEADLK) |
| 99 | { |
| 100 | printf ("2nd circular join %d, not EDEADLK\n", err); |
| 101 | exit (1); |
| 102 | } |
| 103 | |
| 104 | if (pthread_create (&th, NULL, tf2, (void *) pthread_self ()) != 0) |
| 105 | { |
| 106 | puts ("2nd create failed"); |
| 107 | exit (1); |
| 108 | } |
| 109 | |
| 110 | if (pthread_cancel (th) != 0) |
| 111 | { |
| 112 | puts ("cannot cancel 2nd thread"); |
| 113 | exit (1); |
| 114 | } |
| 115 | |
| 116 | if (pthread_join (th, &r) != 0) |
| 117 | { |
| 118 | puts ("cannot join 2nd thread"); |
| 119 | exit (1); |
| 120 | } |
| 121 | if (r != PTHREAD_CANCELED) |
| 122 | { |
| 123 | puts ("2nd thread not canceled"); |
| 124 | exit (1); |
| 125 | } |
| 126 | |
| 127 | err = pthread_join (pthread_self (), NULL); |
| 128 | if (err == 0) |
| 129 | { |
| 130 | puts ("2nd circular join succeeded"); |
| 131 | exit (1); |
| 132 | } |
| 133 | if (err != EDEADLK) |
| 134 | { |
| 135 | printf ("2nd circular join %d, not EDEADLK\n", err); |
| 136 | exit (1); |
| 137 | } |
| 138 | |
| 139 | exit (0); |
| 140 | } |
| 141 | |
| 142 | #define TEST_FUNCTION do_test () |
| 143 | #include "../test-skeleton.c" |