1 /*
2  * Copyright (c) 2008 Bret S. Lambert <blambert@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <err.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <pthread.h>
24 
25 #define THREAD_COUNT 64
26 
27 #define TEXT	"barnacles"
28 #define	TEXT_N	"barnacles\n"
29 
30 void	run_threads(void (*)(void *), void *);
31 
32 static pthread_rwlock_t	start;
33 static void	(*real_func)(void *);
34 
35 static void *
36 thread(void *arg)
37 {
38 	int	r;
39 
40 	if ((r = pthread_rwlock_rdlock(&start)))
41 		errc(1, r, "could not obtain lock in thread");
42 	real_func(arg);
43 	if ((r = pthread_rwlock_unlock(&start)))
44 		errc(1, r, "could not release lock in thread");
45 	return NULL;
46 }
47 
48 void
49 run_threads(void (*func)(void *), void *arg)
50 {
51 	pthread_t	self, pthread[THREAD_COUNT];
52 	int	i, r;
53 
54 	self = pthread_self();
55 	real_func = func;
56 	if ((r = pthread_rwlock_init(&start, NULL)))
57 		errc(1, r, "could not initialize lock");
58 
59 	if ((r = pthread_rwlock_wrlock(&start)))		/* block */
60 		errc(1, r, "could not lock lock");
61 
62 	for (i = 0; i < THREAD_COUNT; i++)
63 		if ((r = pthread_create(&pthread[i], NULL, thread, arg))) {
64 			warnc(r, "could not create thread");
65 			pthread[i] = self;
66 		}
67 
68 
69 	if ((r = pthread_rwlock_unlock(&start)))		/* unleash */
70 		errc(1, r, "could not release lock");
71 
72 	sleep(1);
73 
74 	if ((r = pthread_rwlock_wrlock(&start)))		/* sync */
75 		errx(1, "parent could not sync with children: %s",
76 		    strerror(r));
77 
78 	for (i = 0; i < THREAD_COUNT; i++)
79 		if (! pthread_equal(pthread[i], self) &&
80 		    (r = pthread_join(pthread[i], NULL)))
81 			warnc(r, "could not join thread");
82 }
83 
84