xref: /openbsd-src/regress/lib/libc/stdio_threading/include/local.h (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <pthread.h>
23 
24 #define THREAD_COUNT 64
25 
26 #define TEXT	"barnacles"
27 #define	TEXT_N	"barnacles\n"
28 
29 void	run_threads(void (*)(void *), void *);
30 
31 static pthread_rwlock_t	start;
32 static void	(*real_func)(void *);
33 
34 static void *
35 thread(void *arg)
36 {
37 	int	r;
38 
39 	if ((r = pthread_rwlock_rdlock(&start)))
40 		errc(1, r, "could not obtain lock in thread");
41 	real_func(arg);
42 	if ((r = pthread_rwlock_unlock(&start)))
43 		errc(1, r, "could not release lock in thread");
44 	return NULL;
45 }
46 
47 void
48 run_threads(void (*func)(void *), void *arg)
49 {
50 	pthread_t	self, pthread[THREAD_COUNT];
51 	int	i, r;
52 
53 	self = pthread_self();
54 	real_func = func;
55 	if ((r = pthread_rwlock_init(&start, NULL)))
56 		errc(1, r, "could not initialize lock");
57 
58 	if ((r = pthread_rwlock_wrlock(&start)))		/* block */
59 		errc(1, r, "could not lock lock");
60 
61 	for (i = 0; i < THREAD_COUNT; i++)
62 		if ((r = pthread_create(&pthread[i], NULL, thread, arg))) {
63 			warnc(r, "could not create thread");
64 			pthread[i] = self;
65 		}
66 
67 
68 	if ((r = pthread_rwlock_unlock(&start)))		/* unleash */
69 		errc(1, r, "could not release lock");
70 
71 	sleep(1);
72 
73 	if ((r = pthread_rwlock_wrlock(&start)))		/* sync */
74 		errx(1, "parent could not sync with children: %s",
75 		    strerror(r));
76 
77 	for (i = 0; i < THREAD_COUNT; i++)
78 		if (! pthread_equal(pthread[i], self) &&
79 		    (r = pthread_join(pthread[i], NULL)))
80 			warnc(r, "could not join thread");
81 }
82 
83