i386: Remove memset_chk-nonshared.S
[glibc.git] / htl / tests / test-15.c
blob1a0b7482ab2c0b87d7a04ae3ec150a03b5a75938
1 /* Test pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock.
2 Copyright (C) 2000-2023 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 <https://www.gnu.org/licenses/>. */
19 #define _GNU_SOURCE
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>
28 #define THREADS 10
30 pthread_rwlock_t rwlock;
32 void *
33 test (void *arg)
35 error_t err;
36 int foo = (int) arg;
37 struct timespec ts;
38 struct timeval before, after;
39 int diff;
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 (),
46 (int) before.tv_sec);
48 if (foo % 2 == 0)
49 err = pthread_rwlock_timedrdlock (&rwlock, &ts);
50 else
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);
65 return 0;
68 int
69 main (int argc, char **argv)
71 error_t err;
72 int i;
73 pthread_t tid[THREADS];
75 err = pthread_rwlock_init (&rwlock, 0);
76 if (err)
77 error (1, err, "pthread_rwlock_init");
79 /* Lock it so all the threads will block. */
80 err = pthread_rwlock_wrlock (&rwlock);
81 assert (err == 0);
83 for (i = 0; i < THREADS; i++)
85 err = pthread_create (&tid[i], 0, test, (void *) i);
86 if (err)
87 error (1, err, "pthread_create");
90 for (i = 0; i < THREADS; i++)
92 void *ret;
94 err = pthread_join (tid[i], &ret);
95 if (err)
96 error (1, err, "pthread_join");
98 assert (ret == 0);
101 return 0;