1 /* Helper program for testing the pthread_rwlock_t and pthread_rwlockattr_t
2 pretty printers.
3
4 Copyright (C) 2016-2022 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
20
21 /* Keep the calls to the pthread_* functions on separate lines to make it easy
22 to advance through the program using the gdb 'next' command. */
23
24 #include <pthread.h>
25
26 #define PASS 0
27 #define FAIL 1
28
29 /* Need these so we don't have lines longer than 79 chars. */
30 #define SET_KIND(attr, kind) pthread_rwlockattr_setkind_np (attr, kind)
31 #define SET_SHARED(attr, shared) pthread_rwlockattr_setpshared (attr, shared)
32
33 static int rwlock_reinit (pthread_rwlock_t *rwlock,
34 const pthread_rwlockattr_t *attr);
35 static int test_setkind_np (pthread_rwlock_t *rwlock,
36 pthread_rwlockattr_t *attr);
37 static int test_setpshared (pthread_rwlock_t *rwlock,
38 pthread_rwlockattr_t *attr);
39
40 int
main(void)41 main (void)
42 {
43 pthread_rwlock_t rwlock;
44 pthread_rwlockattr_t attr;
45 int result = FAIL;
46
47 if (pthread_rwlockattr_init (&attr) == 0
48 && pthread_rwlock_init (&rwlock, NULL) == 0
49 && test_setkind_np (&rwlock, &attr) == PASS
50 && test_setpshared (&rwlock, &attr) == PASS)
51 result = PASS;
52 /* Else, one of the pthread_rwlock* functions failed. */
53
54 return result;
55 }
56
57 /* Destroys RWLOCK and re-initializes it using ATTR. */
58 static int
rwlock_reinit(pthread_rwlock_t * rwlock,const pthread_rwlockattr_t * attr)59 rwlock_reinit (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
60 {
61 int result = FAIL;
62
63 if (pthread_rwlock_destroy (rwlock) == 0
64 && pthread_rwlock_init (rwlock, attr) == 0)
65 result = PASS;
66
67 return result;
68 }
69
70 /* Tests setting whether the rwlock prefers readers or writers. */
71 static int
test_setkind_np(pthread_rwlock_t * rwlock,pthread_rwlockattr_t * attr)72 test_setkind_np (pthread_rwlock_t *rwlock, pthread_rwlockattr_t *attr)
73 {
74 int result = FAIL;
75
76 if (SET_KIND (attr, PTHREAD_RWLOCK_PREFER_READER_NP) == 0 /* Set kind. */
77 && rwlock_reinit (rwlock, attr) == PASS
78 && SET_KIND (attr, PTHREAD_RWLOCK_PREFER_WRITER_NP) == 0
79 && rwlock_reinit (rwlock, attr) == PASS
80 && SET_KIND (attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) == 0
81 && rwlock_reinit (rwlock, attr) == PASS)
82 result = PASS;
83
84 return result;
85 }
86
87 /* Tests setting whether the rwlock can be shared between processes. */
88 static int
test_setpshared(pthread_rwlock_t * rwlock,pthread_rwlockattr_t * attr)89 test_setpshared (pthread_rwlock_t *rwlock, pthread_rwlockattr_t *attr)
90 {
91 int result = FAIL;
92
93 if (SET_SHARED (attr, PTHREAD_PROCESS_SHARED) == 0 /* Set shared. */
94 && rwlock_reinit (rwlock, attr) == PASS
95 && SET_SHARED (attr, PTHREAD_PROCESS_PRIVATE) == 0
96 && rwlock_reinit (rwlock, attr) == PASS)
97 result = PASS;
98
99 return result;
100 }
101