xref: /openbsd-src/regress/lib/libpthread/switch/switch.c (revision d7259957e8a5d4370d76bfccd4a30d5d1fe80f38)
1*d7259957Scheloha /*	$OpenBSD: switch.c,v 1.7 2022/12/04 23:50:46 cheloha Exp $	*/
2b2ea75c1Sfgsch /*
3b2ea75c1Sfgsch  * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4b2ea75c1Sfgsch  * proven@mit.edu All rights reserved.
5b2ea75c1Sfgsch  *
6b2ea75c1Sfgsch  * Redistribution and use in source and binary forms, with or without
7b2ea75c1Sfgsch  * modification, are permitted provided that the following conditions
8b2ea75c1Sfgsch  * are met:
9b2ea75c1Sfgsch  * 1. Redistributions of source code must retain the above copyright
10b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer.
11b2ea75c1Sfgsch  * 2. Redistributions in binary form must reproduce the above copyright
12b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer in the
13b2ea75c1Sfgsch  *    documentation and/or other materials provided with the distribution.
14b2ea75c1Sfgsch  * 3. All advertising materials mentioning features or use of this software
15b2ea75c1Sfgsch  *    must display the following acknowledgement:
16b2ea75c1Sfgsch  *	This product includes software developed by Chris Provenzano,
17b2ea75c1Sfgsch  *	the University of California, Berkeley, and contributors.
18b2ea75c1Sfgsch  * 4. Neither the name of Chris Provenzano, the University, nor the names of
19b2ea75c1Sfgsch  *   contributors may be used to endorse or promote products derived
20b2ea75c1Sfgsch  *   from this software without specific prior written permission.
21b2ea75c1Sfgsch  *
22b2ea75c1Sfgsch  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23b2ea75c1Sfgsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b2ea75c1Sfgsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b2ea75c1Sfgsch  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26b2ea75c1Sfgsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27b2ea75c1Sfgsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28b2ea75c1Sfgsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29b2ea75c1Sfgsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30b2ea75c1Sfgsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31b2ea75c1Sfgsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32b2ea75c1Sfgsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33b2ea75c1Sfgsch  */
34b2ea75c1Sfgsch 
35b2ea75c1Sfgsch /* ==== test_switch.c ========================================================
36b2ea75c1Sfgsch  * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
37b2ea75c1Sfgsch  *
38b2ea75c1Sfgsch  * Description : Test context switch functionality.
39b2ea75c1Sfgsch  *
40b2ea75c1Sfgsch  *  1.00 93/08/04 proven
41b2ea75c1Sfgsch  *      -Started coding this file.
42b2ea75c1Sfgsch  */
43b2ea75c1Sfgsch 
44b2ea75c1Sfgsch #include <pthread.h>
45b2ea75c1Sfgsch #include <stdio.h>
46fd167ee3Sbluhm #include <err.h>
47b2ea75c1Sfgsch #include <errno.h>
48b2ea75c1Sfgsch #include <unistd.h>
49b2ea75c1Sfgsch #include <stdlib.h>
50fdddb27bSmarc 
51b2ea75c1Sfgsch #include "test.h"
52b2ea75c1Sfgsch 
53b2ea75c1Sfgsch const char buf[] = "abcdefghijklmnopqrstuvwxyz";
54b2ea75c1Sfgsch char x[sizeof(buf)];
55b2ea75c1Sfgsch 
56b2ea75c1Sfgsch volatile int ending = 0;
57b2ea75c1Sfgsch 
58b2ea75c1Sfgsch /* ==========================================================================
59b2ea75c1Sfgsch  * usage();
60b2ea75c1Sfgsch  */
61fd167ee3Sbluhm static __dead void
usage(void)62db3296cfSderaadt usage(void)
63b2ea75c1Sfgsch {
64*d7259957Scheloha 	fprintf(stderr, "usage: %s [-c count]\n", getprogname());
65fd167ee3Sbluhm 	fprintf(stderr, "count is number of theads, between 2 and 26\n");
66fd167ee3Sbluhm 	exit(1);
67b2ea75c1Sfgsch }
68b2ea75c1Sfgsch 
69db3296cfSderaadt static void *
new_thread(void * arg)70db3296cfSderaadt new_thread(void *arg)
71b2ea75c1Sfgsch {
72fdddb27bSmarc 	int i;
73fdddb27bSmarc 
74b2ea75c1Sfgsch 	SET_NAME("writer");
75b2ea75c1Sfgsch 	while (!ending) {
76fd167ee3Sbluhm 		CHECKe(write(STDOUT_FILENO, (char *) arg, 1));
77b2ea75c1Sfgsch 		x[(char *)arg - buf] = 1;
78fdddb27bSmarc 		for (i = 0; i < 999999; i += 1)
79fdddb27bSmarc 			;
80b2ea75c1Sfgsch 	}
81b2ea75c1Sfgsch 	return NULL;
82b2ea75c1Sfgsch }
83b2ea75c1Sfgsch 
84b2ea75c1Sfgsch int
main(int argc,char * argv[])85db3296cfSderaadt main(int argc, char *argv[])
86b2ea75c1Sfgsch {
87b2ea75c1Sfgsch 	pthread_t thread;
8806cf2034Smiod 	int ch, count = 4;
89b2ea75c1Sfgsch 	long i;
90fd167ee3Sbluhm 	const char *errstr;
91b2ea75c1Sfgsch 
92*d7259957Scheloha 	while ((ch = getopt(argc, argv, "c:")) != -1) {
93fd167ee3Sbluhm 		switch (ch) {
94b2ea75c1Sfgsch 		case 'c':
95fd167ee3Sbluhm 			count = strtonum(optarg, 2, 26, &errstr);
96fd167ee3Sbluhm 			if (errstr != NULL)
97fd167ee3Sbluhm 				errx(1, "count is %s: %s", errstr, optarg);
98b2ea75c1Sfgsch 			break;
99b2ea75c1Sfgsch 		default:
100b2ea75c1Sfgsch 			usage();
101fd167ee3Sbluhm 		}
102b2ea75c1Sfgsch 	}
103b2ea75c1Sfgsch 
104b2ea75c1Sfgsch 	/* create the threads */
105b2ea75c1Sfgsch 	for (i = 0; i < count; i++)
106b2ea75c1Sfgsch 		CHECKr(pthread_create(&thread, NULL, new_thread,
107b2ea75c1Sfgsch 		    (void*)(buf+i)));
108b2ea75c1Sfgsch 
109b2ea75c1Sfgsch 	/* give all threads a chance to run */
110fdddb27bSmarc 	sleep (2);
111b2ea75c1Sfgsch 
112b2ea75c1Sfgsch 	ending = 1;
113b2ea75c1Sfgsch 	for (i = 0; i < count; i++)
114b2ea75c1Sfgsch 		ASSERT(x[i]);	/* make sure each thread ran */
115b2ea75c1Sfgsch 
116c0a6f2a4Smarc 	CHECKe(write(STDOUT_FILENO, "\n", 1));
117b2ea75c1Sfgsch 	SUCCEED;
118b2ea75c1Sfgsch }
119