Back to index

glibc  2.9
tst-rwlock8.c
Go to the documentation of this file.
00001 /* Test program for timedout read/write lock functions.
00002    Copyright (C) 2000, 2003 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 DELAY   1000000
00035 
00036 #ifndef INIT
00037 # define INIT PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
00038 #endif
00039 
00040 static pthread_rwlock_t lock = INIT;
00041 
00042 
00043 static void *
00044 writer_thread (void *nr)
00045 {
00046   struct timespec delay;
00047   int n;
00048 
00049   delay.tv_sec = 0;
00050   delay.tv_nsec = DELAY;
00051 
00052   for (n = 0; n < WRITETRIES; ++n)
00053     {
00054       printf ("writer thread %ld tries again\n", (long int) nr);
00055 
00056       if (pthread_rwlock_wrlock (&lock) != 0)
00057        {
00058          puts ("wrlock failed");
00059          exit (1);
00060        }
00061 
00062       printf ("writer thread %ld succeeded\n", (long int) nr);
00063 
00064       nanosleep (&delay, NULL);
00065 
00066       if (pthread_rwlock_unlock (&lock) != 0)
00067        {
00068          puts ("unlock for writer failed");
00069          exit (1);
00070        }
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 delay;
00083   int n;
00084 
00085   delay.tv_sec = 0;
00086   delay.tv_nsec = DELAY;
00087 
00088   for (n = 0; n < READTRIES; ++n)
00089     {
00090       printf ("reader thread %ld tries again\n", (long int) nr);
00091 
00092       if (pthread_rwlock_rdlock (&lock) != 0)
00093        {
00094          puts ("rdlock failed");
00095          exit (1);
00096        }
00097 
00098       printf ("reader thread %ld succeeded\n", (long int) nr);
00099 
00100       nanosleep (&delay, NULL);
00101 
00102       if (pthread_rwlock_unlock (&lock) != 0)
00103        {
00104          puts ("unlock for reader failed");
00105          exit (1);
00106        }
00107 
00108       printf ("reader thread %ld released\n", (long int) nr);
00109     }
00110 
00111   return NULL;
00112 }
00113 
00114 
00115 static int
00116 do_test (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     if (pthread_create (&thwr[n], NULL, writer_thread,
00131                      (void *) (long int) n) != 0)
00132       {
00133        puts ("writer create failed");
00134        exit (1);
00135       }
00136 
00137   for (n = 0; n < NREADERS; ++n)
00138     if (pthread_create (&thrd[n], NULL, reader_thread,
00139                      (void *) (long int) n) != 0)
00140       {
00141        puts ("reader create failed");
00142        exit (1);
00143       }
00144 
00145   /* Wait for all the threads.  */
00146   for (n = 0; n < NWRITERS; ++n)
00147     if (pthread_join (thwr[n], &res) != 0)
00148       {
00149        puts ("writer join failed");
00150        exit (1);
00151       }
00152   for (n = 0; n < NREADERS; ++n)
00153     if (pthread_join (thrd[n], &res) != 0)
00154       {
00155        puts ("reader join failed");
00156        exit (1);
00157       }
00158 
00159   return 0;
00160 }
00161 
00162 #define TIMEOUT 30
00163 #define TEST_FUNCTION do_test ()
00164 #include "../test-skeleton.c"