1 /* Test context switching.
2 * Copyright (C) 2001-2003 Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: test_context.c,v 1.4 2003/02/05 22:13:33 rich Exp $
34 #include "pthr_context.h"
36 #define STACK_SIZE 4096
37 #define MARKER_SIZE 16
39 static unsigned char stack [MARKER_SIZE+STACK_SIZE+MARKER_SIZE];
40 static mctx_t ctx, calling_ctx;
41 static int called = 0;
42 static void fn (void *);
43 static void setjmp_test (void *);
46 #define DATA ((void *) 0x12546731)
53 /* Place magic numbers into the marker areas at each end of the stack, so
54 * we can detect stack overrun.
56 memset (stack, 0xeb, MARKER_SIZE);
57 memset (stack + MARKER_SIZE+STACK_SIZE, 0xeb, MARKER_SIZE);
58 mctx_set (&ctx, fn, DATA, stack + MARKER_SIZE, STACK_SIZE);
59 mctx_switch (&calling_ctx, &ctx);
62 /* Verify the ends of the stack haven't been overrun. */
63 for (i = 0; i < MARKER_SIZE; ++i)
64 assert (stack[i] == 0xeb && stack[i + MARKER_SIZE+STACK_SIZE] == 0xeb);
66 /* Check setjmp/longjmp work on the alternate stack. This was found to be
67 * a problem on Solaris.
69 mctx_set (&ctx, setjmp_test, DATA, stack + MARKER_SIZE, STACK_SIZE);
70 mctx_switch (&calling_ctx, &ctx);
78 assert (data == DATA);
80 mctx_restore (&calling_ctx);
84 static void call_longjmp (void);
87 setjmp_test (void *data)
92 mctx_restore (&calling_ctx);