xref: /freebsd/tools/test/stress2/misc/pthread7.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2015 EMC Corp.
5*8a272653SPeter Holm# All rights reserved.
6*8a272653SPeter Holm#
7*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
8*8a272653SPeter Holm# modification, are permitted provided that the following conditions
9*8a272653SPeter Holm# are met:
10*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
11*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
12*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
13*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
14*8a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
15*8a272653SPeter Holm#
16*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8a272653SPeter Holm# SUCH DAMAGE.
27*8a272653SPeter Holm#
28*8a272653SPeter Holm
29*8a272653SPeter Holm# PTHREAD_PRIO_INHERIT version of pthread2.sh
30*8a272653SPeter Holm
31*8a272653SPeter Holm. ../default.cfg
32*8a272653SPeter Holm
33*8a272653SPeter Holmexport LANG=C
34*8a272653SPeter Holmhere=`pwd`
35*8a272653SPeter Holmcd /tmp
36*8a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > pthread7.c
37*8a272653SPeter Holmmycc -o pthread7 -Wall -Wextra -O2 -g pthread7.c -lpthread || exit 1
38*8a272653SPeter Holmrm -f pthread7.c
39*8a272653SPeter Holm
40*8a272653SPeter Holmfor i in `jot 5`; do
41*8a272653SPeter Holm	for i in `jot 8`; do
42*8a272653SPeter Holm		/tmp/pthread7 &
43*8a272653SPeter Holm	done
44*8a272653SPeter Holm	wait
45*8a272653SPeter Holmdone
46*8a272653SPeter Holmrm -f /tmp/pthread7
47*8a272653SPeter Holmexit 0
48*8a272653SPeter HolmEOF
49*8a272653SPeter Holm/*
50*8a272653SPeter Holm * Threaded producer-consumer test.
51*8a272653SPeter Holm * Loosly based on work by
52*8a272653SPeter Holm * Andrey Zonov (c) 2012
53*8a272653SPeter Holm */
54*8a272653SPeter Holm
55*8a272653SPeter Holm#include <sys/types.h>
56*8a272653SPeter Holm#include <sys/stat.h>
57*8a272653SPeter Holm#include <sys/time.h>
58*8a272653SPeter Holm#include <sys/queue.h>
59*8a272653SPeter Holm#include <err.h>
60*8a272653SPeter Holm#include <errno.h>
61*8a272653SPeter Holm#include <fcntl.h>
62*8a272653SPeter Holm#include <pthread.h>
63*8a272653SPeter Holm#ifdef __FreeBSD__
64*8a272653SPeter Holm#include <pthread_np.h>
65*8a272653SPeter Holm#define	__NP__
66*8a272653SPeter Holm#endif
67*8a272653SPeter Holm#include <sched.h>
68*8a272653SPeter Holm#include <signal.h>
69*8a272653SPeter Holm#include <stdio.h>
70*8a272653SPeter Holm#include <stdlib.h>
71*8a272653SPeter Holm#include <string.h>
72*8a272653SPeter Holm#include <time.h>
73*8a272653SPeter Holm#include <unistd.h>
74*8a272653SPeter Holm
75*8a272653SPeter Holm#define	LOCK(x)		plock(&x.mtx)
76*8a272653SPeter Holm#define	UNLOCK(x)	punlock(&x.mtx)
77*8a272653SPeter Holm#define	SIGNAL(x)	psig(&x.wait)
78*8a272653SPeter Holm#define	WAIT(x)		pwait(&x.wait, &x.mtx)
79*8a272653SPeter Holm
80*8a272653SPeter Holmlong ncreate, nrename, nunlink;
81*8a272653SPeter Holmint max;
82*8a272653SPeter Holmchar *dirname1;
83*8a272653SPeter Holmchar *dirname2;
84*8a272653SPeter Holm
85*8a272653SPeter Holmstruct file {
86*8a272653SPeter Holm	char *name;
87*8a272653SPeter Holm	STAILQ_ENTRY(file) next;
88*8a272653SPeter Holm};
89*8a272653SPeter Holm
90*8a272653SPeter Holmstruct files {
91*8a272653SPeter Holm	pthread_mutex_t mtx;
92*8a272653SPeter Holm	pthread_cond_t wait;
93*8a272653SPeter Holm	STAILQ_HEAD(, file) list;
94*8a272653SPeter Holm};
95*8a272653SPeter Holm
96*8a272653SPeter Holmstatic struct files newfiles;
97*8a272653SPeter Holmstatic struct files renamedfiles;
98*8a272653SPeter Holm
99*8a272653SPeter Holm#define MAXQ 100000		/* Max create queue length */
100*8a272653SPeter Holm#define MESSAGES 10000000;
101*8a272653SPeter Holm
102*8a272653SPeter Holmstatic void
103*8a272653SPeter Holmhand(int i __unused) {	/* handler */
104*8a272653SPeter Holm	fprintf(stderr, "max = %d, ncreate = %ld, nrename = %ld, nunlink = %ld\n",
105*8a272653SPeter Holm			max, ncreate, nrename, nunlink);
106*8a272653SPeter Holm}
107*8a272653SPeter Holm
108*8a272653SPeter Holmstatic void
109*8a272653SPeter Holmahand(int i __unused) {	/* handler */
110*8a272653SPeter Holm	fprintf(stderr, "FAIL\n");
111*8a272653SPeter Holm	hand(0);
112*8a272653SPeter Holm	_exit(0);
113*8a272653SPeter Holm}
114*8a272653SPeter Holm
115*8a272653SPeter Holmvoid
116*8a272653SPeter Holmplock(pthread_mutex_t *l)
117*8a272653SPeter Holm{
118*8a272653SPeter Holm	int rc;
119*8a272653SPeter Holm
120*8a272653SPeter Holm	if ((rc = pthread_mutex_lock(l)) != 0)
121*8a272653SPeter Holm		errc(1, rc, "pthread_mutex_lock");
122*8a272653SPeter Holm}
123*8a272653SPeter Holm
124*8a272653SPeter Holmvoid
125*8a272653SPeter Holmpunlock(pthread_mutex_t *l)
126*8a272653SPeter Holm{
127*8a272653SPeter Holm	int rc;
128*8a272653SPeter Holm
129*8a272653SPeter Holm	if ((rc = pthread_mutex_unlock(l)) != 0)
130*8a272653SPeter Holm		errc(1, rc, "pthread_mutex_unlock");
131*8a272653SPeter Holm}
132*8a272653SPeter Holm
133*8a272653SPeter Holmvoid
134*8a272653SPeter Holmpsig(pthread_cond_t *c)
135*8a272653SPeter Holm{
136*8a272653SPeter Holm	int rc;
137*8a272653SPeter Holm
138*8a272653SPeter Holm	if ((rc = pthread_cond_signal(c)) != 0)
139*8a272653SPeter Holm		errc(1, rc, "pthread_cond_signal");
140*8a272653SPeter Holm}
141*8a272653SPeter Holm
142*8a272653SPeter Holmvoid
143*8a272653SPeter Holmpwait(pthread_cond_t *c, pthread_mutex_t *l)
144*8a272653SPeter Holm{
145*8a272653SPeter Holm	int rc;
146*8a272653SPeter Holm
147*8a272653SPeter Holm	if ((rc = pthread_cond_wait(c, l)) != 0)
148*8a272653SPeter Holm		errc(1, rc, "pthread_cond_wait");
149*8a272653SPeter Holm}
150*8a272653SPeter Holm
151*8a272653SPeter Holmvoid *
152*8a272653SPeter Holmloop_create(void *arg __unused)
153*8a272653SPeter Holm{
154*8a272653SPeter Holm	int i;
155*8a272653SPeter Holm	struct file *file;
156*8a272653SPeter Holm
157*8a272653SPeter Holm#ifdef __NP__
158*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
159*8a272653SPeter Holm#endif
160*8a272653SPeter Holm
161*8a272653SPeter Holm	for (i = 0; i < max; i++) {
162*8a272653SPeter Holm		file = malloc(sizeof(*file));
163*8a272653SPeter Holm		asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
164*8a272653SPeter Holm		LOCK(newfiles);
165*8a272653SPeter Holm		STAILQ_INSERT_TAIL(&newfiles.list, file, next);
166*8a272653SPeter Holm		ncreate++;
167*8a272653SPeter Holm		UNLOCK(newfiles);
168*8a272653SPeter Holm		SIGNAL(newfiles);
169*8a272653SPeter Holm		if (ncreate - nrename > MAXQ)
170*8a272653SPeter Holm			usleep(400);
171*8a272653SPeter Holm	}
172*8a272653SPeter Holm	return (NULL);
173*8a272653SPeter Holm}
174*8a272653SPeter Holm
175*8a272653SPeter Holmvoid *
176*8a272653SPeter Holmloop_rename(void *arg __unused)
177*8a272653SPeter Holm{
178*8a272653SPeter Holm	char *filename, *newname;
179*8a272653SPeter Holm	struct file *file;
180*8a272653SPeter Holm
181*8a272653SPeter Holm#ifdef	__NP__
182*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
183*8a272653SPeter Holm#endif
184*8a272653SPeter Holm
185*8a272653SPeter Holm	while (nrename < max) {
186*8a272653SPeter Holm		LOCK(newfiles);
187*8a272653SPeter Holm		while (STAILQ_EMPTY(&newfiles.list)) {
188*8a272653SPeter Holm			WAIT(newfiles);
189*8a272653SPeter Holm		}
190*8a272653SPeter Holm		file = STAILQ_FIRST(&newfiles.list);
191*8a272653SPeter Holm		STAILQ_REMOVE_HEAD(&newfiles.list, next);
192*8a272653SPeter Holm		UNLOCK(newfiles);
193*8a272653SPeter Holm		filename = strrchr(file->name, '/');
194*8a272653SPeter Holm		asprintf(&newname, "%s/%s", dirname2, filename);
195*8a272653SPeter Holm		nrename++;
196*8a272653SPeter Holm		free(file->name);
197*8a272653SPeter Holm		file->name = newname;
198*8a272653SPeter Holm		LOCK(renamedfiles);
199*8a272653SPeter Holm		STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
200*8a272653SPeter Holm		UNLOCK(renamedfiles);
201*8a272653SPeter Holm		SIGNAL(renamedfiles);
202*8a272653SPeter Holm	}
203*8a272653SPeter Holm	return (NULL);
204*8a272653SPeter Holm}
205*8a272653SPeter Holm
206*8a272653SPeter Holmvoid *
207*8a272653SPeter Holmloop_unlink(void *arg __unused)
208*8a272653SPeter Holm{
209*8a272653SPeter Holm	struct file *file;
210*8a272653SPeter Holm
211*8a272653SPeter Holm#ifdef	__NP__
212*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
213*8a272653SPeter Holm#endif
214*8a272653SPeter Holm
215*8a272653SPeter Holm	while (nunlink < max) {
216*8a272653SPeter Holm		LOCK(renamedfiles);
217*8a272653SPeter Holm		while (STAILQ_EMPTY(&renamedfiles.list)) {
218*8a272653SPeter Holm			WAIT(renamedfiles);
219*8a272653SPeter Holm		}
220*8a272653SPeter Holm		file = STAILQ_FIRST(&renamedfiles.list);
221*8a272653SPeter Holm		STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
222*8a272653SPeter Holm		nunlink++;
223*8a272653SPeter Holm		UNLOCK(renamedfiles);
224*8a272653SPeter Holm		free(file->name);
225*8a272653SPeter Holm		free(file);
226*8a272653SPeter Holm	}
227*8a272653SPeter Holm	return (NULL);
228*8a272653SPeter Holm}
229*8a272653SPeter Holm
230*8a272653SPeter Holmint
231*8a272653SPeter Holmmain(void)
232*8a272653SPeter Holm{
233*8a272653SPeter Holm	int i;
234*8a272653SPeter Holm	int rc;
235*8a272653SPeter Holm	pthread_t tid[3];
236*8a272653SPeter Holm	pthread_mutexattr_t attr, *pattr = NULL;
237*8a272653SPeter Holm
238*8a272653SPeter Holm	asprintf(&dirname1, "%s.1", "f1");
239*8a272653SPeter Holm	asprintf(&dirname2, "%s.2", "f2");
240*8a272653SPeter Holm	max = MESSAGES;
241*8a272653SPeter Holm
242*8a272653SPeter Holm	STAILQ_INIT(&newfiles.list);
243*8a272653SPeter Holm	STAILQ_INIT(&renamedfiles.list);
244*8a272653SPeter Holm
245*8a272653SPeter Holm	pthread_mutexattr_init (&attr);
246*8a272653SPeter Holm	if ((rc = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT)) != 0)
247*8a272653SPeter Holm		errc(1, rc, "PTHREAD_PRIO_INHERIT");
248*8a272653SPeter Holm	pattr = &attr;
249*8a272653SPeter Holm	if ((rc = pthread_mutex_init(&newfiles.mtx, pattr)) != 0)
250*8a272653SPeter Holm		errc(1, rc, "pthread_mutex_init()");
251*8a272653SPeter Holm	if ((rc = pthread_cond_init(&newfiles.wait, NULL)) != 0)
252*8a272653SPeter Holm		errc(1, rc, "pthread_cond_init()");
253*8a272653SPeter Holm	if ((rc = pthread_mutex_init(&renamedfiles.mtx, NULL)) != 0)
254*8a272653SPeter Holm		errc(1, rc, "pthread_mutex_init()");
255*8a272653SPeter Holm	if ((rc = pthread_cond_init(&renamedfiles.wait, NULL)) != 0)
256*8a272653SPeter Holm		errc(1, rc, "pthread_cond_init()");
257*8a272653SPeter Holm
258*8a272653SPeter Holm	signal(SIGINFO, hand);
259*8a272653SPeter Holm	signal(SIGALRM, ahand);
260*8a272653SPeter Holm	alarm(300);
261*8a272653SPeter Holm	if ((rc = pthread_create(&tid[0], NULL, loop_create, NULL)) != 0)
262*8a272653SPeter Holm		errc(1, rc, "pthread_create()");
263*8a272653SPeter Holm	if ((rc = pthread_create(&tid[1], NULL, loop_rename, NULL)) != 0)
264*8a272653SPeter Holm		errc(1, rc, "pthread_create()");
265*8a272653SPeter Holm	if ((rc = pthread_create(&tid[2], NULL, loop_unlink, NULL)) != 0)
266*8a272653SPeter Holm		errc(1, rc, "pthread_create()");
267*8a272653SPeter Holm
268*8a272653SPeter Holm	for (i = 0; i < 3; i++) {
269*8a272653SPeter Holm		if ((rc = pthread_join(tid[i], NULL)) != 0)
270*8a272653SPeter Holm			errc(1, rc, "pthread_join(%d)", i);
271*8a272653SPeter Holm	}
272*8a272653SPeter Holm
273*8a272653SPeter Holm	if ((rc = pthread_mutex_destroy(&newfiles.mtx)) != 0)
274*8a272653SPeter Holm		errc(1, rc, "pthread_mutex_destroy(newfiles)");
275*8a272653SPeter Holm	if ((rc = pthread_cond_destroy(&newfiles.wait)) != 0)
276*8a272653SPeter Holm		errc(1, rc, "pthread_cond_destroy(newfiles)");
277*8a272653SPeter Holm	if ((rc = pthread_mutex_destroy(&renamedfiles.mtx)) != 0)
278*8a272653SPeter Holm		errc(1, rc, "pthread_mutex_destroy(renamedfiles)");
279*8a272653SPeter Holm	if ((rc = pthread_cond_destroy(&renamedfiles.wait)) != 0)
280*8a272653SPeter Holm		errc(1, rc, "pthread_cond_destroy(renamedfiles)");
281*8a272653SPeter Holm	free(dirname1);
282*8a272653SPeter Holm	free(dirname2);
283*8a272653SPeter Holm
284*8a272653SPeter Holm	return (0);
285*8a272653SPeter Holm}
286