2003-05-16 Roland McGrath <roland@redhat.com>
[glibc.git] / nptl / tst-context1.c
blob0edee00c7a1bc2d42fb9af58fe65510ae1763420
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <limits.h>
23 #include <pthread.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <ucontext.h>
29 #define N 4
31 static char stacks[N][PTHREAD_STACK_MIN];
32 static ucontext_t ctx[N][2];
33 static volatile int failures;
36 static void
37 fct (long int n)
39 /* Just to use the thread local descriptor. */
40 printf ("%ld: in %s now\n", n, __FUNCTION__);
41 errno = 0;
45 static void *
46 tf (void *arg)
48 int n = (int) (long int) arg;
50 if (getcontext (&ctx[n][1]) != 0)
52 printf ("%d: cannot get context: %m\n", n);
53 exit (1);
56 printf ("%d: %s: before makecontext\n", n, __FUNCTION__);
58 ctx[n][1].uc_stack.ss_sp = stacks[n];
59 ctx[n][1].uc_stack.ss_size = PTHREAD_STACK_MIN;
60 ctx[n][1].uc_link = &ctx[n][0];
61 makecontext (&ctx[n][1], (void (*) (void)) fct, 1, (long int) n);
63 printf ("%d: %s: before swapcontext\n", n, __FUNCTION__);
65 if (swapcontext (&ctx[n][0], &ctx[n][1]) != 0)
67 ++failures;
68 printf ("%d: %s: swapcontext failed\n", n, __FUNCTION__);
70 else
71 printf ("%d: back in %s\n", n, __FUNCTION__);
73 return NULL;
77 static volatile int global;
80 static int
81 do_test (void)
83 int n;
84 pthread_t th[N];
85 ucontext_t mctx;
87 puts ("making contexts");
88 if (getcontext (&mctx) != 0)
90 if (errno == ENOSYS)
92 puts ("context handling not supported");
93 exit (0);
96 printf ("%s: getcontext: %m\n", __FUNCTION__);
97 exit (1);
100 /* Play some tricks with this context. */
101 if (++global == 1)
102 if (setcontext (&mctx) != 0)
104 puts ("setcontext failed");
105 exit (1);
107 if (global != 2)
109 puts ("global not incremented twice");
110 exit (1);
112 puts ("global OK");
114 for (n = 0; n < N; ++n)
115 if (pthread_create (&th[n], NULL, tf, (void *) (long int) n) != 0)
117 puts ("create failed");
118 exit (1);
121 for (n = 0; n < N; ++n)
122 if (pthread_join (th[n], NULL) != 0)
124 puts ("join failed");
125 exit (1);
128 return failures;
131 #define TEST_FUNCTION do_test ()
132 #include "../test-skeleton.c"