| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Test program for timedout read/write lock functions. | 
 | 2 |    Copyright (C) 2000-2016 Free Software Foundation, Inc. | 
 | 3 |    Contributed by Ulrich Drepper <drepper@redhat.com>, 2000. | 
 | 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 License as | 
 | 7 |    published by the Free Software Foundation; either version 2.1 of the | 
 | 8 |    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; see the file COPYING.LIB.  If | 
 | 17 |    not, see <http://www.gnu.org/licenses/>.  */ | 
 | 18 |  | 
 | 19 | #include <errno.h> | 
 | 20 | #include <error.h> | 
 | 21 | #include <pthread.h> | 
 | 22 | #include <stdio.h> | 
 | 23 | #include <stdlib.h> | 
 | 24 | #include <time.h> | 
 | 25 | #include <unistd.h> | 
 | 26 |  | 
 | 27 |  | 
 | 28 | #define NWRITERS 15 | 
 | 29 | #define WRITETRIES 10 | 
 | 30 | #define NREADERS 15 | 
 | 31 | #define READTRIES 15 | 
 | 32 |  | 
 | 33 | #define DELAY   1000000 | 
 | 34 |  | 
 | 35 | #ifndef INIT | 
 | 36 | # define INIT PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP | 
 | 37 | #endif | 
 | 38 |  | 
 | 39 | static pthread_rwlock_t lock = INIT; | 
 | 40 |  | 
 | 41 |  | 
 | 42 | static void * | 
 | 43 | writer_thread (void *nr) | 
 | 44 | { | 
 | 45 |   struct timespec delay; | 
 | 46 |   int n; | 
 | 47 |  | 
 | 48 |   delay.tv_sec = 0; | 
 | 49 |   delay.tv_nsec = DELAY; | 
 | 50 |  | 
 | 51 |   for (n = 0; n < WRITETRIES; ++n) | 
 | 52 |     { | 
 | 53 |       printf ("writer thread %ld tries again\n", (long int) nr); | 
 | 54 |  | 
 | 55 |       if (pthread_rwlock_wrlock (&lock) != 0) | 
 | 56 | 	{ | 
 | 57 | 	  puts ("wrlock failed"); | 
 | 58 | 	  exit (1); | 
 | 59 | 	} | 
 | 60 |  | 
 | 61 |       printf ("writer thread %ld succeeded\n", (long int) nr); | 
 | 62 |  | 
 | 63 |       nanosleep (&delay, NULL); | 
 | 64 |  | 
 | 65 |       if (pthread_rwlock_unlock (&lock) != 0) | 
 | 66 | 	{ | 
 | 67 | 	  puts ("unlock for writer failed"); | 
 | 68 | 	  exit (1); | 
 | 69 | 	} | 
 | 70 |  | 
 | 71 |       printf ("writer thread %ld released\n", (long int) nr); | 
 | 72 |     } | 
 | 73 |  | 
 | 74 |   return NULL; | 
 | 75 | } | 
 | 76 |  | 
 | 77 |  | 
 | 78 | static void * | 
 | 79 | reader_thread (void *nr) | 
 | 80 | { | 
 | 81 |   struct timespec delay; | 
 | 82 |   int n; | 
 | 83 |  | 
 | 84 |   delay.tv_sec = 0; | 
 | 85 |   delay.tv_nsec = DELAY; | 
 | 86 |  | 
 | 87 |   for (n = 0; n < READTRIES; ++n) | 
 | 88 |     { | 
 | 89 |       printf ("reader thread %ld tries again\n", (long int) nr); | 
 | 90 |  | 
 | 91 |       if (pthread_rwlock_rdlock (&lock) != 0) | 
 | 92 | 	{ | 
 | 93 | 	  puts ("rdlock failed"); | 
 | 94 | 	  exit (1); | 
 | 95 | 	} | 
 | 96 |  | 
 | 97 |       printf ("reader thread %ld succeeded\n", (long int) nr); | 
 | 98 |  | 
 | 99 |       nanosleep (&delay, NULL); | 
 | 100 |  | 
 | 101 |       if (pthread_rwlock_unlock (&lock) != 0) | 
 | 102 | 	{ | 
 | 103 | 	  puts ("unlock for reader failed"); | 
 | 104 | 	  exit (1); | 
 | 105 | 	} | 
 | 106 |  | 
 | 107 |       printf ("reader thread %ld released\n", (long int) nr); | 
 | 108 |     } | 
 | 109 |  | 
 | 110 |   return NULL; | 
 | 111 | } | 
 | 112 |  | 
 | 113 |  | 
 | 114 | static int | 
 | 115 | do_test (void) | 
 | 116 | { | 
 | 117 |   pthread_t thwr[NWRITERS]; | 
 | 118 |   pthread_t thrd[NREADERS]; | 
 | 119 |   int n; | 
 | 120 |   void *res; | 
 | 121 |  | 
 | 122 |   /* Make standard error the same as standard output.  */ | 
 | 123 |   dup2 (1, 2); | 
 | 124 |  | 
 | 125 |   /* Make sure we see all message, even those on stdout.  */ | 
 | 126 |   setvbuf (stdout, NULL, _IONBF, 0); | 
 | 127 |  | 
 | 128 |   for (n = 0; n < NWRITERS; ++n) | 
 | 129 |     if (pthread_create (&thwr[n], NULL, writer_thread, | 
 | 130 | 			(void *) (long int) n) != 0) | 
 | 131 |       { | 
 | 132 | 	puts ("writer create failed"); | 
 | 133 | 	exit (1); | 
 | 134 |       } | 
 | 135 |  | 
 | 136 |   for (n = 0; n < NREADERS; ++n) | 
 | 137 |     if (pthread_create (&thrd[n], NULL, reader_thread, | 
 | 138 | 			(void *) (long int) n) != 0) | 
 | 139 |       { | 
 | 140 | 	puts ("reader create failed"); | 
 | 141 | 	exit (1); | 
 | 142 |       } | 
 | 143 |  | 
 | 144 |   /* Wait for all the threads.  */ | 
 | 145 |   for (n = 0; n < NWRITERS; ++n) | 
 | 146 |     if (pthread_join (thwr[n], &res) != 0) | 
 | 147 |       { | 
 | 148 | 	puts ("writer join failed"); | 
 | 149 | 	exit (1); | 
 | 150 |       } | 
 | 151 |   for (n = 0; n < NREADERS; ++n) | 
 | 152 |     if (pthread_join (thrd[n], &res) != 0) | 
 | 153 |       { | 
 | 154 | 	puts ("reader join failed"); | 
 | 155 | 	exit (1); | 
 | 156 |       } | 
 | 157 |  | 
 | 158 |   return 0; | 
 | 159 | } | 
 | 160 |  | 
 | 161 | #define TIMEOUT 30 | 
 | 162 | #define TEST_FUNCTION do_test () | 
 | 163 | #include "../test-skeleton.c" |