]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-rwlock6.c
nptl: Convert some rwlock tests to use libsupport
[thirdparty/glibc.git] / nptl / tst-rwlock6.c
1 /* Copyright (C) 2002-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <support/check.h>
26 #include <support/timespec.h>
27 #include <support/xthread.h>
28 #include <support/xtime.h>
29
30
31 static int kind[] =
32 {
33 PTHREAD_RWLOCK_PREFER_READER_NP,
34 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
35 PTHREAD_RWLOCK_PREFER_WRITER_NP,
36 };
37
38
39 static void *
40 tf (void *arg)
41 {
42 pthread_rwlock_t *r = arg;
43
44 /* Timeout: 0.3 secs. */
45 struct timespec ts_start;
46 xclock_gettime (CLOCK_REALTIME, &ts_start);
47
48 struct timespec ts_timeout = timespec_add (ts_start,
49 make_timespec (0, 300000000));
50
51 puts ("child calling timedrdlock");
52
53 TEST_COMPARE (pthread_rwlock_timedrdlock (r, &ts_timeout), ETIMEDOUT);
54
55 puts ("1st child timedrdlock done");
56
57 TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts_timeout);
58
59 xclock_gettime (CLOCK_REALTIME, &ts_timeout);
60 ts_timeout.tv_sec += 10;
61 /* Note that the following operation makes ts invalid. */
62 ts_timeout.tv_nsec += 1000000000;
63
64 TEST_COMPARE (pthread_rwlock_timedrdlock (r, &ts_timeout), EINVAL);
65
66 puts ("2nd child timedrdlock done");
67
68 return NULL;
69 }
70
71
72 static int
73 do_test (void)
74 {
75 size_t cnt;
76 for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
77 {
78 pthread_rwlock_t r;
79 pthread_rwlockattr_t a;
80
81 if (pthread_rwlockattr_init (&a) != 0)
82 FAIL_EXIT1 ("round %Zu: rwlockattr_t failed\n", cnt);
83
84 if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
85 FAIL_EXIT1 ("round %Zu: rwlockattr_setkind failed\n", cnt);
86
87 if (pthread_rwlock_init (&r, &a) != 0)
88 FAIL_EXIT1 ("round %Zu: rwlock_init failed\n", cnt);
89
90 if (pthread_rwlockattr_destroy (&a) != 0)
91 FAIL_EXIT1 ("round %Zu: rwlockattr_destroy failed\n", cnt);
92
93 struct timespec ts;
94 xclock_gettime (CLOCK_REALTIME, &ts);
95 ++ts.tv_sec;
96
97 /* Get a write lock. */
98 int e = pthread_rwlock_timedwrlock (&r, &ts);
99 if (e != 0)
100 FAIL_EXIT1 ("round %Zu: rwlock_timedwrlock failed (%d)\n", cnt, e);
101
102 puts ("1st timedwrlock done");
103
104 xclock_gettime (CLOCK_REALTIME, &ts);
105 ++ts.tv_sec;
106 TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EDEADLK);
107
108 puts ("1st timedrdlock done");
109
110 xclock_gettime (CLOCK_REALTIME, &ts);
111 ++ts.tv_sec;
112 TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EDEADLK);
113
114 puts ("2nd timedwrlock done");
115
116 pthread_t th;
117 if (pthread_create (&th, NULL, tf, &r) != 0)
118 FAIL_EXIT1 ("round %Zu: create failed\n", cnt);
119
120 puts ("started thread");
121
122 void *status;
123 if (pthread_join (th, &status) != 0)
124 FAIL_EXIT1 ("round %Zu: join failed\n", cnt);
125 if (status != NULL)
126 FAIL_EXIT1 ("failure in round %Zu\n", cnt);
127
128 puts ("joined thread");
129
130 if (pthread_rwlock_destroy (&r) != 0)
131 FAIL_EXIT1 ("round %Zu: rwlock_destroy failed\n", cnt);
132 }
133
134 return 0;
135 }
136
137 #include <support/test-driver.c>