Back to index

glibc  2.9
ex11.c
Go to the documentation of this file.
00001 /* Test program for timedout read/write lock functions.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License as
00007    published by the Free Software Foundation; either version 2.1 of the
00008    License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00017    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.  */
00019 
00020 #include <errno.h>
00021 #include <error.h>
00022 #include <pthread.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <time.h>
00026 #include <unistd.h>
00027 
00028 
00029 #define NWRITERS 15
00030 #define WRITETRIES 10
00031 #define NREADERS 15
00032 #define READTRIES 15
00033 
00034 #define TIMEOUT 1000000
00035 #define DELAY   1000000
00036 
00037 static pthread_rwlock_t lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP;
00038 
00039 
00040 static void *
00041 writer_thread (void *nr)
00042 {
00043   struct timespec ts;
00044   struct timespec delay;
00045   int n;
00046 
00047   ts.tv_sec = 0;
00048   ts.tv_nsec = TIMEOUT;
00049 
00050   delay.tv_sec = 0;
00051   delay.tv_nsec = DELAY;
00052 
00053   for (n = 0; n < WRITETRIES; ++n)
00054     {
00055       do
00056        {
00057          clock_gettime (CLOCK_REALTIME, &ts);
00058 
00059          ts.tv_nsec += 2 * TIMEOUT;
00060 
00061          printf ("writer thread %ld tries again\n", (long int) nr);
00062        }
00063       //while (pthread_rwlock_wrlock (&lock), 0);
00064       while (pthread_rwlock_timedwrlock (&lock, &ts) == ETIMEDOUT);
00065 
00066       printf ("writer thread %ld succeeded\n", (long int) nr);
00067 
00068       nanosleep (&delay, NULL);
00069 
00070       pthread_rwlock_unlock (&lock);
00071 
00072       printf ("writer thread %ld released\n", (long int) nr);
00073     }
00074 
00075   return NULL;
00076 }
00077 
00078 
00079 static void *
00080 reader_thread (void *nr)
00081 {
00082   struct timespec ts;
00083   struct timespec delay;
00084   int n;
00085 
00086   delay.tv_sec = 0;
00087   delay.tv_nsec = DELAY;
00088 
00089   for (n = 0; n < READTRIES; ++n)
00090     {
00091       do
00092        {
00093          clock_gettime (CLOCK_REALTIME, &ts);
00094 
00095          ts.tv_nsec += TIMEOUT;
00096 
00097          printf ("reader thread %ld tries again\n", (long int) nr);
00098        }
00099       //while (pthread_rwlock_rdlock (&lock), 0);
00100       while (pthread_rwlock_timedrdlock (&lock, &ts) == ETIMEDOUT);
00101 
00102       printf ("reader thread %ld succeeded\n", (long int) nr);
00103 
00104       nanosleep (&delay, NULL);
00105 
00106       pthread_rwlock_unlock (&lock);
00107 
00108       printf ("reader thread %ld released\n", (long int) nr);
00109     }
00110 
00111   return NULL;
00112 }
00113 
00114 
00115 int
00116 main (void)
00117 {
00118   pthread_t thwr[NWRITERS];
00119   pthread_t thrd[NREADERS];
00120   int n;
00121   void *res;
00122 
00123   /* Make standard error the same as standard output.  */
00124   dup2 (1, 2);
00125 
00126   /* Make sure we see all message, even those on stdout.  */
00127   setvbuf (stdout, NULL, _IONBF, 0);
00128 
00129   for (n = 0; n < NWRITERS; ++n)
00130     {
00131       int err = pthread_create (&thwr[n], NULL, writer_thread,
00132                             (void *) (long int) n);
00133 
00134       if (err != 0)
00135        error (EXIT_FAILURE, err, "cannot create writer thread");
00136     }
00137 
00138   for (n = 0; n < NREADERS; ++n)
00139     {
00140       int err = pthread_create (&thrd[n], NULL, reader_thread,
00141                             (void *) (long int) n);
00142 
00143       if (err != 0)
00144        error (EXIT_FAILURE, err, "cannot create reader thread");
00145     }
00146 
00147   /* Wait for all the threads.  */
00148   for (n = 0; n < NWRITERS; ++n)
00149     pthread_join (thwr[n], &res);
00150   for (n = 0; n < NREADERS; ++n)
00151     pthread_join (thrd[n], &res);
00152 
00153   return 0;
00154 }