Back to index

glibc  2.9
Defines | Functions | Variables
tst-mutex7.c File Reference
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.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 TYPE   PTHREAD_MUTEX_DEFAULT
#define ROUNDS   1000
#define N   100
#define TIMEOUT   60
#define TEST_FUNCTION   do_test ()

Functions

static void * tf (void *arg)
static int do_test (void)

Variables

static pthread_mutex_t lock

Define Documentation

#define N   100

Definition at line 36 of file tst-mutex7.c.

#define ROUNDS   1000

Definition at line 35 of file tst-mutex7.c.

#define TEST_FUNCTION   do_test ()

Definition at line 164 of file tst-mutex7.c.

#define TIMEOUT   60

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

#define TYPE   PTHREAD_MUTEX_DEFAULT

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


Function Documentation

static int do_test ( void  ) [static]

Definition at line 68 of file tst-mutex7.c.

{
  pthread_mutexattr_t a;

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

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

#ifdef ENABLE_PI
  if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
    {
      puts ("pthread_mutexattr_setprotocol failed");
      return 1;
    }
#endif

  int e = pthread_mutex_init (&lock, &a);
  if (e != 0)
    {
#ifdef ENABLE_PI
      if (e == ENOTSUP)
       {
         puts ("PI mutexes unsupported");
         return 0;
       }
#endif
      puts ("mutex_init failed");
      return 1;
    }

  if (pthread_mutexattr_destroy (&a) != 0)
    {
      puts ("mutexattr_destroy failed");
      return 1;
    }

  pthread_attr_t at;
  pthread_t th[N];
  int cnt;

  if (pthread_attr_init (&at) != 0)
    {
      puts ("attr_init failed");
      return 1;
    }

  if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
    {
      puts ("attr_setstacksize failed");
      return 1;
    }

  if (pthread_mutex_lock (&lock) != 0)
    {
      puts ("locking in parent failed");
      return 1;
    }

  for (cnt = 0; cnt < N; ++cnt)
    if (pthread_create (&th[cnt], &at, tf, (void *) (long int) cnt) != 0)
      {
       printf ("creating thread %d failed\n", cnt);
       return 1;
      }

  if (pthread_attr_destroy (&at) != 0)
    {
      puts ("attr_destroy failed");
      return 1;
    }

  if (pthread_mutex_unlock (&lock) != 0)
    {
      puts ("unlocking in parent failed");
      return 1;
    }

  for (cnt = 0; cnt < N; ++cnt)
    if (pthread_join (th[cnt], NULL) != 0)
      {
       printf ("joining thread %d failed\n", cnt);
       return 1;
      }

  return 0;
}

Here is the call graph for this function:

static void* tf ( void *  arg) [static]

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

{
  int nr = (long int) arg;
  int cnt;
  struct timespec ts = { .tv_sec = 0, .tv_nsec = 11000 };

  for (cnt = 0; cnt < ROUNDS; ++cnt)
    {
      if (pthread_mutex_lock (&lock) != 0)
       {
         printf ("thread %d: failed to get the lock\n", nr);
         return (void *) 1l;
       }

      if (pthread_mutex_unlock (&lock) != 0)
       {
         printf ("thread %d: failed to release the lock\n", nr);
         return (void *) 1l;
       }

      nanosleep (&ts, NULL);
    }

  return NULL;
}

Here is the call graph for this function:


Variable Documentation

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