Back to index

glibc  2.9
tst-rwlock14.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
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 <stdio.h>
00023 #include <stdlib.h>
00024 #include <time.h>
00025 
00026 
00027 static pthread_barrier_t b;
00028 static pthread_rwlock_t r = PTHREAD_RWLOCK_INITIALIZER;
00029 
00030 
00031 static void *
00032 tf (void *arg)
00033 {
00034   /* Lock the read-write lock.  */
00035   if (pthread_rwlock_wrlock (&r) != 0)
00036     {
00037       puts ("tf: cannot lock rwlock");
00038       exit (EXIT_FAILURE);
00039     }
00040 
00041   pthread_t mt = *(pthread_t *) arg;
00042 
00043   pthread_barrier_wait (&b);
00044 
00045   /* This call will never return.  */
00046   pthread_join (mt, NULL);
00047 
00048   return NULL;
00049 }
00050 
00051 
00052 static int
00053 do_test (void)
00054 {
00055   int result = 0;
00056   struct timespec ts;
00057 
00058   if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
00059     {
00060       puts ("clock_gettime failed");
00061       return 1;
00062     }
00063 
00064   if (pthread_barrier_init (&b, NULL, 2) != 0)
00065     {
00066       puts ("barrier_init failed");
00067       return 1;
00068     }
00069 
00070   pthread_t me = pthread_self ();
00071   pthread_t th;
00072   if (pthread_create (&th, NULL, tf, &me) != 0)
00073     {
00074       puts ("create failed");
00075       return 1;
00076     }
00077 
00078   /* Wait until the rwlock is locked.  */
00079   pthread_barrier_wait (&b);
00080 
00081   ts.tv_nsec = -1;
00082 
00083   int e = pthread_rwlock_timedrdlock (&r, &ts);
00084   if (e == 0)
00085     {
00086       puts ("first rwlock_timedrdlock did not fail");
00087       result = 1;
00088     }
00089   else if (e != EINVAL)
00090     {
00091       puts ("first rwlock_timedrdlock did not return EINVAL");
00092       result = 1;
00093     }
00094 
00095   e = pthread_rwlock_timedwrlock (&r, &ts);
00096   if (e == 0)
00097     {
00098       puts ("first rwlock_timedwrlock did not fail");
00099       result = 1;
00100     }
00101   else if (e != EINVAL)
00102     {
00103       puts ("first rwlock_timedwrlock did not return EINVAL");
00104       result = 1;
00105     }
00106 
00107   ts.tv_nsec = 1000000000;
00108 
00109   e = pthread_rwlock_timedrdlock (&r, &ts);
00110   if (e == 0)
00111     {
00112       puts ("second rwlock_timedrdlock did not fail");
00113       result = 1;
00114     }
00115   else if (e != EINVAL)
00116     {
00117       puts ("second rwlock_timedrdlock did not return EINVAL");
00118       result = 1;
00119     }
00120 
00121   e = pthread_rwlock_timedrdlock (&r, &ts);
00122   if (e == 0)
00123     {
00124       puts ("second rwlock_timedrdlock did not fail");
00125       result = 1;
00126     }
00127   else if (e != EINVAL)
00128     {
00129       puts ("second rwlock_timedrdlock did not return EINVAL");
00130       result = 1;
00131     }
00132 
00133   ts.tv_nsec = (__typeof (ts.tv_nsec)) 0x100001000LL;
00134   if ((__typeof (ts.tv_nsec)) 0x100001000LL != 0x100001000LL)
00135     ts.tv_nsec = 2000000000;
00136 
00137   e = pthread_rwlock_timedrdlock (&r, &ts);
00138   if (e == 0)
00139     {
00140       puts ("third rwlock_timedrdlock did not fail");
00141       result = 1;
00142     }
00143   else if (e != EINVAL)
00144     {
00145       puts ("third rwlock_timedrdlock did not return EINVAL");
00146       result = 1;
00147     }
00148 
00149   e = pthread_rwlock_timedrdlock (&r, &ts);
00150   if (e == 0)
00151     {
00152       puts ("third rwlock_timedrdlock did not fail");
00153       result = 1;
00154     }
00155   else if (e != EINVAL)
00156     {
00157       puts ("third rwlock_timedrdlock did not return EINVAL");
00158       result = 1;
00159     }
00160 
00161   if (result == 0)
00162     puts ("no bugs");
00163 
00164   return result;
00165 }
00166 
00167 
00168 #define TEST_FUNCTION do_test ()
00169 #include "../test-skeleton.c"