Back to index

glibc  2.9
Defines | Functions | Variables
tst-cond5.c File Reference
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "../test-skeleton.c"

Go to the source code of this file.

Defines

#define TEST_FUNCTION   do_test ()

Functions

static int do_test (void)

Variables

static pthread_mutex_t mut
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER

Define Documentation

#define TEST_FUNCTION   do_test ()

Definition at line 105 of file tst-cond5.c.


Function Documentation

static int do_test ( void  ) [static]

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

{
  pthread_mutexattr_t ma;
  int err;
  struct timespec ts;
  struct timeval tv;

  if (pthread_mutexattr_init (&ma) != 0)
    {
      puts ("mutexattr_init failed");
      exit (1);
    }

  if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
    {
      puts ("mutexattr_settype failed");
      exit (1);
    }

  if (pthread_mutex_init (&mut, &ma) != 0)
    {
      puts ("mutex_init failed");
      exit (1);
    }

  /* Get the mutex.  */
  if (pthread_mutex_lock (&mut) != 0)
    {
      puts ("mutex_lock failed");
      exit (1);
    }

  /* Waiting for the condition will fail.  But we want the timeout here.  */
  if (gettimeofday (&tv, NULL) != 0)
    {
      puts ("gettimeofday failed");
      exit (1);
    }

  TIMEVAL_TO_TIMESPEC (&tv, &ts);
  ts.tv_nsec += 500000000;
  if (ts.tv_nsec >= 1000000000)
    {
      ts.tv_nsec -= 1000000000;
      ++ts.tv_sec;
    }
  err = pthread_cond_timedwait (&cond, &mut, &ts);
  if (err == 0)
    {
      /* This could in theory happen but here without any signal and
        additional waiter it should not.  */
      puts ("cond_timedwait succeeded");
      exit (1);
    }
  else if (err != ETIMEDOUT)
    {
      printf ("cond_timedwait returned with %s\n", strerror (err));
      exit (1);
    }

  err = pthread_mutex_unlock (&mut);
  if (err != 0)
    {
      printf ("mutex_unlock failed: %s\n", strerror (err));
      exit (1);
    }

  return 0;
}

Here is the call graph for this function:


Variable Documentation

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

pthread_mutex_t mut [static]

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