powerpc: Remove optimized finite
[glibc.git] / htl / tests / test-11.c
blob5261d9786bd6b0e7e9376ff2152200537b15ac6a
1 /* Test rwlocks.
2 Copyright (C) 2000-2019 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/>. */
19 #define _GNU_SOURCE
21 #include <pthread.h>
22 #include <assert.h>
23 #include <error.h>
24 #include <errno.h>
26 #define THREADS 1
28 int a;
29 int b;
31 /* Get a read lock and assert that a == b. */
32 void *
33 test1 (void *arg)
35 error_t err;
36 pthread_rwlock_t *lock = arg;
37 int i;
39 for (i = 0; i < 200; i++)
41 err = pthread_rwlock_rdlock (lock);
42 assert (err == 0);
44 assert (a == b);
46 sched_yield ();
48 assert (a == b);
50 err = pthread_rwlock_unlock (lock);
51 assert (err == 0);
54 return 0;
57 int
58 main (int argc, char **argv)
60 error_t err;
61 pthread_rwlockattr_t attr;
62 pthread_rwlock_t lock;
63 int pshared;
65 int i;
66 pthread_t tid[THREADS];
67 void *ret;
69 err = pthread_rwlockattr_init (&attr);
70 if (err)
71 error (1, err, "pthread_rwlockattr_init");
73 err = pthread_rwlockattr_getpshared (&attr, &pshared);
74 if (err)
75 error (1, err, "pthread_rwlockattr_getpshared");
77 /* Assert the default state as mandated by POSIX. */
78 assert (pshared == PTHREAD_PROCESS_PRIVATE);
80 err = pthread_rwlockattr_setpshared (&attr, pshared);
81 if (err)
82 error (1, err, "pthread_rwlockattr_setpshared");
84 err = pthread_rwlock_init (&lock, &attr);
85 if (err)
86 error (1, err, "pthread_rwlock_init");
88 err = pthread_rwlockattr_destroy (&attr);
89 if (err)
90 error (1, err, "pthread_rwlockattr_destroy");
92 /* Now test the lock. */
94 for (i = 0; i < THREADS; i++)
96 err = pthread_create (&tid[i], 0, test1, &lock);
97 if (err)
98 error (1, err, "pthread_create");
101 for (i = 0; i < 10; i++)
103 sched_yield ();
105 /* Get a write lock. */
106 pthread_rwlock_wrlock (&lock);
107 /* Increment a and b giving other threads a chance to run in
108 between. */
109 sched_yield ();
110 a++;
111 sched_yield ();
112 b++;
113 sched_yield ();
114 /* Unlock. */
115 pthread_rwlock_unlock (&lock);
118 for (i = 0; i < THREADS; i++)
120 err = pthread_join (tid[i], &ret);
121 if (err)
122 error (1, err, "pthread_join");
125 /* Read lock it. */
126 err = pthread_rwlock_tryrdlock (&lock);
127 assert (err == 0);
129 /* Try to write lock it. It should fail with EBUSY. */
130 err = pthread_rwlock_trywrlock (&lock);
131 assert (err == EBUSY);
133 /* Drop the read lock. */
134 err = pthread_rwlock_unlock (&lock);
135 assert (err == 0);
137 /* Get a write lock. */
138 err = pthread_rwlock_trywrlock (&lock);
139 assert (err == 0);
141 /* Fail trying to acquire another write lock. */
142 err = pthread_rwlock_trywrlock (&lock);
143 assert (err == EBUSY);
145 /* Try to get a read lock which should also fail. */
146 err = pthread_rwlock_tryrdlock (&lock);
147 assert (err == EBUSY);
149 /* Unlock it. */
150 err = pthread_rwlock_unlock (&lock);
151 assert (err == 0);
154 err = pthread_rwlock_destroy (&lock);
155 if (err)
156 error (1, err, "pthread_rwlock_destroy");
158 return 0;