Back to index

glibc  2.9
Defines | Functions | Variables
tst-cancel1.c File Reference
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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 ()

Functions

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

Variables

static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER
static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER
static int cntr

Define Documentation

#define TEST_FUNCTION   do_test ()

Definition at line 162 of file tst-cancel1.c.


Function Documentation

static void cleanup ( void *  arg) [static]

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

{
  if (arg != (void *) 42l)
    cntr = 42;
  else
    cntr = 1;
}
static int do_test ( void  ) [static]

Definition at line 92 of file tst-cancel1.c.

{
  int err;
  pthread_t th;
  int result = 0;
  void *retval;

  /* Get the mutexes.  */
  err = pthread_mutex_lock (&m1);
  if (err != 0)
    {
      printf ("parent: 1st mutex_lock failed: %s\n", strerror (err));
      return 1;
    }
  err = pthread_mutex_lock (&m2);
  if (err != 0)
    {
      printf ("parent: 2nd mutex_lock failed: %s\n", strerror (err));
      return 1;
    }

  err = pthread_create (&th, NULL, tf, NULL);
  if (err != 0)
    {
      printf ("create failed: %s\n", strerror (err));
      return 1;
    }

  err = pthread_mutex_lock (&m2);
  if (err != 0)
    {
      printf ("parent: 3rd mutex_lock failed: %s\n", strerror (err));
      return 1;
    }

  err = pthread_cancel (th);
  if (err != 0)
    {
      printf ("cancel failed: %s\n", strerror (err));
      return 1;
    }

  err = pthread_join (th, &retval);
  if (err != 0)
    {
      printf ("join failed: %s\n", strerror (err));
      return 1;
    }

  if (retval != PTHREAD_CANCELED)
    {
      printf ("wrong return value: %p\n", retval);
      result = 1;
    }

  if (cntr == 42)
    {
      puts ("cleanup handler called with wrong argument");
      result = 1;
    }
  else if (cntr != 1)
    {
      puts ("cleanup handling not called");
      result = 1;
    }

  return result;
}

Here is the call graph for this function:

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

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

{
  /* Ignore all signals.  This must not have any effect on delivering
     the cancellation signal.  */
  sigset_t ss;

  sigfillset (&ss);

  if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
    {
      puts ("pthread_sigmask failed");
      exit (1);
    }

  pthread_cleanup_push (cleanup, (void *) 42l);

  int err = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  if (err != 0)
    {
      printf ("setcanceltype failed: %s\n", strerror (err));
      exit (1);
    }
  /* The following code is not standard compliant: the mutex functions
     must not be called with asynchronous cancellation enabled.  */

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

  err = pthread_mutex_lock (&m1);
  if (err != 0)
    {
      printf ("child: 1st mutex_lock failed: %s\n", strerror (err));
      exit (1);
    }

  /* We should never come here.  */

  pthread_cleanup_pop (0);

  return NULL;
}

Here is the call graph for this function:


Variable Documentation

int cntr [static]

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

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

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