mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-12 06:10:10 +00:00
2b778ceb40
I used these shell commands: ../glibc/scripts/update-copyrights $PWD/../gnulib/build-aux/update-copyright (cd ../glibc && git commit -am"[this commit message]") and then ignored the output, which consisted lines saying "FOO: warning: copyright statement not found" for each of 6694 files FOO. I then removed trailing white space from benchtests/bench-pthread-locks.c and iconvdata/tst-iconv-big5-hkscs-to-2ucs4.c, to work around this diagnostic from Savannah: remote: *** pre-commit check failed ... remote: *** error: lines with trailing whitespace found remote: error: hook declined to update refs/heads/master
160 lines
3.6 KiB
C
160 lines
3.6 KiB
C
/* Test rwlocks.
|
|
Copyright (C) 2000-2021 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, see
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <pthread.h>
|
|
#include <assert.h>
|
|
#include <error.h>
|
|
#include <errno.h>
|
|
|
|
#define THREADS 1
|
|
|
|
int a;
|
|
int b;
|
|
|
|
/* Get a read lock and assert that a == b. */
|
|
void *
|
|
test1 (void *arg)
|
|
{
|
|
error_t err;
|
|
pthread_rwlock_t *lock = arg;
|
|
int i;
|
|
|
|
for (i = 0; i < 200; i++)
|
|
{
|
|
err = pthread_rwlock_rdlock (lock);
|
|
assert (err == 0);
|
|
|
|
assert (a == b);
|
|
|
|
sched_yield ();
|
|
|
|
assert (a == b);
|
|
|
|
err = pthread_rwlock_unlock (lock);
|
|
assert (err == 0);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
main (int argc, char **argv)
|
|
{
|
|
error_t err;
|
|
pthread_rwlockattr_t attr;
|
|
pthread_rwlock_t lock;
|
|
int pshared;
|
|
|
|
int i;
|
|
pthread_t tid[THREADS];
|
|
void *ret;
|
|
|
|
err = pthread_rwlockattr_init (&attr);
|
|
if (err)
|
|
error (1, err, "pthread_rwlockattr_init");
|
|
|
|
err = pthread_rwlockattr_getpshared (&attr, &pshared);
|
|
if (err)
|
|
error (1, err, "pthread_rwlockattr_getpshared");
|
|
|
|
/* Assert the default state as mandated by POSIX. */
|
|
assert (pshared == PTHREAD_PROCESS_PRIVATE);
|
|
|
|
err = pthread_rwlockattr_setpshared (&attr, pshared);
|
|
if (err)
|
|
error (1, err, "pthread_rwlockattr_setpshared");
|
|
|
|
err = pthread_rwlock_init (&lock, &attr);
|
|
if (err)
|
|
error (1, err, "pthread_rwlock_init");
|
|
|
|
err = pthread_rwlockattr_destroy (&attr);
|
|
if (err)
|
|
error (1, err, "pthread_rwlockattr_destroy");
|
|
|
|
/* Now test the lock. */
|
|
|
|
for (i = 0; i < THREADS; i++)
|
|
{
|
|
err = pthread_create (&tid[i], 0, test1, &lock);
|
|
if (err)
|
|
error (1, err, "pthread_create");
|
|
}
|
|
|
|
for (i = 0; i < 10; i++)
|
|
{
|
|
sched_yield ();
|
|
|
|
/* Get a write lock. */
|
|
pthread_rwlock_wrlock (&lock);
|
|
/* Increment a and b giving other threads a chance to run in
|
|
between. */
|
|
sched_yield ();
|
|
a++;
|
|
sched_yield ();
|
|
b++;
|
|
sched_yield ();
|
|
/* Unlock. */
|
|
pthread_rwlock_unlock (&lock);
|
|
}
|
|
|
|
for (i = 0; i < THREADS; i++)
|
|
{
|
|
err = pthread_join (tid[i], &ret);
|
|
if (err)
|
|
error (1, err, "pthread_join");
|
|
}
|
|
|
|
/* Read lock it. */
|
|
err = pthread_rwlock_tryrdlock (&lock);
|
|
assert (err == 0);
|
|
|
|
/* Try to write lock it. It should fail with EBUSY. */
|
|
err = pthread_rwlock_trywrlock (&lock);
|
|
assert (err == EBUSY);
|
|
|
|
/* Drop the read lock. */
|
|
err = pthread_rwlock_unlock (&lock);
|
|
assert (err == 0);
|
|
|
|
/* Get a write lock. */
|
|
err = pthread_rwlock_trywrlock (&lock);
|
|
assert (err == 0);
|
|
|
|
/* Fail trying to acquire another write lock. */
|
|
err = pthread_rwlock_trywrlock (&lock);
|
|
assert (err == EBUSY);
|
|
|
|
/* Try to get a read lock which should also fail. */
|
|
err = pthread_rwlock_tryrdlock (&lock);
|
|
assert (err == EBUSY);
|
|
|
|
/* Unlock it. */
|
|
err = pthread_rwlock_unlock (&lock);
|
|
assert (err == 0);
|
|
|
|
|
|
err = pthread_rwlock_destroy (&lock);
|
|
if (err)
|
|
error (1, err, "pthread_rwlock_destroy");
|
|
|
|
return 0;
|
|
}
|