Back to index

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

Functions

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

Variables

static pthread_barrier_t b
static int cl_called

Define Documentation

#define TEST_FUNCTION   do_test ()

Definition at line 173 of file tst-cancel18.c.


Function Documentation

static void cl ( void *  arg) [static]

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

{
  ++cl_called;
}

Here is the caller graph for this function:

static int do_test ( void  ) [static]

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

{
  if (pthread_barrier_init (&b, NULL, 2) != 0)
    {
      puts ("barrier_init failed");
      return 1;
    }

  pthread_t th;
  if (pthread_create (&th, NULL, tf, NULL) != 0)
    {
      puts ("1st create failed");
      return 1;
    }

  int r = pthread_barrier_wait (&b);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      exit (1);
    }

  struct timespec  ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  while (nanosleep (&ts, &ts) != 0)
    continue;

  puts ("going to cancel in-time");
  if (pthread_cancel (th) != 0)
    {
      puts ("1st cancel failed");
      return 1;
    }

  void *status;
  if (pthread_join (th, &status) != 0)
    {
      puts ("1st join failed");
      return 1;
    }
  if (status != PTHREAD_CANCELED)
    {
      puts ("1st thread not canceled");
      return 1;
    }

  if (cl_called == 0)
    {
      puts ("cleanup handler not called");
      return 1;
    }
  if (cl_called > 1)
    {
      puts ("cleanup handler called more than once");
      return 1;
    }

  puts ("in-time cancellation succeeded");


  cl_called = 0;

  if (pthread_create (&th, NULL, tf, NULL) != 0)
    {
      puts ("2nd create failed");
      return 1;
    }

  puts ("going to cancel early");
  if (pthread_cancel (th) != 0)
    {
      puts ("2nd cancel failed");
      return 1;
    }

  r = pthread_barrier_wait (&b);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      exit (1);
    }

  if (pthread_join (th, &status) != 0)
    {
      puts ("2nd join failed");
      return 1;
    }
  if (status != PTHREAD_CANCELED)
    {
      puts ("2nd thread not canceled");
      return 1;
    }

  if (cl_called == 0)
    {
      printf ("cleanup handler not called\n");
      return 1;
    }
  if (cl_called > 1)
    {
      printf ("cleanup handler called more than once\n");
      return 1;
    }

  puts ("early cancellation succeeded");

  return 0;
}

Here is the call graph for this function:

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

Definition at line 42 of file tst-cancel18.c.

{
  int r = pthread_barrier_wait (&b);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      exit (1);
    }

  pthread_cleanup_push (cl, NULL);

  struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
  TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts));

  pthread_cleanup_pop (0);

  puts ("clock_nanosleep returned");

  exit (1);
}

Here is the call graph for this function:


Variable Documentation

pthread_barrier_t b [static]

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

int cl_called [static]

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