Back to index

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

Go to the source code of this file.

Defines

#define N   20
#define TEST_FUNCTION   do_test ()

Functions

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

Variables

static pthread_t th [N]
static pthread_mutex_t lock [N]

Define Documentation

#define N   20

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

#define TEST_FUNCTION   do_test ()

Definition at line 120 of file tst-basic2.c.


Function Documentation

int do_test ( void  )

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

{
  if (pthread_equal (pthread_self (), pthread_self ()) == 0)
    {
      puts ("pthread_equal (pthread_self (), pthread_self ()) failed");
      exit (1);
    }

  pthread_attr_t at;

  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;
    }

  int i;
  for (i = 0; i < N; ++i)
    {
      if (pthread_mutex_init (&lock[i], NULL) != 0)
       {
         puts ("mutex_init failed");
         exit (1);
       }

      if (pthread_mutex_lock (&lock[i]) != 0)
       {
         puts ("mutex_lock failed");
         exit (1);
       }

      if (pthread_create (&th[i], &at, tf, (void *) (long int) i) != 0)
       {
         puts ("create failed");
         exit (1);
       }

      if (pthread_mutex_unlock (&lock[i]) != 0)
       {
         puts ("mutex_unlock failed");
         exit (1);
       }

      printf ("created thread %d\n", i);
    }

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

  int result = 0;
  for (i = 0; i < N; ++i)
    {
      void *r;
      int e;
      if ((e = pthread_join (th[i], &r)) != 0)
       {
         printf ("join failed: %d\n", e);
         _exit (1);
       }
      else if (r != NULL)
       result = 1;
    }

  return 0;
}

Here is the call graph for this function:

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

Definition at line 33 of file tst-basic2.c.

{
  uintptr_t idx = (uintptr_t) a;

  pthread_mutex_lock (&lock[idx]);

  return pthread_equal (pthread_self (), th[idx]) ? NULL : (void *) 1l;
}

Here is the call graph for this function:


Variable Documentation

pthread_mutex_t lock[N] [static]

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

pthread_t th[N] [static]

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