1 /* $NetBSD: t_swapcontext.c,v 1.2 2014/08/25 16:31:15 bouyer Exp $ */
4 * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
40 #define STACKSIZE 65536
42 char stack
[STACKSIZE
];
54 * If the test fails, we are very likely to crash
55 * without the opportunity to report
57 nself
= (void *)pthread_self();
58 printf("after swapcontext self = %p\n", nself
);
60 ATF_REQUIRE_EQ(oself
, nself
);
61 printf("Test succeeded\n");
63 ATF_REQUIRE(swapcontext(&nctx
, &octx
));
73 nctx
.uc_stack
.ss_sp
= stack
;
74 nctx
.uc_stack
.ss_size
= sizeof(stack
);
76 makecontext(&nctx
, (void *)*swapfunc
, 0);
78 oself
= (void *)pthread_self();
79 printf("before swapcontext self = %p\n", oself
);
80 PTHREAD_REQUIRE(swapcontext(&octx
, &nctx
));
88 ATF_TC_HEAD(swapcontext1
, tc
)
90 atf_tc_set_md_var(tc
, "descr", "Testing if swapcontext() "
91 "alters pthread_self()");
93 ATF_TC_BODY(swapcontext1
, tc
)
97 oself
= (void *)&val1
;
98 nself
= (void *)&val2
;
100 printf("Testing if swapcontext() alters pthread_self()\n");
102 PTHREAD_REQUIRE(getcontext(&nctx
));
103 PTHREAD_REQUIRE(pthread_create(&thread
, NULL
, threadfunc
, NULL
));
104 PTHREAD_REQUIRE(pthread_join(thread
, NULL
));
109 ATF_TP_ADD_TC(tp
, swapcontext1
);
111 return atf_no_error();