~ubuntu-branches/ubuntu/utopic/eglibc/utopic

« back to all changes in this revision

Viewing changes to manual/examples/swapcontext.c

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2012-10-26 05:14:58 UTC
  • mfrom: (1.5.1) (4.4.22 experimental)
  • Revision ID: package-import@ubuntu.com-20121026051458-oryotr4i03ob5pab
Tags: 2.16-0ubuntu1
* Merge with unreleased 2.16 in Debian experimental, remaining changes:
  - Drop the Breaks line from libc6, which refers to a Debian transition
  - Remove the libc6 recommends on libc6-i686, which we don't build
  - Enable libc6{,-dev}-armel on armhf and libc6{-dev}-armhf on armel
  - Ship update-locale and validlocale in /usr/sbin in libc-bin
  - Don't build locales or locales-all in Ubuntu, we rely on langpacks
  - Heavily mangle the way we do service restarting on major upgrades
  - Use different MIN_KERNEL_SUPPORTED versions than Debian, due to
    buildd needs.  This should be universally bumped to 3.2.0 once all
    our buildds (including the PPA guests) are running precise kernels
  - Build i386 variants as -march=i686, build amd64 with -O3, and build
    ppc64 variants (both 64-bit and 32-bit) with -O3 -fno-tree-vectorize
  - Re-enable unsubmitted-ldconfig-cache-abi.diff and rebuild the cache
    on upgrades from previous versions that used a different constant
  - debian/patches/any/local-CVE-2012-3406.diff: switch to malloc when
    array grows too large to handle via alloca extension (CVE-2012-3406)
  - Build generic i386/i686 flavour with -mno-tls-direct-seg-refs
* Changes added/dropped with this merge while reducing our delta:
  - Stop building glibc docs from the eglibc source, and instead make
    the glibc-docs stub have a hard dependency on glibc-doc-reference
  - Remove outdated conflicts against ancient versions of ia32-libs
  - Drop the tzdata dependency from libc6, it's in required and minimal
  - Use gcc-4.7/g++-4.7 by default on all our supported architectures
  - Save our historical changelog as changelog.ubuntu in the source
  - Drop nscd's libaudit build-dep for now, as libaudit is in universe
  - Drop the unnecessary Breaks from libc6 to locales and locales-all
  - Ship xen's ld.so.conf.d snippet as /etc/ld.so.conf.d/libc6-xen.conf
* Disable hard failures on the test suite for the first upload to raring

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <signal.h>
2
 
#include <stdio.h>
3
 
#include <stdlib.h>
4
 
#include <ucontext.h>
5
 
#include <sys/time.h>
6
 
 
7
 
/* Set by the signal handler.  */
8
 
static volatile int expired;
9
 
 
10
 
/* The contexts.  */
11
 
static ucontext_t uc[3];
12
 
 
13
 
/* We do only a certain number of switches.  */
14
 
static int switches;
15
 
 
16
 
 
17
 
/* This is the function doing the work.  It is just a
18
 
   skeleton, real code has to be filled in.  */
19
 
static void
20
 
f (int n)
21
 
{
22
 
  int m = 0;
23
 
  while (1)
24
 
    {
25
 
      /* This is where the work would be done.  */
26
 
      if (++m % 100 == 0)
27
 
        {
28
 
          putchar ('.');
29
 
          fflush (stdout);
30
 
        }
31
 
 
32
 
      /* Regularly the @var{expire} variable must be checked.  */
33
 
      if (expired)
34
 
        {
35
 
          /* We do not want the program to run forever.  */
36
 
          if (++switches == 20)
37
 
            return;
38
 
 
39
 
          printf ("\nswitching from %d to %d\n", n, 3 - n);
40
 
          expired = 0;
41
 
          /* Switch to the other context, saving the current one.  */
42
 
          swapcontext (&uc[n], &uc[3 - n]);
43
 
        }
44
 
    }
45
 
}
46
 
 
47
 
/* This is the signal handler which simply set the variable.  */
48
 
void
49
 
handler (int signal)
50
 
{
51
 
  expired = 1;
52
 
}
53
 
 
54
 
 
55
 
int
56
 
main (void)
57
 
{
58
 
  struct sigaction sa;
59
 
  struct itimerval it;
60
 
  char st1[8192];
61
 
  char st2[8192];
62
 
 
63
 
  /* Initialize the data structures for the interval timer.  */
64
 
  sa.sa_flags = SA_RESTART;
65
 
  sigfillset (&sa.sa_mask);
66
 
  sa.sa_handler = handler;
67
 
  it.it_interval.tv_sec = 0;
68
 
  it.it_interval.tv_usec = 1;
69
 
  it.it_value = it.it_interval;
70
 
 
71
 
  /* Install the timer and get the context we can manipulate.  */
72
 
  if (sigaction (SIGPROF, &sa, NULL) < 0
73
 
      || setitimer (ITIMER_PROF, &it, NULL) < 0
74
 
      || getcontext (&uc[1]) == -1
75
 
      || getcontext (&uc[2]) == -1)
76
 
    abort ();
77
 
 
78
 
  /* Create a context with a separate stack which causes the
79
 
     function @code{f} to be call with the parameter @code{1}.
80
 
     Note that the @code{uc_link} points to the main context
81
 
     which will cause the program to terminate once the function
82
 
     return.  */
83
 
  uc[1].uc_link = &uc[0];
84
 
  uc[1].uc_stack.ss_sp = st1;
85
 
  uc[1].uc_stack.ss_size = sizeof st1;
86
 
  makecontext (&uc[1], (void (*) (void)) f, 1, 1);
87
 
 
88
 
  /* Similarly, but @code{2} is passed as the parameter to @code{f}.  */
89
 
  uc[2].uc_link = &uc[0];
90
 
  uc[2].uc_stack.ss_sp = st2;
91
 
  uc[2].uc_stack.ss_size = sizeof st2;
92
 
  makecontext (&uc[2], (void (*) (void)) f, 1, 2);
93
 
 
94
 
  /* Start running.  */
95
 
  swapcontext (&uc[0], &uc[1]);
96
 
  putchar ('\n');
97
 
 
98
 
  return 0;
99
 
}