Back to index

glibc  2.9
tst-rwlock7.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the 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; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <pthread.h>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <sys/time.h>
00026 
00027 
00028 static int kind[] =
00029   {
00030     PTHREAD_RWLOCK_PREFER_READER_NP,
00031     PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
00032     PTHREAD_RWLOCK_PREFER_WRITER_NP,
00033   };
00034 
00035 
00036 static void *
00037 tf (void *arg)
00038 {
00039   pthread_rwlock_t *r = arg;
00040 
00041   /* Timeout: 0.3 secs.  */
00042   struct timeval tv;
00043   (void) gettimeofday (&tv, NULL);
00044 
00045   struct timespec ts;
00046   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00047   ts.tv_nsec += 300000000;
00048   if (ts.tv_nsec >= 1000000000)
00049     {
00050       ts.tv_nsec -= 1000000000;
00051       ++ts.tv_sec;
00052     }
00053 
00054   int err = pthread_rwlock_timedwrlock (r, &ts);
00055   if (err == 0)
00056     {
00057       puts ("rwlock_timedwrlock returned");
00058       pthread_exit ((void *) 1l);
00059     }
00060 
00061   if (err != ETIMEDOUT)
00062     {
00063       printf ("err = %s (%d), expected %s (%d)\n",
00064              strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT);
00065       pthread_exit ((void *) 1l);
00066     }
00067   puts ("child: timedwrlock failed with ETIMEDOUT");
00068 
00069   struct timeval tv2;
00070   (void) gettimeofday (&tv2, NULL);
00071 
00072   timersub (&tv2, &tv, &tv);
00073 
00074   if (tv.tv_usec < 200000)
00075     {
00076       puts ("timeout too short");
00077       pthread_exit ((void *) 1l);
00078     }
00079 
00080   (void) gettimeofday (&tv, NULL);
00081   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00082   ts.tv_sec += 10;
00083   /* Note that the following operation makes ts invalid.  */
00084   ts.tv_nsec += 1000000000;
00085 
00086   err = pthread_rwlock_timedwrlock (r, &ts);
00087   if (err == 0)
00088     {
00089       puts ("2nd timedwrlock succeeded");
00090       pthread_exit ((void *) 1l);
00091     }
00092   if (err != EINVAL)
00093     {
00094       puts ("2nd timedwrlock did not return EINVAL");
00095       pthread_exit ((void *) 1l);
00096     }
00097   puts ("child: timedwrlock failed with EINVAL");
00098 
00099   return NULL;
00100 }
00101 
00102 
00103 static int
00104 do_test (void)
00105 {
00106   size_t cnt;
00107   for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
00108     {
00109       pthread_rwlock_t r;
00110       pthread_rwlockattr_t a;
00111 
00112       if (pthread_rwlockattr_init (&a) != 0)
00113        {
00114          printf ("round %Zu: rwlockattr_t failed\n", cnt);
00115          exit (1);
00116        }
00117 
00118       if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
00119        {
00120          printf ("round %Zu: rwlockattr_setkind failed\n", cnt);
00121          exit (1);
00122        }
00123 
00124       if (pthread_rwlock_init (&r, &a) != 0)
00125        {
00126          printf ("round %Zu: rwlock_init failed\n", cnt);
00127          exit (1);
00128        }
00129 
00130       if (pthread_rwlockattr_destroy (&a) != 0)
00131        {
00132          printf ("round %Zu: rwlockattr_destroy failed\n", cnt);
00133          exit (1);
00134        }
00135 
00136       struct timeval tv;
00137       (void) gettimeofday (&tv, NULL);
00138 
00139       struct timespec ts;
00140       TIMEVAL_TO_TIMESPEC (&tv, &ts);
00141 
00142       ++ts.tv_sec;
00143 
00144       /* Get a read lock.  */
00145       if (pthread_rwlock_timedrdlock (&r, &ts) != 0)
00146        {
00147          printf ("round %Zu: rwlock_timedrdlock failed\n", cnt);
00148          exit (1);
00149        }
00150       printf ("%zu: got timedrdlock\n", cnt);
00151 
00152       pthread_t th;
00153       if (pthread_create (&th, NULL, tf, &r) != 0)
00154        {
00155          printf ("round %Zu: create failed\n", cnt);
00156          exit (1);
00157        }
00158 
00159       void *status;
00160       if (pthread_join (th, &status) != 0)
00161        {
00162          printf ("round %Zu: join failed\n", cnt);
00163          exit (1);
00164        }
00165       if (status != NULL)
00166        {
00167          printf ("failure in round %Zu\n", cnt);
00168          exit (1);
00169        }
00170 
00171       if (pthread_rwlock_destroy (&r) != 0)
00172        {
00173          printf ("round %Zu: rwlock_destroy failed\n", cnt);
00174          exit (1);
00175        }
00176     }
00177 
00178   return 0;
00179 }
00180 
00181 #define TEST_FUNCTION do_test ()
00182 #include "../test-skeleton.c"