xref: /openbsd-src/regress/lib/libpthread/setjmp/setjmp.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: setjmp.c,v 1.3 2003/07/31 21:48:06 deraadt Exp $	*/
2 /*
3  * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4  * proven@mit.edu All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Chris Provenzano,
17  *	the University of California, Berkeley, and contributors.
18  * 4. Neither the name of Chris Provenzano, the University, nor the names of
19  *   contributors may be used to endorse or promote products derived
20  *   from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <setjmp.h>
36 #include <stdlib.h>
37 #include "test.h"
38 
39 int reached;
40 
41 static void *
_jump(void * arg)42 _jump(void *arg)
43 {
44 	jmp_buf foo;
45 
46 	reached = 0;
47 	if (_setjmp(foo)) {
48 		ASSERT(reached);
49 		return NULL;
50 	}
51 	reached = 1;
52 	_longjmp(foo, 1);
53 	PANIC("_longjmp");
54 }
55 
56 static void *
jump(void * arg)57 jump(void *arg)
58 {
59 	jmp_buf foo;
60 
61 	reached = 0;
62 	if (setjmp(foo)) {
63 		ASSERT(reached);
64 		return NULL;
65 	}
66 	reached = 1;
67 	longjmp(foo, 1);
68 	PANIC("longjmp");
69 }
70 
71 int
main(int argc,char * argv[])72 main(int argc, char *argv[])
73 {
74 	pthread_t child;
75 	void *res;
76 
77 	printf("jumping in main thread\n");
78 	(void)jump(NULL);
79 	printf("_jumping in main thread\n");
80 	(void)_jump(NULL);
81 
82 	printf("jumping in child thread\n");
83 	CHECKr(pthread_create(&child, NULL, jump, NULL));
84 	CHECKr(pthread_join(child, &res));
85 
86 	printf("_jumping in child thread\n");
87 	CHECKr(pthread_create(&child, NULL, _jump, NULL));
88 	CHECKr(pthread_join(child, &res));
89 
90 	SUCCEED;
91 }
92