]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/pthread/tst-rwlock4.c
1 /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
32 size_t ps
= sysconf (_SC_PAGESIZE
);
33 char tmpfname
[] = "/tmp/tst-rwlock4.XXXXXX";
38 pthread_rwlockattr_t a
;
44 fd
= mkstemp (tmpfname
);
47 printf ("cannot open temporary file: %m\n");
51 /* Make sure it is always removed. */
54 /* Create one page of data. */
55 memset (data
, '\0', ps
);
57 /* Write the data to the file. */
58 if (write (fd
, data
, ps
) != (ssize_t
) ps
)
64 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
65 if (mem
== MAP_FAILED
)
67 printf ("mmap failed: %m\n");
71 r
= (pthread_rwlock_t
*) (((uintptr_t) mem
+ __alignof (pthread_rwlock_t
))
72 & ~(__alignof (pthread_rwlock_t
) - 1));
75 if (pthread_rwlockattr_init (&a
) != 0)
77 puts ("rwlockattr_init failed");
81 if (pthread_rwlockattr_getpshared (&a
, &s
) != 0)
83 puts ("1st rwlockattr_getpshared failed");
87 if (s
!= PTHREAD_PROCESS_PRIVATE
)
89 puts ("default pshared value wrong");
93 if (pthread_rwlockattr_setpshared (&a
, PTHREAD_PROCESS_SHARED
) != 0)
95 puts ("rwlockattr_setpshared failed");
99 if (pthread_rwlockattr_getpshared (&a
, &s
) != 0)
101 puts ("2nd rwlockattr_getpshared failed");
105 if (s
!= PTHREAD_PROCESS_SHARED
)
107 puts ("pshared value after setpshared call wrong");
111 if (pthread_rwlock_init (r
, &a
) != 0)
113 puts ("rwlock_init failed");
117 if (pthread_rwlock_rdlock (r
) != 0)
119 puts ("rwlock_rdlock failed");
123 if (pthread_rwlockattr_destroy (&a
) != 0)
125 puts ("rwlockattr_destroy failed");
129 err
= pthread_rwlock_trywrlock (r
);
132 puts ("rwlock_trywrlock succeeded");
135 else if (err
!= EBUSY
)
137 puts ("rwlock_trywrlock didn't return EBUSY");
143 puts ("going to fork now");
147 puts ("fork failed");
152 /* Play some lock ping-pong. It's our turn to unlock first. */
155 puts ("child: *p != 0");
159 if (pthread_rwlock_unlock (r
) != 0)
161 puts ("child: 1st rwlock_unlock failed");
169 if (pthread_rwlock_wrlock (r
) != 0)
171 puts ("parent: rwlock_wrlock failed");
181 puts ("parent done");
187 #define TEST_FUNCTION do_test ()
188 #include "../test-skeleton.c"