1 /* Test pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
30 pthread_rwlock_t rwlock
;
38 struct timeval before
, after
;
41 gettimeofday (&before
, 0);
42 ts
.tv_sec
= before
.tv_sec
+ 1;
43 ts
.tv_nsec
= before
.tv_usec
* 1000;
45 printf ("Thread %d starting wait @ %d\n", pthread_self (),
49 err
= pthread_rwlock_timedrdlock (&rwlock
, &ts
);
51 err
= pthread_rwlock_timedwrlock (&rwlock
, &ts
);
53 assert (err
== ETIMEDOUT
);
55 gettimeofday (&after
, 0);
57 printf ("Thread %d ending wait @ %d\n", pthread_self (), (int) after
.tv_sec
);
59 diff
= after
.tv_sec
* 1000000 + after
.tv_usec
60 - before
.tv_sec
* 1000000 - before
.tv_usec
;
62 if (diff
< 900000 || diff
> 1100000)
63 error (1, EGRATUITOUS
, "pthread_mutex_timedlock waited %d us", diff
);
69 main (int argc
, char **argv
)
73 pthread_t tid
[THREADS
];
75 err
= pthread_rwlock_init (&rwlock
, 0);
77 error (1, err
, "pthread_rwlock_init");
79 /* Lock it so all the threads will block. */
80 err
= pthread_rwlock_wrlock (&rwlock
);
83 for (i
= 0; i
< THREADS
; i
++)
85 err
= pthread_create (&tid
[i
], 0, test
, (void *) i
);
87 error (1, err
, "pthread_create");
90 for (i
= 0; i
< THREADS
; i
++)
94 err
= pthread_join (tid
[i
], &ret
);
96 error (1, err
, "pthread_join");