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/>. */
31 /* Get a read lock and assert that a == b. */
36 pthread_rwlock_t
*lock
= arg
;
39 for (i
= 0; i
< 200; i
++)
41 err
= pthread_rwlock_rdlock (lock
);
50 err
= pthread_rwlock_unlock (lock
);
58 main (int argc
, char **argv
)
61 pthread_rwlockattr_t attr
;
62 pthread_rwlock_t lock
;
66 pthread_t tid
[THREADS
];
69 err
= pthread_rwlockattr_init (&attr
);
71 error (1, err
, "pthread_rwlockattr_init");
73 err
= pthread_rwlockattr_getpshared (&attr
, &pshared
);
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
);
82 error (1, err
, "pthread_rwlockattr_setpshared");
84 err
= pthread_rwlock_init (&lock
, &attr
);
86 error (1, err
, "pthread_rwlock_init");
88 err
= pthread_rwlockattr_destroy (&attr
);
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
);
98 error (1, err
, "pthread_create");
101 for (i
= 0; i
< 10; i
++)
105 /* Get a write lock. */
106 pthread_rwlock_wrlock (&lock
);
107 /* Increment a and b giving other threads a chance to run in
115 pthread_rwlock_unlock (&lock
);
118 for (i
= 0; i
< THREADS
; i
++)
120 err
= pthread_join (tid
[i
], &ret
);
122 error (1, err
, "pthread_join");
126 err
= pthread_rwlock_tryrdlock (&lock
);
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
);
137 /* Get a write lock. */
138 err
= pthread_rwlock_trywrlock (&lock
);
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
);
150 err
= pthread_rwlock_unlock (&lock
);
154 err
= pthread_rwlock_destroy (&lock
);
156 error (1, err
, "pthread_rwlock_destroy");