--- /dev/null
+/* Test program for timedout read/write lock functions.
+ Copyright (C) 2000 Free Software Foundation, Inc.
+ Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 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
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <errno.h>
+#include <error.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+
+#define NWRITERS 15
+#define WRITETRIES 10
+#define NREADERS 15
+#define READTRIES 15
+
+#define TIMEOUT 1000000
+#define DELAY 1000000
+
+static pthread_rwlock_t lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP;
+
+
+static void *
+writer_thread (void *nr)
+{
+ struct timespec ts;
+ struct timespec delay;
+ int n;
+
+ ts.tv_sec = 0;
+ ts.tv_nsec = TIMEOUT;
+
+ delay.tv_sec = 0;
+ delay.tv_nsec = DELAY;
+
+ for (n = 0; n < WRITETRIES; ++n)
+ {
+ do
+ {
+ clock_gettime (CLOCK_REALTIME, &ts);
+
+ ts.tv_nsec += 2 * TIMEOUT;
+
+ printf ("writer thread %ld tries again\n", (long int) nr);
+ }
+ //while (pthread_rwlock_wrlock (&lock), 0);
+ while (pthread_rwlock_timedwrlock (&lock, &ts) == ETIMEDOUT);
+
+ printf ("writer thread %ld succeeded\n", (long int) nr);
+
+ nanosleep (&delay, NULL);
+
+ pthread_rwlock_unlock (&lock);
+
+ printf ("writer thread %ld released\n", (long int) nr);
+ }
+
+ return NULL;
+}
+
+
+static void *
+reader_thread (void *nr)
+{
+ struct timespec ts;
+ struct timespec delay;
+ int n;
+
+ delay.tv_sec = 0;
+ delay.tv_nsec = DELAY;
+
+ for (n = 0; n < READTRIES; ++n)
+ {
+ do
+ {
+ clock_gettime (CLOCK_REALTIME, &ts);
+
+ ts.tv_nsec += TIMEOUT;
+
+ printf ("reader thread %ld tries again\n", (long int) nr);
+ }
+ //while (pthread_rwlock_rdlock (&lock), 0);
+ while (pthread_rwlock_timedrdlock (&lock, &ts) == ETIMEDOUT);
+
+ printf ("reader thread %ld succeeded\n", (long int) nr);
+
+ nanosleep (&delay, NULL);
+
+ pthread_rwlock_unlock (&lock);
+
+ printf ("reader thread %ld released\n", (long int) nr);
+ }
+
+ return NULL;
+}
+
+
+int
+main (void)
+{
+ pthread_t thwr[NWRITERS];
+ pthread_t thrd[NREADERS];
+ int n;
+ void *res;
+
+ /* Make standard error the same as standard output. */
+ dup2 (1, 2);
+
+ /* Make sure we see all message, even those on stdout. */
+ setvbuf (stdout, NULL, _IONBF, 0);
+
+ for (n = 0; n < NWRITERS; ++n)
+ {
+ int err = pthread_create (&thwr[n], NULL, writer_thread, (void *) n);
+
+ if (err != 0)
+ error (EXIT_FAILURE, err, "cannot create writer thread");
+ }
+
+ for (n = 0; n < NREADERS; ++n)
+ {
+ int err = pthread_create (&thrd[n], NULL, reader_thread, (void *) n);
+
+ if (err != 0)
+ error (EXIT_FAILURE, err, "cannot create reader thread");
+ }
+
+ /* Wait for all the threads. */
+ for (n = 0; n < NWRITERS; ++n)
+ pthread_join (thwr[n], &res);
+ for (n = 0; n < NREADERS; ++n)
+ pthread_join (thrd[n], &res);
+
+ return 0;
+}
#include "spinlock.h"
#include "restart.h"
+/* Function called by pthread_cancel to remove the thread from
+ waiting inside pthread_rwlock_timedrdlock or pthread_rwlock_timedwrlock. */
+
+static int rwlock_rd_extricate_func(void *obj, pthread_descr th)
+{
+ volatile pthread_descr self = thread_self();
+ pthread_rwlock_t *rwlock = obj;
+ int did_remove = 0;
+
+ __pthread_lock((struct _pthread_fastlock *) &rwlock->__rw_lock, self);
+ did_remove = remove_from_queue(&rwlock->__rw_read_waiting, th);
+ __pthread_unlock((struct _pthread_fastlock *) &rwlock->__rw_lock);
+
+ return did_remove;
+}
+
+static int rwlock_wr_extricate_func(void *obj, pthread_descr th)
+{
+ volatile pthread_descr self = thread_self();
+ pthread_rwlock_t *rwlock = obj;
+ int did_remove = 0;
+
+ __pthread_lock((struct _pthread_fastlock *) &rwlock->__rw_lock, self);
+ did_remove = remove_from_queue(&rwlock->__rw_write_waiting, th);
+ __pthread_unlock((struct _pthread_fastlock *) &rwlock->__rw_lock);
+
+ return did_remove;
+}
+
/*
* Check whether the calling thread already owns one or more read locks on the
* specified lock. If so, return a pointer to the read lock info structure
int readers;
_pthread_descr writer;
- __pthread_alt_lock (&rwlock->__rw_lock, NULL);
+ __pthread_lock (&rwlock->__rw_lock, NULL);
readers = rwlock->__rw_readers;
writer = rwlock->__rw_writer;
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
if (readers > 0 || writer != NULL)
return EBUSY;
for (;;)
{
- __pthread_alt_lock (&rwlock->__rw_lock, self);
+ __pthread_lock (&rwlock->__rw_lock, self);
if (rwlock_can_rdlock(rwlock, have_lock_already))
break;
enqueue (&rwlock->__rw_read_waiting, self);
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
suspend (self); /* This is not a cancellation point */
}
++rwlock->__rw_readers;
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
if (have_lock_already || out_of_mem)
{
pthread_descr self = NULL;
pthread_readlock_info *existing;
int out_of_mem, have_lock_already;
+ pthread_extricate_if extr;
if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
return EINVAL;
if (self == NULL)
self = thread_self ();
+ /* Set up extrication interface */
+ extr.pu_object = rwlock;
+ extr.pu_extricate_func = rwlock_rd_extricate_func;
+
+ /* Register extrication interface */
+ __pthread_set_own_extricate_if (self, &extr);
+
for (;;)
{
- if (__pthread_alt_timedlock (&rwlock->__rw_lock, self, abstime) == 0)
- return ETIMEDOUT;
+ __pthread_lock (&rwlock->__rw_lock, self);
if (rwlock_can_rdlock(rwlock, have_lock_already))
break;
enqueue (&rwlock->__rw_read_waiting, self);
- __pthread_alt_unlock (&rwlock->__rw_lock);
- suspend (self); /* This is not a cancellation point */
+ __pthread_unlock (&rwlock->__rw_lock);
+ /* This is not a cancellation point */
+ if (timedsuspend (self, abstime) == 0)
+ {
+ int was_on_queue;
+
+ __pthread_lock (&rwlock->__rw_lock, self);
+ was_on_queue = remove_from_queue (&rwlock->__rw_read_waiting, self);
+ __pthread_unlock (&rwlock->__rw_lock);
+
+ if (was_on_queue)
+ {
+ __pthread_set_own_extricate_if (self, 0);
+ return ETIMEDOUT;
+ }
+
+ /* Eat the outstanding restart() from the signaller */
+ suspend (self);
+ }
}
+ __pthread_set_own_extricate_if (self, 0);
+
++rwlock->__rw_readers;
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
if (have_lock_already || out_of_mem)
{
have_lock_already = rwlock_have_already(&self, rwlock,
&existing, &out_of_mem);
- __pthread_alt_lock (&rwlock->__rw_lock, self);
+ __pthread_lock (&rwlock->__rw_lock, self);
/* 0 is passed to here instead of have_lock_already.
This is to meet Single Unix Spec requirements:
retval = 0;
}
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
if (retval == 0)
{
while(1)
{
- __pthread_alt_lock (&rwlock->__rw_lock, self);
+ __pthread_lock (&rwlock->__rw_lock, self);
if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
{
rwlock->__rw_writer = self;
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
return 0;
}
/* Suspend ourselves, then try again */
enqueue (&rwlock->__rw_write_waiting, self);
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
suspend (self); /* This is not a cancellation point */
}
}
const struct timespec *abstime)
{
pthread_descr self;
+ pthread_extricate_if extr;
if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
return EINVAL;
self = thread_self ();
+ /* Set up extrication interface */
+ extr.pu_object = rwlock;
+ extr.pu_extricate_func = rwlock_wr_extricate_func;
+
+ /* Register extrication interface */
+ __pthread_set_own_extricate_if (self, &extr);
+
while(1)
{
- if (__pthread_alt_timedlock (&rwlock->__rw_lock, self, abstime) == 0)
- return ETIMEDOUT;
+ __pthread_lock (&rwlock->__rw_lock, self);
if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
{
rwlock->__rw_writer = self;
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_set_own_extricate_if (self, 0);
+ __pthread_unlock (&rwlock->__rw_lock);
return 0;
}
/* Suspend ourselves, then try again */
enqueue (&rwlock->__rw_write_waiting, self);
- __pthread_alt_unlock (&rwlock->__rw_lock);
- suspend (self); /* This is not a cancellation point */
+ __pthread_unlock (&rwlock->__rw_lock);
+ /* This is not a cancellation point */
+ if (timedsuspend (self, abstime) == 0)
+ {
+ int was_on_queue;
+
+ __pthread_lock (&rwlock->__rw_lock, self);
+ was_on_queue = remove_from_queue (&rwlock->__rw_write_waiting, self);
+ __pthread_unlock (&rwlock->__rw_lock);
+
+ if (was_on_queue)
+ {
+ __pthread_set_own_extricate_if (self, 0);
+ return ETIMEDOUT;
+ }
+
+ /* Eat the outstanding restart() from the signaller */
+ suspend (self);
+ }
}
}
strong_alias (__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock)
{
int result = EBUSY;
- __pthread_alt_lock (&rwlock->__rw_lock, NULL);
+ __pthread_lock (&rwlock->__rw_lock, NULL);
if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
{
rwlock->__rw_writer = thread_self ();
result = 0;
}
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
return result;
}
pthread_descr torestart;
pthread_descr th;
- __pthread_alt_lock (&rwlock->__rw_lock, NULL);
+ __pthread_lock (&rwlock->__rw_lock, NULL);
if (rwlock->__rw_writer != NULL)
{
/* Unlocking a write lock. */
if (rwlock->__rw_writer != thread_self ())
{
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
return EPERM;
}
rwlock->__rw_writer = NULL;
/* Restart all waiting readers. */
torestart = rwlock->__rw_read_waiting;
rwlock->__rw_read_waiting = NULL;
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
while ((th = dequeue (&torestart)) != NULL)
restart (th);
}
else
{
/* Restart one waiting writer. */
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
restart (th);
}
}
/* Unlocking a read lock. */
if (rwlock->__rw_readers == 0)
{
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
return EPERM;
}
else
th = NULL;
- __pthread_alt_unlock (&rwlock->__rw_lock);
+ __pthread_unlock (&rwlock->__rw_lock);
if (th != NULL)
restart (th);