Back to index

glibc  2.9
Defines | Functions | Variables
tst-cond18.c File Reference
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "../test-skeleton.c"

Go to the source code of this file.

Defines

#define TEST_FUNCTION   do_test ()
#define TIMEOUT   40

Functions

void * tf (void *id)
int do_test (void)

Variables

pthread_cond_t cv = PTHREAD_COND_INITIALIZER
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER
bool exiting
int fd
int count
int spins
int nn

Define Documentation

#define TEST_FUNCTION   do_test ()

Definition at line 115 of file tst-cond18.c.

#define TIMEOUT   40

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


Function Documentation

int do_test ( void  )

Definition at line 76 of file tst-cond18.c.

{
  fd = open ("/dev/null", O_WRONLY);
  if (fd < 0)
    {
      printf ("couldn't open /dev/null, %m\n");
      return 1;
    }

  count = sysconf (_SC_NPROCESSORS_ONLN);
  if (count <= 0)
    count = 1;
  count *= 8;

  pthread_t th[count + 1];
  int i, ret;

  for (i = 0; i <= count; ++i)
    if ((ret = pthread_create (&th[i], NULL, tf, (void *) (long) i)) != 0)
      {
       errno = ret;
       printf ("pthread_create %d failed: %m\n", i);
       return 1;
      }

  struct timespec ts = { .tv_sec = 20, .tv_nsec = 0 };
  while (nanosleep (&ts, &ts) != 0);

  pthread_mutex_lock (&lock);
  exiting = true;
  pthread_mutex_unlock (&lock);

  for (i = 0; i < count; ++i)
    pthread_join (th[i], NULL);

  close (fd);
  return 0;
}

Here is the call graph for this function:

void* tf ( void *  id)

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

{
  pthread_mutex_lock (&lock);

  if ((long) id == 0)
    {
      while (!exiting)
       {
         if ((spins++ % 1000) == 0)
           write (fd, ".", 1);
         pthread_mutex_unlock (&lock);

         pthread_mutex_lock (&lock);
         int njobs = rand () % (count + 1);
         nn = njobs;
         if ((rand () % 30) == 0)
           pthread_cond_broadcast (&cv);
         else
           while (njobs--)
             pthread_cond_signal (&cv);
       }

      pthread_cond_broadcast (&cv);
    }
  else
    {
      while (!exiting)
       {
         while (!nn && !exiting)
           pthread_cond_wait (&cv, &lock);
         --nn;
         pthread_mutex_unlock (&lock);

         pthread_mutex_lock (&lock);
       }
    }

  pthread_mutex_unlock (&lock);
  return NULL;
}

Here is the call graph for this function:


Variable Documentation

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

Definition at line 28 of file tst-cond18.c.

bool exiting

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

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

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

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

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