1 /* Helper program for testing the pthread_rwlock_t pretty printer.
2 
3    Copyright (C) 2016-2022 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10 
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <https://www.gnu.org/licenses/>.  */
19 
20 /* Keep the calls to the pthread_* functions on separate lines to make it easy
21    to advance through the program using the gdb 'next' command.  */
22 
23 #include <pthread.h>
24 
25 #define PASS 0
26 #define FAIL 1
27 
28 static int test_locking_reader (pthread_rwlock_t *rwlock);
29 static int test_locking_writer (pthread_rwlock_t *rwlock);
30 
31 int
main(void)32 main (void)
33 {
34   pthread_rwlock_t rwlock;
35 
36   int result = FAIL;
37 
38   if (test_locking_reader (&rwlock) == PASS
39       && test_locking_writer (&rwlock) == PASS)
40     result = PASS;
41   /* Else, one of the pthread_rwlock* functions failed.  */
42 
43   return result;
44 }
45 
46 /* Tests locking the rwlock multiple times as a reader.  */
47 static int
test_locking_reader(pthread_rwlock_t * rwlock)48 test_locking_reader (pthread_rwlock_t *rwlock)
49 {
50   int result = FAIL;
51 
52   if (pthread_rwlock_init (rwlock, NULL) == 0
53       && pthread_rwlock_rdlock (rwlock) == 0 /* Test locking (reader).  */
54       && pthread_rwlock_rdlock (rwlock) == 0
55       && pthread_rwlock_rdlock (rwlock) == 0
56       && pthread_rwlock_unlock (rwlock) == 0
57       && pthread_rwlock_unlock (rwlock) == 0
58       && pthread_rwlock_unlock (rwlock) == 0
59       && pthread_rwlock_destroy (rwlock) == 0)
60     result = PASS;
61 
62   return result;
63 }
64 
65 /* Tests locking the rwlock as a writer.  */
66 static int
test_locking_writer(pthread_rwlock_t * rwlock)67 test_locking_writer (pthread_rwlock_t *rwlock)
68 {
69   int result = FAIL;
70 
71   if (pthread_rwlock_init (rwlock, NULL) == 0
72       && pthread_rwlock_wrlock (rwlock) == 0 /* Test locking (writer).  */
73       && pthread_rwlock_unlock (rwlock) == 0
74       && pthread_rwlock_destroy (rwlock) == 0)
75     result = PASS;
76 
77   return result;
78 }
79