lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <pthreadP.h> |
| 2 | #include <signal.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | |
| 6 | |
| 7 | static pthread_barrier_t b; |
| 8 | static pthread_t th2; |
| 9 | |
| 10 | |
| 11 | static void * |
| 12 | tf2 (void *arg) |
| 13 | { |
| 14 | #ifdef SIGCANCEL |
| 15 | sigset_t mask; |
| 16 | if (pthread_sigmask (SIG_SETMASK, NULL, &mask) != 0) |
| 17 | { |
| 18 | puts ("pthread_sigmask failed"); |
| 19 | exit (1); |
| 20 | } |
| 21 | if (sigismember (&mask, SIGCANCEL)) |
| 22 | { |
| 23 | puts ("SIGCANCEL blocked in new thread"); |
| 24 | exit (1); |
| 25 | } |
| 26 | #endif |
| 27 | |
| 28 | /* Sync with the main thread so that we do not test anything else. */ |
| 29 | int e = pthread_barrier_wait (&b); |
| 30 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) |
| 31 | { |
| 32 | puts ("barrier_wait failed"); |
| 33 | exit (1); |
| 34 | } |
| 35 | |
| 36 | while (1) |
| 37 | { |
| 38 | /* Just a cancelable call. */ |
| 39 | struct timespec ts = { 10000, 0 }; |
| 40 | nanosleep (&ts, 0); |
| 41 | } |
| 42 | |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | static void |
| 48 | unwhand (void *arg) |
| 49 | { |
| 50 | if (pthread_create (&th2, NULL, tf2, NULL) != 0) |
| 51 | { |
| 52 | puts ("unwhand: create failed"); |
| 53 | exit (1); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static void * |
| 59 | tf (void *arg) |
| 60 | { |
| 61 | pthread_cleanup_push (unwhand, NULL); |
| 62 | |
| 63 | /* Sync with the main thread so that we do not test anything else. */ |
| 64 | int e = pthread_barrier_wait (&b); |
| 65 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) |
| 66 | { |
| 67 | puts ("barrier_wait failed"); |
| 68 | exit (1); |
| 69 | } |
| 70 | |
| 71 | while (1) |
| 72 | { |
| 73 | /* Just a cancelable call. */ |
| 74 | struct timespec ts = { 10000, 0 }; |
| 75 | nanosleep (&ts, 0); |
| 76 | } |
| 77 | |
| 78 | pthread_cleanup_pop (0); |
| 79 | |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static int |
| 85 | do_test (void) |
| 86 | { |
| 87 | if (pthread_barrier_init (&b, NULL, 2) != 0) |
| 88 | { |
| 89 | puts ("barrier_init failed"); |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | pthread_t th1; |
| 94 | if (pthread_create (&th1, NULL, tf, NULL) != 0) |
| 95 | { |
| 96 | puts ("create failed"); |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | int e = pthread_barrier_wait (&b); |
| 101 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) |
| 102 | { |
| 103 | puts ("barrier_wait failed"); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | /* Make sure tf1 enters nanosleep. */ |
| 108 | struct timespec ts = { 0, 500000000 }; |
| 109 | while (nanosleep (&ts, &ts) != 0) |
| 110 | ; |
| 111 | |
| 112 | if (pthread_cancel (th1) != 0) |
| 113 | { |
| 114 | puts ("1st cancel failed"); |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | void *res; |
| 119 | if (pthread_join (th1, &res) != 0) |
| 120 | { |
| 121 | puts ("1st join failed"); |
| 122 | return 1; |
| 123 | } |
| 124 | if (res != PTHREAD_CANCELED) |
| 125 | { |
| 126 | puts ("1st thread not canceled"); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | e = pthread_barrier_wait (&b); |
| 131 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) |
| 132 | { |
| 133 | puts ("barrier_wait failed"); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | /* Make sure tf2 enters nanosleep. */ |
| 138 | ts.tv_sec = 0; |
| 139 | ts.tv_nsec = 500000000; |
| 140 | while (nanosleep (&ts, &ts) != 0) |
| 141 | ; |
| 142 | |
| 143 | puts ("calling pthread_cancel the second time"); |
| 144 | if (pthread_cancel (th2) != 0) |
| 145 | { |
| 146 | puts ("2nd cancel failed"); |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | puts ("calling pthread_join the second time"); |
| 151 | if (pthread_join (th2, &res) != 0) |
| 152 | { |
| 153 | puts ("2nd join failed"); |
| 154 | return 1; |
| 155 | } |
| 156 | if (res != PTHREAD_CANCELED) |
| 157 | { |
| 158 | puts ("2nd thread not canceled"); |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | if (pthread_barrier_destroy (&b) != 0) |
| 163 | { |
| 164 | puts ("barrier_destroy failed"); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | #define TEST_FUNCTION do_test () |
| 172 | #define TIMEOUT 4 |
| 173 | #include "../test-skeleton.c" |