1/*	$OpenBSD: pthread_join.c,v 1.5 2018/04/27 06:47:34 guenther 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/* ==== test_pthread_join.c =================================================
36 * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
37 *
38 * Description : Test pthread_join(). Run this after test_create()
39 *
40 *  1.23 94/05/04 proven
41 *      -Started coding this file.
42 */
43
44#include <err.h>
45#include <pthread.h>
46#include <signal.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <unistd.h>
50#include "test.h"
51
52static void
53handler(int sig)
54{
55}
56
57/* This thread yields so the creator has a live thread to wait on */
58static void *
59new_thread_1(void * new_buf)
60{
61	int i;
62
63	snprintf((char *)new_buf, 512, "New thread %%d stack at %p\n", &i);
64	pthread_yield();	/* (ensure parent can wait on live thread) */
65	sleep(2);
66	return(new_buf);
67	PANIC("return");
68}
69
70/* This thread doesn't yield so the creator has a dead thread to wait on */
71static void *
72new_thread_2(void * new_buf)
73{
74	int i;
75
76	snprintf((char *)new_buf, 512, "New thread %%d stack at %p\n", &i);
77	return(new_buf);
78	PANIC("return");
79}
80
81int
82main(int argc, char *argv[])
83{
84	struct sigaction sa;
85	sigset_t mask;
86	char buf[256], *status;
87	pthread_t thread;
88	int debug = 1;
89	int i = 0;
90
91	sa.sa_handler = &handler;
92	sigemptyset(&sa.sa_mask);
93	sa.sa_flags = 0;
94	if (sigaction(SIGALRM, &sa, NULL))
95		err(1, "sigaction");
96	sigemptyset(&mask);
97	sigaddset(&mask, SIGALRM);
98
99	if (debug)
100		printf("Original thread stack at %p\n", &i);
101
102	if (sigprocmask(SIG_BLOCK, &mask, NULL))
103		err(1, "sigprocmask");
104	CHECKr(pthread_create(&thread, NULL, new_thread_1, (void *)buf));
105	if (sigprocmask(SIG_UNBLOCK, &mask, NULL))
106		err(1, "sigprocmask");
107	alarm(1);
108	CHECKr(pthread_join(thread, (void **)(&status)));
109	if (debug)
110		printf(status, ++i);
111
112	/* Now have the created thread finishing before the join. */
113	CHECKr(pthread_create(&thread, NULL, new_thread_2, (void *)buf));
114	pthread_yield();
115	sleep(1); /* (ensure thread is dead) */
116	CHECKr(pthread_join(thread, (void **)(&status)));
117
118	if (debug)
119		printf(status, ++i);
120
121	SUCCEED;
122}
123
124