xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2003-2016 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 <sys/time.h> |
| 25 | |
| 26 | |
| 27 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| 28 | static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
| 29 | |
| 30 | |
| 31 | static void * |
| 32 | tf (void *arg) |
| 33 | { |
| 34 | int err = pthread_cond_wait (&cond, &mut); |
| 35 | if (err == 0) |
| 36 | { |
| 37 | puts ("cond_wait did not fail"); |
| 38 | exit (1); |
| 39 | } |
| 40 | |
| 41 | if (err != EPERM) |
| 42 | { |
| 43 | printf ("cond_wait didn't return EPERM but %d\n", err); |
| 44 | exit (1); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /* Current time. */ |
| 49 | struct timeval tv; |
| 50 | (void) gettimeofday (&tv, NULL); |
| 51 | /* +1000 seconds in correct format. */ |
| 52 | struct timespec ts; |
| 53 | TIMEVAL_TO_TIMESPEC (&tv, &ts); |
| 54 | ts.tv_sec += 1000; |
| 55 | |
| 56 | err = pthread_cond_timedwait (&cond, &mut, &ts); |
| 57 | if (err == 0) |
| 58 | { |
| 59 | puts ("cond_timedwait did not fail"); |
| 60 | exit (1); |
| 61 | } |
| 62 | |
| 63 | if (err != EPERM) |
| 64 | { |
| 65 | printf ("cond_timedwait didn't return EPERM but %d\n", err); |
| 66 | exit (1); |
| 67 | } |
| 68 | |
| 69 | return (void *) 1l; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static int |
| 74 | do_test (void) |
| 75 | { |
| 76 | pthread_t th; |
| 77 | int err; |
| 78 | |
| 79 | printf ("&cond = %p\n&mut = %p\n", &cond, &mut); |
| 80 | |
| 81 | err = pthread_cond_wait (&cond, &mut); |
| 82 | if (err == 0) |
| 83 | { |
| 84 | puts ("cond_wait did not fail"); |
| 85 | exit (1); |
| 86 | } |
| 87 | |
| 88 | if (err != EPERM) |
| 89 | { |
| 90 | printf ("cond_wait didn't return EPERM but %d\n", err); |
| 91 | exit (1); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /* Current time. */ |
| 96 | struct timeval tv; |
| 97 | (void) gettimeofday (&tv, NULL); |
| 98 | /* +1000 seconds in correct format. */ |
| 99 | struct timespec ts; |
| 100 | TIMEVAL_TO_TIMESPEC (&tv, &ts); |
| 101 | ts.tv_sec += 1000; |
| 102 | |
| 103 | err = pthread_cond_timedwait (&cond, &mut, &ts); |
| 104 | if (err == 0) |
| 105 | { |
| 106 | puts ("cond_timedwait did not fail"); |
| 107 | exit (1); |
| 108 | } |
| 109 | |
| 110 | if (err != EPERM) |
| 111 | { |
| 112 | printf ("cond_timedwait didn't return EPERM but %d\n", err); |
| 113 | exit (1); |
| 114 | } |
| 115 | |
| 116 | if (pthread_mutex_lock (&mut) != 0) |
| 117 | { |
| 118 | puts ("parent: mutex_lock failed"); |
| 119 | exit (1); |
| 120 | } |
| 121 | |
| 122 | puts ("creating thread"); |
| 123 | |
| 124 | if (pthread_create (&th, NULL, tf, NULL) != 0) |
| 125 | { |
| 126 | puts ("create failed"); |
| 127 | exit (1); |
| 128 | } |
| 129 | |
| 130 | void *r; |
| 131 | if (pthread_join (th, &r) != 0) |
| 132 | { |
| 133 | puts ("join failed"); |
| 134 | exit (1); |
| 135 | } |
| 136 | if (r != (void *) 1l) |
| 137 | { |
| 138 | puts ("thread has wrong return value"); |
| 139 | exit (1); |
| 140 | } |
| 141 | |
| 142 | puts ("done"); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | #define TEST_FUNCTION do_test () |
| 149 | #include "../test-skeleton.c" |