xref: /freebsd/tools/test/stress2/misc/pthread8.sh (revision 02e9120893770924227138ba49df1edb3896112a)
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# PTHREAD_PRIO_INHERIT test scenario
30
31. ../default.cfg
32
33here=`pwd`
34cd /tmp
35sed '1,/^EOF/d' < $here/$0 > pthread8.c
36mycc -o pthread8 -Wall -Wextra -O0 -g pthread8.c -lpthread || exit 1
37rm -f pthread8.c /tmp/pthread8.core
38
39/tmp/pthread8
40
41rm -f /tmp/pthread8
42exit 0
43EOF
44/* $Id: pi.c,v 1.2 2015/01/31 11:36:07 kostik Exp kostik $ */
45
46#include <sys/types.h>
47#include <sys/sysctl.h>
48#include <err.h>
49#include <pthread.h>
50#include <stdlib.h>
51#include <time.h>
52#include <unistd.h>
53
54struct runner_arg {
55	pthread_mutex_t *locks;
56	u_int lock_cnt;
57};
58
59int stop;
60
61void *
62runner(void *arg)
63{
64	struct runner_arg *ra;
65	pthread_mutex_t *l;
66	u_int i;
67	int error;
68
69	ra = arg;
70	while (stop == 0) {
71		for (i = 0; i < ra->lock_cnt; i++) {
72			l = &ra->locks[i];
73			error = pthread_mutex_lock(l);
74			if (error != 0)
75				errc(1, error, "pthread_mutex_lock");
76			pthread_yield();
77		}
78		for (i = 0; i < ra->lock_cnt; i++) {
79			l = &ra->locks[i];
80			error = pthread_mutex_unlock(l);
81			if (error != 0)
82				errc(1, error, "pthread_mutex_lock");
83			pthread_yield();
84		}
85	}
86	return (NULL);
87}
88
89int
90main(void)
91{
92	struct runner_arg ra;
93	time_t start;
94	pthread_t *threads;
95	pthread_mutexattr_t mattr;
96	u_int i, ncpus;
97	int error;
98	size_t ncpus_len;
99
100	ncpus_len = sizeof(ncpus);
101	error = sysctlbyname("hw.ncpu", &ncpus, &ncpus_len, NULL, 0);
102	if (error != 0)
103		err(1, "sysctl hw.ncpus");
104	threads = calloc(ncpus, sizeof(pthread_t));
105	if (threads == NULL)
106		err(1, "calloc threads");
107
108	ra.lock_cnt = 100;
109	ra.locks = calloc(ra.lock_cnt, sizeof(pthread_mutex_t));
110	if (ra.locks == NULL)
111		err(1, "calloc locks");
112	error = pthread_mutexattr_init(&mattr);
113	if (error != 0)
114		errc(1, error, "pthread_mutexattr_init");
115	error = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
116	if (error != 0)
117		errc(1, error, "pthread_mutexattr_setprotocol PRIO_INHERIT");
118	for (i = 0; i < ra.lock_cnt; i++) {
119		error = pthread_mutex_init(&ra.locks[i], &mattr);
120		if (error != 0)
121			errc(1, error, "pthread_mutex_init");
122	}
123
124	for (i = 0; i < ncpus; i++) {
125		error = pthread_create(&threads[i], NULL, runner, &ra);
126		if (error != 0)
127			errc(1, error, "pthread_create");
128	}
129	start = time(NULL);
130	while (time(NULL) - start < 180)
131		sleep(1);
132	stop = 1;
133	for (i = 0; i < ncpus; i++)
134		pthread_join(threads[i], NULL);
135
136	return (0);
137}
138