Back to index

glibc  2.9
tst-rwlock2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002 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 <stdio.h>
00023 
00024 
00025 static int
00026 do_test (void)
00027 {
00028   pthread_rwlock_t r;
00029   pthread_rwlockattr_t at;
00030   int e;
00031 
00032   if (pthread_rwlockattr_init (&at) != 0)
00033     {
00034       puts ("rwlockattr_init failed");
00035       return 1;
00036     }
00037   puts ("rwlockattr_init succeeded");
00038 
00039 #ifndef TYPE
00040 # define TYPE PTHREAD_RWLOCK_PREFER_READER_NP
00041 #endif
00042 
00043   if (pthread_rwlockattr_setkind_np (&at, TYPE) != 0)
00044     {
00045       puts ("rwlockattr_setkind failed");
00046       return 1;
00047     }
00048   puts ("rwlockattr_setkind succeeded");
00049 
00050   if (pthread_rwlock_init (&r, &at) != 0)
00051     {
00052       puts ("rwlock_init failed");
00053       return 1;
00054     }
00055   puts ("rwlock_init succeeded");
00056 
00057   if (pthread_rwlockattr_destroy (&at) != 0)
00058     {
00059       puts ("rwlockattr_destroy failed");
00060       return 1;
00061     }
00062   puts ("rwlockattr_destroy succeeded");
00063 
00064   if (pthread_rwlock_wrlock (&r) != 0)
00065     {
00066       puts ("1st rwlock_wrlock failed");
00067       return 1;
00068     }
00069   puts ("1st rwlock_wrlock succeeded");
00070 
00071   e = pthread_rwlock_tryrdlock (&r);
00072   if (e == 0)
00073     {
00074       puts ("rwlock_tryrdlock on rwlock with writer succeeded");
00075       return 1;
00076     }
00077   if (e != EBUSY)
00078     {
00079       puts ("rwlock_tryrdlock on rwlock with writer return value != EBUSY");
00080       return 1;
00081     }
00082   puts ("rwlock_tryrdlock on rwlock with writer failed with EBUSY");
00083 
00084   e = pthread_rwlock_trywrlock (&r);
00085   if (e == 0)
00086     {
00087       puts ("rwlock_trywrlock on rwlock with writer succeeded");
00088       return 1;
00089     }
00090   if (e != EBUSY)
00091     {
00092       puts ("rwlock_trywrlock on rwlock with writer return value != EBUSY");
00093       return 1;
00094     }
00095   puts ("rwlock_trywrlock on rwlock with writer failed with EBUSY");
00096 
00097   if (pthread_rwlock_unlock (&r) != 0)
00098     {
00099       puts ("1st rwlock_unlock failed");
00100       return 1;
00101     }
00102   puts ("1st rwlock_unlock succeeded");
00103 
00104   if (pthread_rwlock_tryrdlock (&r) != 0)
00105     {
00106       puts ("rwlock_tryrdlock on unlocked rwlock failed");
00107       return 1;
00108     }
00109   puts ("rwlock_tryrdlock on unlocked rwlock succeeded");
00110 
00111   e = pthread_rwlock_trywrlock (&r);
00112   if (e == 0)
00113     {
00114       puts ("rwlock_trywrlock on rwlock with reader succeeded");
00115       return 1;
00116     }
00117   if (e != EBUSY)
00118     {
00119       puts ("rwlock_trywrlock on rwlock with reader return value != EBUSY");
00120       return 1;
00121     }
00122   puts ("rwlock_trywrlock on rwlock with reader failed with EBUSY");
00123 
00124   if (pthread_rwlock_unlock (&r) != 0)
00125     {
00126       puts ("2nd rwlock_unlock failed");
00127       return 1;
00128     }
00129   puts ("2nd rwlock_unlock succeeded");
00130 
00131   if (pthread_rwlock_trywrlock (&r) != 0)
00132     {
00133       puts ("rwlock_trywrlock on unlocked rwlock failed");
00134       return 1;
00135     }
00136   puts ("rwlock_trywrlock on unlocked rwlock succeeded");
00137 
00138   e = pthread_rwlock_tryrdlock (&r);
00139   if (e == 0)
00140     {
00141       puts ("rwlock_tryrdlock on rwlock with writer succeeded");
00142       return 1;
00143     }
00144   if (e != EBUSY)
00145     {
00146       puts ("rwlock_tryrdlock on rwlock with writer return value != EBUSY");
00147       return 1;
00148     }
00149   puts ("rwlock_tryrdlock on rwlock with writer failed with EBUSY");
00150 
00151   if (pthread_rwlock_unlock (&r) != 0)
00152     {
00153       puts ("3rd rwlock_unlock failed");
00154       return 1;
00155     }
00156   puts ("3rd rwlock_unlock succeeded");
00157 
00158   if (pthread_rwlock_destroy (&r) != 0)
00159     {
00160       puts ("rwlock_destroy failed");
00161       return 1;
00162     }
00163   puts ("rwlock_destroy succeeded");
00164 
00165   return 0;
00166 }
00167 
00168 #define TEST_FUNCTION do_test ()
00169 #include "../test-skeleton.c"