Back to index

glibc  2.9
Defines | Functions | Variables
tst-cond16.c File Reference
#include <errno.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.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 TEST_FUNCTION   do_test ()
#define TIMEOUT   40

Functions

void * tf (void *dummy)
int do_test (void)

Variables

pthread_cond_t cv = PTHREAD_COND_INITIALIZER
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER
bool n
bool exiting
FILEf
int count

Define Documentation

#define TEST_FUNCTION   do_test ()

Definition at line 103 of file tst-cond16.c.

#define TIMEOUT   40

Definition at line 104 of file tst-cond16.c.


Function Documentation

int do_test ( void  )

Definition at line 65 of file tst-cond16.c.

{
  f = fopen ("/dev/null", "w");
  if (f == NULL)
    {
      printf ("couldn't open /dev/null, %m\n");
      return 1;
    }

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

  pthread_t th[count];
  int i, ret;
  for (i = 0; i < count; ++i)
    if ((ret = pthread_create (&th[i], NULL, tf, NULL)) != 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);

  fclose (f);
  return 0;
}

Here is the call graph for this function:

void* tf ( void *  dummy)

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

{
  bool loop = true;

  while (loop)
    {
      pthread_mutex_lock (&lock);
      while (n && !exiting)
       pthread_cond_wait (&cv, &lock);
      n = true;
      pthread_mutex_unlock (&lock);

      fputs (".", f);

      pthread_mutex_lock (&lock);
      n = false;
      if (exiting)
       loop = false;
#ifdef UNLOCK_AFTER_BROADCAST
      pthread_cond_broadcast (&cv);
      pthread_mutex_unlock (&lock);
#else
      pthread_mutex_unlock (&lock);
      pthread_cond_broadcast (&cv);
#endif
    }

  return NULL;
}

Here is the call graph for this function:


Variable Documentation

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

Definition at line 27 of file tst-cond16.c.

bool exiting

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

FILE* f

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

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

bool n

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