(root)/
glibc-2.38/
nptl/
tst-rwlock2.c
       1  /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <errno.h>
      19  #include <pthread.h>
      20  #include <stdio.h>
      21  
      22  
      23  static int
      24  do_test (void)
      25  {
      26    pthread_rwlock_t r;
      27    pthread_rwlockattr_t at;
      28    int e;
      29  
      30    if (pthread_rwlockattr_init (&at) != 0)
      31      {
      32        puts ("rwlockattr_init failed");
      33        return 1;
      34      }
      35    puts ("rwlockattr_init succeeded");
      36  
      37  #ifndef TYPE
      38  # define TYPE PTHREAD_RWLOCK_PREFER_READER_NP
      39  #endif
      40  
      41    if (pthread_rwlockattr_setkind_np (&at, TYPE) != 0)
      42      {
      43        puts ("rwlockattr_setkind failed");
      44        return 1;
      45      }
      46    puts ("rwlockattr_setkind succeeded");
      47  
      48    if (pthread_rwlock_init (&r, &at) != 0)
      49      {
      50        puts ("rwlock_init failed");
      51        return 1;
      52      }
      53    puts ("rwlock_init succeeded");
      54  
      55    if (pthread_rwlockattr_destroy (&at) != 0)
      56      {
      57        puts ("rwlockattr_destroy failed");
      58        return 1;
      59      }
      60    puts ("rwlockattr_destroy succeeded");
      61  
      62    if (pthread_rwlock_wrlock (&r) != 0)
      63      {
      64        puts ("1st rwlock_wrlock failed");
      65        return 1;
      66      }
      67    puts ("1st rwlock_wrlock succeeded");
      68  
      69    e = pthread_rwlock_tryrdlock (&r);
      70    if (e == 0)
      71      {
      72        puts ("rwlock_tryrdlock on rwlock with writer succeeded");
      73        return 1;
      74      }
      75    if (e != EBUSY)
      76      {
      77        puts ("rwlock_tryrdlock on rwlock with writer return value != EBUSY");
      78        return 1;
      79      }
      80    puts ("rwlock_tryrdlock on rwlock with writer failed with EBUSY");
      81  
      82    e = pthread_rwlock_trywrlock (&r);
      83    if (e == 0)
      84      {
      85        puts ("rwlock_trywrlock on rwlock with writer succeeded");
      86        return 1;
      87      }
      88    if (e != EBUSY)
      89      {
      90        puts ("rwlock_trywrlock on rwlock with writer return value != EBUSY");
      91        return 1;
      92      }
      93    puts ("rwlock_trywrlock on rwlock with writer failed with EBUSY");
      94  
      95    if (pthread_rwlock_unlock (&r) != 0)
      96      {
      97        puts ("1st rwlock_unlock failed");
      98        return 1;
      99      }
     100    puts ("1st rwlock_unlock succeeded");
     101  
     102    if (pthread_rwlock_tryrdlock (&r) != 0)
     103      {
     104        puts ("rwlock_tryrdlock on unlocked rwlock failed");
     105        return 1;
     106      }
     107    puts ("rwlock_tryrdlock on unlocked rwlock succeeded");
     108  
     109    e = pthread_rwlock_trywrlock (&r);
     110    if (e == 0)
     111      {
     112        puts ("rwlock_trywrlock on rwlock with reader succeeded");
     113        return 1;
     114      }
     115    if (e != EBUSY)
     116      {
     117        puts ("rwlock_trywrlock on rwlock with reader return value != EBUSY");
     118        return 1;
     119      }
     120    puts ("rwlock_trywrlock on rwlock with reader failed with EBUSY");
     121  
     122    if (pthread_rwlock_unlock (&r) != 0)
     123      {
     124        puts ("2nd rwlock_unlock failed");
     125        return 1;
     126      }
     127    puts ("2nd rwlock_unlock succeeded");
     128  
     129    if (pthread_rwlock_trywrlock (&r) != 0)
     130      {
     131        puts ("rwlock_trywrlock on unlocked rwlock failed");
     132        return 1;
     133      }
     134    puts ("rwlock_trywrlock on unlocked rwlock succeeded");
     135  
     136    e = pthread_rwlock_tryrdlock (&r);
     137    if (e == 0)
     138      {
     139        puts ("rwlock_tryrdlock on rwlock with writer succeeded");
     140        return 1;
     141      }
     142    if (e != EBUSY)
     143      {
     144        puts ("rwlock_tryrdlock on rwlock with writer return value != EBUSY");
     145        return 1;
     146      }
     147    puts ("rwlock_tryrdlock on rwlock with writer failed with EBUSY");
     148  
     149    if (pthread_rwlock_unlock (&r) != 0)
     150      {
     151        puts ("3rd rwlock_unlock failed");
     152        return 1;
     153      }
     154    puts ("3rd rwlock_unlock succeeded");
     155  
     156    if (pthread_rwlock_destroy (&r) != 0)
     157      {
     158        puts ("rwlock_destroy failed");
     159        return 1;
     160      }
     161    puts ("rwlock_destroy succeeded");
     162  
     163    return 0;
     164  }
     165  
     166  #define TEST_FUNCTION do_test ()
     167  #include "../test-skeleton.c"