4 # Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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
31 # panic: spin lock held too long
33 # Test program and scenario by Peter Wemm <peter@FreeBSD.org>
40 sed '1,/^EOF/d' < $odir/$0 > pth.c
41 cc -o pth -Wall pth.c -pthread
45 for i in `jot 2000`; do
55 #include <sys/types.h>
61 static pthread_t worker1_thr;
62 static pthread_t worker2_thr;
64 static pthread_mutex_t worker_mtx;
65 static pthread_cond_t worker_go;
66 static pthread_cond_t worker_done;
69 struct workitem *next;
73 struct workitem *head;
80 pthread_detach(pthread_self());
81 fprintf(stderr, "WORKER: started %p\n", arg); fflush(stderr);
84 pthread_mutex_lock(&worker_mtx);
85 while (head == NULL) {
86 pthread_cond_wait(&worker_go, &worker_mtx);
90 pthread_mutex_unlock(&worker_mtx);
92 fprintf(stderr, "WORKER(%p): got work a=%d b=%d\n", arg, w->a, w->b); fflush(stderr);
94 pthread_cond_signal(&worker_done);
99 work_add(int a, int b)
104 w = calloc(sizeof(*w), 1);
107 pthread_mutex_lock(&worker_mtx);
112 pthread_mutex_unlock(&worker_mtx);
114 pthread_cond_signal(&worker_go);
120 pthread_mutex_init(&worker_mtx, NULL);
121 pthread_cond_init(&worker_go, NULL);
122 pthread_cond_init(&worker_done, NULL);
124 fprintf(stderr, "pthread create\n"); fflush(stderr);
125 pthread_create(&worker1_thr, NULL, worker, (void *)1);
126 pthread_create(&worker2_thr, NULL, worker, (void *)2);
132 pthread_mutex_lock(&worker_mtx);
133 while (head != NULL) {
134 pthread_cond_wait(&worker_done, &worker_mtx);
136 pthread_mutex_unlock(&worker_mtx);
138 fprintf(stderr, "job complete\n"); fflush(stderr);