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 <signal.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/mman.h> |
| 26 | #include <sys/wait.h> |
| 27 | |
| 28 | |
| 29 | static char fname[] = "/tmp/tst-cond12-XXXXXX"; |
| 30 | static int fd; |
| 31 | |
| 32 | |
| 33 | static void prepare (void); |
| 34 | #define PREPARE(argc, argv) prepare () |
| 35 | |
| 36 | static int do_test (void); |
| 37 | #define TEST_FUNCTION do_test () |
| 38 | |
| 39 | #include "../test-skeleton.c" |
| 40 | |
| 41 | |
| 42 | static void |
| 43 | prepare (void) |
| 44 | { |
| 45 | fd = mkstemp (fname); |
| 46 | if (fd == -1) |
| 47 | { |
| 48 | printf ("mkstemp failed: %m\n"); |
| 49 | exit (1); |
| 50 | } |
| 51 | add_temp_file (fname); |
| 52 | if (ftruncate (fd, 1000) < 0) |
| 53 | { |
| 54 | printf ("ftruncate failed: %m\n"); |
| 55 | exit (1); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static int |
| 61 | do_test (void) |
| 62 | { |
| 63 | struct |
| 64 | { |
| 65 | pthread_mutex_t m; |
| 66 | pthread_cond_t c; |
| 67 | int var; |
| 68 | } *p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 69 | if (p == MAP_FAILED) |
| 70 | { |
| 71 | printf ("initial mmap failed: %m\n"); |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | pthread_mutexattr_t ma; |
| 76 | if (pthread_mutexattr_init (&ma) != 0) |
| 77 | { |
| 78 | puts ("mutexattr_init failed"); |
| 79 | return 1; |
| 80 | } |
| 81 | if (pthread_mutexattr_setpshared (&ma, 1) != 0) |
| 82 | { |
| 83 | puts ("mutexattr_setpshared failed"); |
| 84 | return 1; |
| 85 | } |
| 86 | if (pthread_mutex_init (&p->m, &ma) != 0) |
| 87 | { |
| 88 | puts ("mutex_init failed"); |
| 89 | return 1; |
| 90 | } |
| 91 | if (pthread_mutexattr_destroy (&ma) != 0) |
| 92 | { |
| 93 | puts ("mutexattr_destroy failed"); |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | pthread_condattr_t ca; |
| 98 | if (pthread_condattr_init (&ca) != 0) |
| 99 | { |
| 100 | puts ("condattr_init failed"); |
| 101 | return 1; |
| 102 | } |
| 103 | if (pthread_condattr_setpshared (&ca, 1) != 0) |
| 104 | { |
| 105 | puts ("condattr_setpshared failed"); |
| 106 | return 1; |
| 107 | } |
| 108 | if (pthread_cond_init (&p->c, &ca) != 0) |
| 109 | { |
| 110 | puts ("mutex_init failed"); |
| 111 | return 1; |
| 112 | } |
| 113 | if (pthread_condattr_destroy (&ca) != 0) |
| 114 | { |
| 115 | puts ("condattr_destroy failed"); |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | if (pthread_mutex_lock (&p->m) != 0) |
| 120 | { |
| 121 | puts ("initial mutex_lock failed"); |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | p->var = 42; |
| 126 | |
| 127 | pid_t pid = fork (); |
| 128 | if (pid == -1) |
| 129 | { |
| 130 | printf ("fork failed: %m\n"); |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | if (pid == 0) |
| 135 | { |
| 136 | void *oldp = p; |
| 137 | p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 138 | |
| 139 | if (p == oldp) |
| 140 | { |
| 141 | puts ("child: mapped to same address"); |
| 142 | kill (getppid (), SIGKILL); |
| 143 | exit (1); |
| 144 | } |
| 145 | |
| 146 | munmap (oldp, sizeof (*p)); |
| 147 | |
| 148 | if (pthread_mutex_lock (&p->m) != 0) |
| 149 | { |
| 150 | puts ("child: mutex_lock failed"); |
| 151 | kill (getppid (), SIGKILL); |
| 152 | exit (1); |
| 153 | } |
| 154 | |
| 155 | p->var = 0; |
| 156 | |
| 157 | #ifndef USE_COND_SIGNAL |
| 158 | if (pthread_cond_broadcast (&p->c) != 0) |
| 159 | { |
| 160 | puts ("child: cond_broadcast failed"); |
| 161 | kill (getppid (), SIGKILL); |
| 162 | exit (1); |
| 163 | } |
| 164 | #else |
| 165 | if (pthread_cond_signal (&p->c) != 0) |
| 166 | { |
| 167 | puts ("child: cond_signal failed"); |
| 168 | kill (getppid (), SIGKILL); |
| 169 | exit (1); |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | if (pthread_mutex_unlock (&p->m) != 0) |
| 174 | { |
| 175 | puts ("child: mutex_unlock failed"); |
| 176 | kill (getppid (), SIGKILL); |
| 177 | exit (1); |
| 178 | } |
| 179 | |
| 180 | exit (0); |
| 181 | } |
| 182 | |
| 183 | do |
| 184 | pthread_cond_wait (&p->c, &p->m); |
| 185 | while (p->var != 0); |
| 186 | |
| 187 | if (TEMP_FAILURE_RETRY (waitpid (pid, NULL, 0)) != pid) |
| 188 | { |
| 189 | printf ("waitpid failed: %m\n"); |
| 190 | kill (pid, SIGKILL); |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |