(atomic_add): Don't allow address register for operand 0.
[glibc.git] / linuxthreads / tst-context.c
blobc72b2ac101676d8ca7fa1c872f2b467b108f4482
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 static volatile int global;
61 int
62 main (void)
64 #ifndef FLOATING_STACKS
65 puts ("not supported");
66 return 0;
67 #else
68 int n;
69 pthread_t th[N];
70 ucontext_t mctx;
72 puts ("making contexts");
73 if (getcontext (&mctx) != 0)
75 if (errno == ENOSYS)
77 puts ("context handling not supported");
78 exit (0);
81 printf ("%s: getcontext: %m\n", __FUNCTION__);
82 exit (1);
85 /* Play some tricks with this context. */
86 if (++global == 1)
87 if (setcontext (&mctx) != 0)
89 printf ("%s: setcontext: %m\n", __FUNCTION__);
90 exit (1);
92 if (global != 2)
94 printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
95 exit (1);
98 for (n = 0; n < N; ++n)
99 if (pthread_create (&th[n], NULL, threadfct, (void *) n) != 0)
100 error (EXIT_FAILURE, errno, "cannot create all threads");
102 for (n = 0; n < N; ++n)
103 pthread_join (th[n], NULL);
105 return failures;
106 #endif