xref: /freebsd/tools/test/stress2/misc/pthread.sh (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1#!/bin/sh
2
3#
4# Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
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# panic: spin lock held too long
30
31# Test program and scenario by Peter Wemm <peter@FreeBSD.org>
32
33. ../default.cfg
34
35odir=`pwd`
36
37cd /tmp
38sed '1,/^EOF/d' < $odir/$0 > pth.c
39mycc -o pth -Wall pth.c -pthread
40rm -f pth.c
41cd $odir
42
43for i in `jot 2000`; do
44	/tmp/pth 2>/dev/null
45done
46
47rm -f /tmp/pth
48exit
49EOF
50#include <pthread.h>
51
52#include <sys/types.h>
53#include <stdlib.h>
54#include <unistd.h>
55#include <string.h>
56#include <stdio.h>
57
58static pthread_t worker1_thr;
59static pthread_t worker2_thr;
60
61static pthread_mutex_t worker_mtx;
62static pthread_cond_t worker_go;
63static pthread_cond_t worker_done;
64
65struct workitem {
66	struct workitem *next;
67	int a, b;
68};
69
70struct workitem *head;
71
72static void *
73worker(void *arg)
74{
75	struct workitem *w;
76
77	pthread_detach(pthread_self());
78	fprintf(stderr, "WORKER: started %p\n", arg); fflush(stderr);
79
80	for (;;) {
81		pthread_mutex_lock(&worker_mtx);
82		while (head == NULL) {
83			pthread_cond_wait(&worker_go, &worker_mtx);
84		}
85		w = head;
86		head = w->next;
87		pthread_mutex_unlock(&worker_mtx);
88
89		fprintf(stderr, "WORKER(%p): got work a=%d b=%d\n", arg, w->a, w->b); fflush(stderr);
90		free(w);
91		pthread_cond_signal(&worker_done);
92	}
93}
94
95void
96work_add(int a, int b)
97{
98	struct workitem *w;
99	int dowake = 0;
100
101	w = calloc(sizeof(*w), 1);
102	w->a = a;
103	w->b = b;
104	pthread_mutex_lock(&worker_mtx);
105	if (head == 0)
106		dowake = 1;
107	w->next = head;
108	head = w;
109	pthread_mutex_unlock(&worker_mtx);
110	if (dowake)
111		pthread_cond_signal(&worker_go);
112}
113
114int
115main()
116{
117        pthread_mutex_init(&worker_mtx, NULL);
118        pthread_cond_init(&worker_go, NULL);
119        pthread_cond_init(&worker_done, NULL);
120
121	fprintf(stderr, "pthread create\n"); fflush(stderr);
122        pthread_create(&worker1_thr, NULL, worker, (void *)1);
123        pthread_create(&worker2_thr, NULL, worker, (void *)2);
124
125	work_add(10, 15);
126	work_add(11, 22);
127	work_add(314, 159);
128
129	pthread_mutex_lock(&worker_mtx);
130	while (head != NULL) {
131		pthread_cond_wait(&worker_done, &worker_mtx);
132	}
133	pthread_mutex_unlock(&worker_mtx);
134
135	fprintf(stderr, "job complete\n"); fflush(stderr);
136	exit(0);
137}
138