Back to index

glibc  2.9
Defines | Functions | Variables
tst-rwlock8.c File Reference
#include <errno.h>
#include <error.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "../test-skeleton.c"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define NWRITERS   15
#define WRITETRIES   10
#define NREADERS   15
#define READTRIES   15
#define DELAY   1000000
#define INIT   PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
#define TIMEOUT   30
#define TEST_FUNCTION   do_test ()

Functions

static void * writer_thread (void *nr)
static void * reader_thread (void *nr)
static int do_test (void)

Variables

static pthread_rwlock_t lock = INIT

Define Documentation

#define DELAY   1000000

Definition at line 34 of file tst-rwlock8.c.

#define INIT   PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP

Definition at line 37 of file tst-rwlock8.c.

#define NREADERS   15

Definition at line 31 of file tst-rwlock8.c.

#define NWRITERS   15

Definition at line 29 of file tst-rwlock8.c.

#define READTRIES   15

Definition at line 32 of file tst-rwlock8.c.

#define TEST_FUNCTION   do_test ()

Definition at line 163 of file tst-rwlock8.c.

#define TIMEOUT   30

Definition at line 162 of file tst-rwlock8.c.

#define WRITETRIES   10

Definition at line 30 of file tst-rwlock8.c.


Function Documentation

static int do_test ( void  ) [static]

Definition at line 116 of file tst-rwlock8.c.

{
  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)
    if (pthread_create (&thwr[n], NULL, writer_thread,
                     (void *) (long int) n) != 0)
      {
       puts ("writer create failed");
       exit (1);
      }

  for (n = 0; n < NREADERS; ++n)
    if (pthread_create (&thrd[n], NULL, reader_thread,
                     (void *) (long int) n) != 0)
      {
       puts ("reader create failed");
       exit (1);
      }

  /* Wait for all the threads.  */
  for (n = 0; n < NWRITERS; ++n)
    if (pthread_join (thwr[n], &res) != 0)
      {
       puts ("writer join failed");
       exit (1);
      }
  for (n = 0; n < NREADERS; ++n)
    if (pthread_join (thrd[n], &res) != 0)
      {
       puts ("reader join failed");
       exit (1);
      }

  return 0;
}

Here is the call graph for this function:

static void* reader_thread ( void *  nr) [static]

Definition at line 80 of file tst-rwlock8.c.

{
  struct timespec delay;
  int n;

  delay.tv_sec = 0;
  delay.tv_nsec = DELAY;

  for (n = 0; n < READTRIES; ++n)
    {
      printf ("reader thread %ld tries again\n", (long int) nr);

      if (pthread_rwlock_rdlock (&lock) != 0)
       {
         puts ("rdlock failed");
         exit (1);
       }

      printf ("reader thread %ld succeeded\n", (long int) nr);

      nanosleep (&delay, NULL);

      if (pthread_rwlock_unlock (&lock) != 0)
       {
         puts ("unlock for reader failed");
         exit (1);
       }

      printf ("reader thread %ld released\n", (long int) nr);
    }

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void* writer_thread ( void *  nr) [static]

Definition at line 44 of file tst-rwlock8.c.

{
  struct timespec delay;
  int n;

  delay.tv_sec = 0;
  delay.tv_nsec = DELAY;

  for (n = 0; n < WRITETRIES; ++n)
    {
      printf ("writer thread %ld tries again\n", (long int) nr);

      if (pthread_rwlock_wrlock (&lock) != 0)
       {
         puts ("wrlock failed");
         exit (1);
       }

      printf ("writer thread %ld succeeded\n", (long int) nr);

      nanosleep (&delay, NULL);

      if (pthread_rwlock_unlock (&lock) != 0)
       {
         puts ("unlock for writer failed");
         exit (1);
       }

      printf ("writer thread %ld released\n", (long int) nr);
    }

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

pthread_rwlock_t lock = INIT [static]

Definition at line 40 of file tst-rwlock8.c.