xref: /freebsd/tools/test/stress2/misc/pthread9.sh (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Thread suspend deadlock seen:
30# https://people.freebsd.org/~pho/stress/log/pthread9.txt
31
32# Test scenario by Conrad Meyer.
33# Fixed by r283320.
34
35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36
37. ../default.cfg
38
39here=`pwd`
40cd /tmp
41sed '1,/^EOF/d' < $here/$0 > pthread9.c
42mycc -o pthread9 -Wall -Wextra -O2 pthread9.c -lpthread || exit 1
43rm -f pthread9.c
44
45status=0
46if ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1; then
47	mount -t nfs -o nfsv3,tcp,nolockd,retrycnt=3,intr $nfs_export \
48	    $mntpoint || exit 1
49	sleep .5
50	echo "Expect core dumps"
51	(cd $mntpoint; /tmp/pthread9) &
52	sleep 200
53	if pgrep -q pthread9; then
54		echo FAIL
55		procstat -k `pgrep pthread9 | grep -v $!`
56		status=1
57	fi
58	rm -f $mntpoint/pthread9.core
59	umount -f $mntpoint
60	wait
61fi
62
63rm -f /tmp/pthread9 /tmp/pthread9.core
64exit $status
65EOF
66#include <sys/types.h>
67
68#include <machine/atomic.h>
69
70#include <err.h>
71#include <errno.h>
72#include <fcntl.h>
73#include <pthread.h>
74#ifdef __FreeBSD__
75#include <pthread_np.h>
76#define	__NP__
77#endif
78#include <stdio.h>
79#include <stdlib.h>
80#include <time.h>
81#include <sys/wait.h>
82#include <unistd.h>
83
84#define LOOPS 50
85#define RUNTIME 180
86
87volatile u_int go;
88int fd;
89char file[] = "pthread9.file";
90
91static void *
92t1(void *data __unused)
93{
94	int i;
95
96#ifdef __NP__
97	pthread_set_name_np(pthread_self(), __func__);
98#endif
99	while (go == 0)
100		pthread_yield();
101
102	atomic_add_int(&go, 1);
103	for (i = 0; i < 100; i++)
104		if (ftruncate(fd, 0) == -1)
105			err(1, "truncate");
106
107	return (NULL);
108}
109
110static void *
111t2(void *data __unused)
112{
113	int i;
114
115#ifdef __NP__
116	pthread_set_name_np(pthread_self(), __func__);
117#endif
118	while (go == 0)
119		pthread_yield();
120
121	atomic_add_int(&go, 1);
122	for (i = 0; i < 100; i++)
123		if (ftruncate(fd, 0) == -1)
124			err(1, "truncate");
125
126	return (NULL);
127}
128
129static void *
130t3(void *data __unused)
131{
132
133#ifdef __NP__
134	pthread_set_name_np(pthread_self(), __func__);
135#endif
136	while (go != 3)
137		pthread_yield();
138	abort();
139
140	return (NULL);
141}
142
143int
144test(void)
145{
146	pthread_t tid[3];
147	int i, rc;
148
149	go = 0;
150	if ((rc = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
151		errc(1, rc, "pthread_create");
152	if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
153		errc(1, rc, "pthread_create");
154	if ((rc = pthread_create(&tid[2], NULL, t3, NULL)) != 0)
155		errc(1, rc, "pthread_create");
156	usleep(200);
157	atomic_add_int(&go, 1);
158
159	for (i = 0; i < 3; i++) {
160		if ((rc = pthread_join(tid[i], NULL)) != 0)
161			errc(1, rc, "pthread_join");
162	}
163
164	_exit(0);
165}
166
167int
168main(void)
169{
170	time_t start;
171
172	if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
173		err(1, "open(%s)", file);
174
175	start = time(NULL);
176	while (time(NULL) - start < RUNTIME) {
177		if (fork() == 0)
178			test();
179		wait(NULL);
180	}
181	close(fd);
182	unlink(file);
183
184	return (0);
185}
186