Update.
[glibc.git] / linuxthreads / tst-context.c
blob5c3272a4fbf1b1adf598148b15296de130cded06
1 #include <errno.h>
2 #include <error.h>
3 #include <pthread.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <ucontext.h>
8 #include "pt-machine.h"
11 #define N 4
13 #ifdef FLOATING_STACKS
14 static char stacks[N][8192];
15 static ucontext_t ctx[N][2];
16 static volatile int failures;
18 static void
19 fct (long int n)
21 /* Just to use the thread local descriptor. */
22 printf ("%ld: in %s now\n", n, __FUNCTION__);
23 errno = 0;
26 static void *
27 threadfct (void *arg)
29 int n = (int) (long int) arg;
31 if (getcontext (&ctx[n][1]) != 0)
33 printf ("%d: cannot get context: %m\n", n);
34 exit (1);
37 printf ("%d: %s: before makecontext\n", n, __FUNCTION__);
39 ctx[n][1].uc_stack.ss_sp = stacks[n];
40 ctx[n][1].uc_stack.ss_size = 8192;
41 ctx[n][1].uc_link = &ctx[n][0];
42 makecontext (&ctx[n][1], (void (*) (void)) fct, 1, (long int) n);
44 printf ("%d: %s: before swapcontext\n", n, __FUNCTION__);
46 if (swapcontext (&ctx[n][0], &ctx[n][1]) != 0)
48 ++failures;
49 printf ("%d: %s: swapcontext failed\n", n, __FUNCTION__);
51 else
52 printf ("%d: back in %s\n", n, __FUNCTION__);
54 return NULL;
56 #endif
59 #ifdef FLOATING_STACKS
60 static volatile int global;
61 #endif
63 int
64 main (void)
66 #ifndef FLOATING_STACKS
67 puts ("not supported");
68 return 0;
69 #else
70 int n;
71 pthread_t th[N];
72 ucontext_t mctx;
74 puts ("making contexts");
75 if (getcontext (&mctx) != 0)
77 if (errno == ENOSYS)
79 puts ("context handling not supported");
80 exit (0);
83 printf ("%s: getcontext: %m\n", __FUNCTION__);
84 exit (1);
87 /* Play some tricks with this context. */
88 if (++global == 1)
89 if (setcontext (&mctx) != 0)
91 printf ("%s: setcontext: %m\n", __FUNCTION__);
92 exit (1);
94 if (global != 2)
96 printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
97 exit (1);
100 for (n = 0; n < N; ++n)
101 if (pthread_create (&th[n], NULL, threadfct, (void *) (long int) n) != 0)
102 error (EXIT_FAILURE, errno, "cannot create all threads");
104 for (n = 0; n < N; ++n)
105 pthread_join (th[n], NULL);
107 return failures;
108 #endif