lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2003-2015 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 <errno.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <time.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | |
| 27 | static pthread_barrier_t b; |
| 28 | |
| 29 | |
| 30 | /* Cleanup handling test. */ |
| 31 | static int cl_called; |
| 32 | |
| 33 | static void |
| 34 | cl (void *arg) |
| 35 | { |
| 36 | ++cl_called; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | static void * |
| 41 | tf (void *arg) |
| 42 | { |
| 43 | int r = pthread_barrier_wait (&b); |
| 44 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) |
| 45 | { |
| 46 | puts ("barrier_wait failed"); |
| 47 | exit (1); |
| 48 | } |
| 49 | |
| 50 | pthread_cleanup_push (cl, NULL); |
| 51 | |
| 52 | struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 }; |
| 53 | TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts)); |
| 54 | |
| 55 | pthread_cleanup_pop (0); |
| 56 | |
| 57 | puts ("clock_nanosleep returned"); |
| 58 | |
| 59 | exit (1); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static int |
| 64 | do_test (void) |
| 65 | { |
| 66 | if (pthread_barrier_init (&b, NULL, 2) != 0) |
| 67 | { |
| 68 | puts ("barrier_init failed"); |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | pthread_t th; |
| 73 | if (pthread_create (&th, NULL, tf, NULL) != 0) |
| 74 | { |
| 75 | puts ("1st create failed"); |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | int r = pthread_barrier_wait (&b); |
| 80 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) |
| 81 | { |
| 82 | puts ("barrier_wait failed"); |
| 83 | exit (1); |
| 84 | } |
| 85 | |
| 86 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 }; |
| 87 | while (nanosleep (&ts, &ts) != 0) |
| 88 | continue; |
| 89 | |
| 90 | puts ("going to cancel in-time"); |
| 91 | if (pthread_cancel (th) != 0) |
| 92 | { |
| 93 | puts ("1st cancel failed"); |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | void *status; |
| 98 | if (pthread_join (th, &status) != 0) |
| 99 | { |
| 100 | puts ("1st join failed"); |
| 101 | return 1; |
| 102 | } |
| 103 | if (status != PTHREAD_CANCELED) |
| 104 | { |
| 105 | puts ("1st thread not canceled"); |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | if (cl_called == 0) |
| 110 | { |
| 111 | puts ("cleanup handler not called"); |
| 112 | return 1; |
| 113 | } |
| 114 | if (cl_called > 1) |
| 115 | { |
| 116 | puts ("cleanup handler called more than once"); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | puts ("in-time cancellation succeeded"); |
| 121 | |
| 122 | |
| 123 | cl_called = 0; |
| 124 | |
| 125 | if (pthread_create (&th, NULL, tf, NULL) != 0) |
| 126 | { |
| 127 | puts ("2nd create failed"); |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | puts ("going to cancel early"); |
| 132 | if (pthread_cancel (th) != 0) |
| 133 | { |
| 134 | puts ("2nd cancel failed"); |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | r = pthread_barrier_wait (&b); |
| 139 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) |
| 140 | { |
| 141 | puts ("barrier_wait failed"); |
| 142 | exit (1); |
| 143 | } |
| 144 | |
| 145 | if (pthread_join (th, &status) != 0) |
| 146 | { |
| 147 | puts ("2nd join failed"); |
| 148 | return 1; |
| 149 | } |
| 150 | if (status != PTHREAD_CANCELED) |
| 151 | { |
| 152 | puts ("2nd thread not canceled"); |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | if (cl_called == 0) |
| 157 | { |
| 158 | printf ("cleanup handler not called\n"); |
| 159 | return 1; |
| 160 | } |
| 161 | if (cl_called > 1) |
| 162 | { |
| 163 | printf ("cleanup handler called more than once\n"); |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | puts ("early cancellation succeeded"); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | #define TEST_FUNCTION do_test () |
| 173 | #include "../test-skeleton.c" |