Back to index

glibc  2.9
Classes | Defines | Functions | Variables
tst-context1.c File Reference
#include <errno.h>
#include <error.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
#include "../test-skeleton.c"

Go to the source code of this file.

Classes

struct  tst_context_t

Defines

#define N   4
#define GUARD_PATTERN   0xdeadbeaf
#define TEST_FUNCTION   do_test ()

Functions

static void fct (long int n)
static void * tf (void *arg)
static int do_test (void)

Variables

static char stacks [N][2 *PTHREAD_STACK_MIN]
static tst_context_t ctx [N][2]
static volatile int failures
static volatile int global

Class Documentation

struct tst_context_t

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

Collaboration diagram for tst_context_t:
Class Members
unsigned long guard
ucontext_t uctx

Define Documentation

#define GUARD_PATTERN   0xdeadbeaf

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

#define N   4

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

#define TEST_FUNCTION   do_test ()

Definition at line 205 of file tst-context1.c.


Function Documentation

static int do_test ( void  ) [static]

Definition at line 135 of file tst-context1.c.

{
  int n;
  pthread_t th[N];
  ucontext_t mctx;

  puts ("making contexts");
  if (getcontext (&mctx) != 0)
    {
      if (errno == ENOSYS)
       {
         puts ("context handling not supported");
         exit (0);
       }

      printf ("%s: getcontext: %m\n", __FUNCTION__);
      exit (1);
    }

  /* Play some tricks with this context.  */
  if (++global == 1)
    if (setcontext (&mctx) != 0)
      {
       puts ("setcontext failed");
       exit (1);
      }
  if (global != 2)
    {
      puts ("global not incremented twice");
      exit (1);
    }
  puts ("global OK");

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

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

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

  for (n = 0; n < N; ++n)
    if (pthread_join (th[n], NULL) != 0)
      {
       puts ("join failed");
       exit (1);
      }

  return failures;
}

Here is the call graph for this function:

static void fct ( long int  n) [static]

Definition at line 46 of file tst-context1.c.

{
  char on_stack[1];

  /* Just to use the thread local descriptor.  */
  printf ("%ld: in %s now, on_stack = %p\n", n, __FUNCTION__, on_stack);
  errno = 0;

  if (ctx[n][1].uctx.uc_link != &ctx[n][0].uctx)
    {
      printf ("context[%ld][1] uc_link damaged, = %p\n", n,
             ctx[n][1].uctx.uc_link);
      exit (1);
    }

  if ((ctx[n][0].guard[0] != GUARD_PATTERN)
  ||  (ctx[n][0].guard[1] != GUARD_PATTERN)
  ||  (ctx[n][0].guard[2] != GUARD_PATTERN))
    {
      printf ("%ld: %s context[0] overflow detected!\n", n, __FUNCTION__);
      ++failures;
    }

  if ((ctx[n][1].guard[0] != GUARD_PATTERN)
  ||  (ctx[n][1].guard[1] != GUARD_PATTERN)
  ||  (ctx[n][1].guard[2] != GUARD_PATTERN))
    {
      printf ("%ld: %s context[1] overflow detected!\n", n, __FUNCTION__);
      ++failures;
    }

  if (n < 0 || n >= N)
    {
      printf ("%ld out of range\n", n);
      exit (1);
    }

  if (on_stack < stacks[n] || on_stack >= stacks[n] + sizeof (stacks[0]))
    {
      printf ("%ld: on_stack not on appropriate stack\n", n);
      exit (1);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

{
  int n = (int) (long int) arg;

  ctx[n][0].guard[0] = GUARD_PATTERN;
  ctx[n][0].guard[1] = GUARD_PATTERN;
  ctx[n][0].guard[2] = GUARD_PATTERN;

  ctx[n][1].guard[0] = GUARD_PATTERN;
  ctx[n][1].guard[1] = GUARD_PATTERN;
  ctx[n][1].guard[2] = GUARD_PATTERN;

  if (getcontext (&ctx[n][1].uctx) != 0)
    {
      printf ("%d: cannot get context: %m\n", n);
      exit (1);
    }

  printf ("%d: %s: before makecontext\n", n, __FUNCTION__);

  ctx[n][1].uctx.uc_stack.ss_sp = stacks[n];
  ctx[n][1].uctx.uc_stack.ss_size = sizeof (stacks[n]);
  ctx[n][1].uctx.uc_link = &ctx[n][0].uctx;
  makecontext (&ctx[n][1].uctx, (void (*) (void)) fct, 1, (long int) n);

  printf ("%d: %s: before swapcontext\n", n, __FUNCTION__);

  if (swapcontext (&ctx[n][0].uctx, &ctx[n][1].uctx) != 0)
    {
      ++failures;
      printf ("%d: %s: swapcontext failed\n", n, __FUNCTION__);
    }
  else
    printf ("%d: back in %s\n", n, __FUNCTION__);

  return NULL;
}

Here is the call graph for this function:


Variable Documentation

tst_context_t ctx[N][2] [static]

Definition at line 41 of file tst-context1.c.

volatile int failures [static]

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

volatile int global [static]

Definition at line 131 of file tst-context1.c.

char stacks[N][2 *PTHREAD_STACK_MIN] [static]

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