1#!/bin/sh
2
3#
4# Copyright (c) 2014 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# Stress shchan allocations.
30
31. ../default.cfg
32[ `swapinfo | wc -l` -eq 1 ] && exit 0 # kstack allocation failed
33
34here=`pwd`
35cd /tmp
36sed '1,/^EOF/d' < $here/$0 > pthread5.c
37mycc -o pthread5 -Wall -Wextra -O2 pthread5.c -lpthread || exit 1
38rm -f pthread5.c
39
40/tmp/pthread5
41
42rm -f /tmp/pthread5
43exit 0
44EOF
45#include <sys/types.h>
46#include <err.h>
47#include <errno.h>
48#include <pthread.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <sys/event.h>
52#include <sys/time.h>
53#include <sys/wait.h>
54#include <unistd.h>
55
56#define ITER 2
57#define PARALLEL 1000
58#define THREADS 100
59
60pthread_cond_t cond  = PTHREAD_COND_INITIALIZER;
61pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
62
63static void *
64nicethreads(void *data __unused)
65{
66	struct timespec ts;
67	struct timeval tp;
68
69	pthread_mutex_lock(&mutex);
70	gettimeofday(&tp, NULL);
71
72	ts.tv_sec  = tp.tv_sec;
73	ts.tv_nsec = tp.tv_usec * 1000;
74	ts.tv_sec += 30;
75
76	pthread_cond_timedwait(&cond, &mutex, &ts);
77	pthread_mutex_unlock(&mutex);
78
79	return (NULL);
80}
81
82int
83test(void)
84{
85	int num_thread = THREADS;
86	pthread_t tid[num_thread];
87	int i, iter, rc;
88
89	for (iter = 0; iter < ITER; iter++) {
90		for (i = 0; i < num_thread; i++) {
91			if ((rc = pthread_create(&tid[i], NULL, nicethreads,
92			    NULL)) != 0)
93				errc(1, rc, "pthread_create");
94		}
95		usleep(20000);
96		for (i = 0; i < num_thread; i++) {
97			rc = pthread_mutex_lock(&mutex);
98			rc = pthread_cond_signal(&cond);
99			rc = pthread_mutex_unlock(&mutex);
100		}
101		for (i = 0; i < num_thread; i++)
102			if ((rc = pthread_join(tid[i], NULL)) != 0)
103				errc(1, rc, "pthread_join");
104	}
105
106	_exit(0);
107}
108
109int
110main(void)
111{
112	int i;
113
114	for (i = 0; i < PARALLEL; i++)
115		if (fork() == 0)
116			test();
117	for (i = 0; i < PARALLEL; i++)
118		wait(NULL);
119
120	return (0);
121}
122