Update.
[glibc.git] / linuxthreads / tst-context.c
blob2938a9f7fae3ec71f2d6a7cffc1504ffddc53a49
1 /* Ack, a hack! We need to get the proper definition, or lack thereof,
2 for FLOATING_STACKS. But when !IS_IN_libpthread, this can get defined
3 incidentally by <tls.h>. So kludge around it. */
5 #define IS_IN_libpthread
6 #include <tls.h>
7 #undef IS_IN_libpthread
8 #undef USE___THREAD
10 #include <errno.h>
11 #include <error.h>
12 #include <pthread.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <ucontext.h>
18 #define N 4
20 #ifdef FLOATING_STACKS
21 static char stacks[N][8192];
22 static ucontext_t ctx[N][2];
23 static volatile int failures;
25 static void
26 fct (long int n)
28 /* Just to use the thread local descriptor. */
29 printf ("%ld: in %s now\n", n, __FUNCTION__);
30 errno = 0;
33 static void *
34 threadfct (void *arg)
36 int n = (int) (long int) arg;
38 if (getcontext (&ctx[n][1]) != 0)
40 printf ("%d: cannot get context: %m\n", n);
41 exit (1);
44 printf ("%d: %s: before makecontext\n", n, __FUNCTION__);
46 ctx[n][1].uc_stack.ss_sp = stacks[n];
47 ctx[n][1].uc_stack.ss_size = 8192;
48 ctx[n][1].uc_link = &ctx[n][0];
49 makecontext (&ctx[n][1], (void (*) (void)) fct, 1, (long int) n);
51 printf ("%d: %s: before swapcontext\n", n, __FUNCTION__);
53 if (swapcontext (&ctx[n][0], &ctx[n][1]) != 0)
55 ++failures;
56 printf ("%d: %s: swapcontext failed\n", n, __FUNCTION__);
58 else
59 printf ("%d: back in %s\n", n, __FUNCTION__);
61 return NULL;
63 #endif
66 #ifdef FLOATING_STACKS
67 static volatile int global;
68 #endif
70 int
71 main (void)
73 #ifndef FLOATING_STACKS
74 puts ("not supported");
75 return 0;
76 #else
77 int n;
78 pthread_t th[N];
79 ucontext_t mctx;
81 puts ("making contexts");
82 if (getcontext (&mctx) != 0)
84 if (errno == ENOSYS)
86 puts ("context handling not supported");
87 exit (0);
90 printf ("%s: getcontext: %m\n", __FUNCTION__);
91 exit (1);
94 /* Play some tricks with this context. */
95 if (++global == 1)
96 if (setcontext (&mctx) != 0)
98 printf ("%s: setcontext: %m\n", __FUNCTION__);
99 exit (1);
101 if (global != 2)
103 printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
104 exit (1);
107 for (n = 0; n < N; ++n)
108 if (pthread_create (&th[n], NULL, threadfct, (void *) (long int) n) != 0)
109 error (EXIT_FAILURE, errno, "cannot create all threads");
111 for (n = 0; n < N; ++n)
112 pthread_join (th[n], NULL);
114 return failures;
115 #endif