1/* Test pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <https://www.gnu.org/licenses/>. */
18
19#define _GNU_SOURCE
20
21#include <pthread.h>
22#include <stdio.h>
23#include <assert.h>
24#include <error.h>
25#include <errno.h>
26#include <sys/time.h>
27
28#define THREADS 10
29
30pthread_rwlock_t rwlock;
31
32void *
33test (void *arg)
34{
35 error_t err;
36 int foo = (int) arg;
37 struct timespec ts;
38 struct timeval before, after;
39 int diff;
40
41 gettimeofday (tv: &before, tz: 0);
42 ts.tv_sec = before.tv_sec + 1;
43 ts.tv_nsec = before.tv_usec * 1000;
44
45 printf (format: "Thread %d starting wait @ %d\n", pthread_self (),
46 (int) before.tv_sec);
47
48 if (foo % 2 == 0)
49 err = pthread_rwlock_timedrdlock (rwlock: &rwlock, abstime: &ts);
50 else
51 err = pthread_rwlock_timedwrlock (rwlock: &rwlock, abstime: &ts);
52
53 assert (err == ETIMEDOUT);
54
55 gettimeofday (tv: &after, tz: 0);
56
57 printf (format: "Thread %d ending wait @ %d\n", pthread_self (), (int) after.tv_sec);
58
59 diff = after.tv_sec * 1000000 + after.tv_usec
60 - before.tv_sec * 1000000 - before.tv_usec;
61
62 if (diff < 900000 || diff > 1100000)
63 error (status: 1, errnum: EGRATUITOUS, format: "pthread_mutex_timedlock waited %d us", diff);
64
65 return 0;
66}
67
68int
69main (int argc, char **argv)
70{
71 error_t err;
72 int i;
73 pthread_t tid[THREADS];
74
75 err = pthread_rwlock_init (rwlock: &rwlock, attr: 0);
76 if (err)
77 error (status: 1, errnum: err, format: "pthread_rwlock_init");
78
79 /* Lock it so all the threads will block. */
80 err = pthread_rwlock_wrlock (rwlock: &rwlock);
81 assert (err == 0);
82
83 for (i = 0; i < THREADS; i++)
84 {
85 err = pthread_create (newthread: &tid[i], attr: 0, start_routine: test, arg: (void *) i);
86 if (err)
87 error (status: 1, errnum: err, format: "pthread_create");
88 }
89
90 for (i = 0; i < THREADS; i++)
91 {
92 void *ret;
93
94 err = pthread_join (th: tid[i], thread_return: &ret);
95 if (err)
96 error (status: 1, errnum: err, format: "pthread_join");
97
98 assert (ret == 0);
99 }
100
101 return 0;
102}
103

source code of glibc/htl/tests/test-15.c