xref: /minix3/external/bsd/bind/dist/bin/tests/rwlock_test.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1*00b67f09SDavid van Moolenbroek /*	$NetBSD: rwlock_test.c,v 1.7 2014/12/10 04:37:53 christos Exp $	*/
2*00b67f09SDavid van Moolenbroek 
3*00b67f09SDavid van Moolenbroek /*
4*00b67f09SDavid van Moolenbroek  * Copyright (C) 2004, 2005, 2007, 2013  Internet Systems Consortium, Inc. ("ISC")
5*00b67f09SDavid van Moolenbroek  * Copyright (C) 1998-2001  Internet Software Consortium.
6*00b67f09SDavid van Moolenbroek  *
7*00b67f09SDavid van Moolenbroek  * Permission to use, copy, modify, and/or distribute this software for any
8*00b67f09SDavid van Moolenbroek  * purpose with or without fee is hereby granted, provided that the above
9*00b67f09SDavid van Moolenbroek  * copyright notice and this permission notice appear in all copies.
10*00b67f09SDavid van Moolenbroek  *
11*00b67f09SDavid van Moolenbroek  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12*00b67f09SDavid van Moolenbroek  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13*00b67f09SDavid van Moolenbroek  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14*00b67f09SDavid van Moolenbroek  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15*00b67f09SDavid van Moolenbroek  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16*00b67f09SDavid van Moolenbroek  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17*00b67f09SDavid van Moolenbroek  * PERFORMANCE OF THIS SOFTWARE.
18*00b67f09SDavid van Moolenbroek  */
19*00b67f09SDavid van Moolenbroek 
20*00b67f09SDavid van Moolenbroek /* Id: rwlock_test.c,v 1.26 2007/06/19 23:46:59 tbox Exp  */
21*00b67f09SDavid van Moolenbroek 
22*00b67f09SDavid van Moolenbroek #include <config.h>
23*00b67f09SDavid van Moolenbroek 
24*00b67f09SDavid van Moolenbroek #include <stdio.h>
25*00b67f09SDavid van Moolenbroek #include <stdlib.h>
26*00b67f09SDavid van Moolenbroek #include <unistd.h>
27*00b67f09SDavid van Moolenbroek 
28*00b67f09SDavid van Moolenbroek #include <isc/print.h>
29*00b67f09SDavid van Moolenbroek #include <isc/thread.h>
30*00b67f09SDavid van Moolenbroek #include <isc/rwlock.h>
31*00b67f09SDavid van Moolenbroek #include <isc/string.h>
32*00b67f09SDavid van Moolenbroek #include <isc/util.h>
33*00b67f09SDavid van Moolenbroek 
34*00b67f09SDavid van Moolenbroek #ifdef WIN32
35*00b67f09SDavid van Moolenbroek #define sleep(x)	Sleep(1000 * x)
36*00b67f09SDavid van Moolenbroek #endif
37*00b67f09SDavid van Moolenbroek 
38*00b67f09SDavid van Moolenbroek #ifdef ISC_PLATFORM_USETHREADS
39*00b67f09SDavid van Moolenbroek 
40*00b67f09SDavid van Moolenbroek isc_rwlock_t lock;
41*00b67f09SDavid van Moolenbroek 
42*00b67f09SDavid van Moolenbroek static isc_threadresult_t
43*00b67f09SDavid van Moolenbroek #ifdef WIN32
44*00b67f09SDavid van Moolenbroek WINAPI
45*00b67f09SDavid van Moolenbroek #endif
run1(void * arg)46*00b67f09SDavid van Moolenbroek run1(void *arg) {
47*00b67f09SDavid van Moolenbroek 	char *message = arg;
48*00b67f09SDavid van Moolenbroek 
49*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
50*00b67f09SDavid van Moolenbroek 		      ISC_R_SUCCESS);
51*00b67f09SDavid van Moolenbroek 	printf("%s got READ lock\n", message);
52*00b67f09SDavid van Moolenbroek 	sleep(1);
53*00b67f09SDavid van Moolenbroek 	printf("%s giving up READ lock\n", message);
54*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
55*00b67f09SDavid van Moolenbroek 	       ISC_R_SUCCESS);
56*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
57*00b67f09SDavid van Moolenbroek 		      ISC_R_SUCCESS);
58*00b67f09SDavid van Moolenbroek 	printf("%s got READ lock\n", message);
59*00b67f09SDavid van Moolenbroek 	sleep(1);
60*00b67f09SDavid van Moolenbroek 	printf("%s giving up READ lock\n", message);
61*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
62*00b67f09SDavid van Moolenbroek 	       ISC_R_SUCCESS);
63*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
64*00b67f09SDavid van Moolenbroek 		      ISC_R_SUCCESS);
65*00b67f09SDavid van Moolenbroek 	printf("%s got WRITE lock\n", message);
66*00b67f09SDavid van Moolenbroek 	sleep(1);
67*00b67f09SDavid van Moolenbroek 	printf("%s giving up WRITE lock\n", message);
68*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
69*00b67f09SDavid van Moolenbroek 	       ISC_R_SUCCESS);
70*00b67f09SDavid van Moolenbroek 	return ((isc_threadresult_t)0);
71*00b67f09SDavid van Moolenbroek }
72*00b67f09SDavid van Moolenbroek 
73*00b67f09SDavid van Moolenbroek static isc_threadresult_t
74*00b67f09SDavid van Moolenbroek #ifdef WIN32
75*00b67f09SDavid van Moolenbroek WINAPI
76*00b67f09SDavid van Moolenbroek #endif
run2(void * arg)77*00b67f09SDavid van Moolenbroek run2(void *arg) {
78*00b67f09SDavid van Moolenbroek 	char *message = arg;
79*00b67f09SDavid van Moolenbroek 
80*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
81*00b67f09SDavid van Moolenbroek 		      ISC_R_SUCCESS);
82*00b67f09SDavid van Moolenbroek 	printf("%s got WRITE lock\n", message);
83*00b67f09SDavid van Moolenbroek 	sleep(1);
84*00b67f09SDavid van Moolenbroek 	printf("%s giving up WRITE lock\n", message);
85*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
86*00b67f09SDavid van Moolenbroek 	       ISC_R_SUCCESS);
87*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
88*00b67f09SDavid van Moolenbroek 		      ISC_R_SUCCESS);
89*00b67f09SDavid van Moolenbroek 	printf("%s got WRITE lock\n", message);
90*00b67f09SDavid van Moolenbroek 	sleep(1);
91*00b67f09SDavid van Moolenbroek 	printf("%s giving up WRITE lock\n", message);
92*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
93*00b67f09SDavid van Moolenbroek 	       ISC_R_SUCCESS);
94*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
95*00b67f09SDavid van Moolenbroek 		      ISC_R_SUCCESS);
96*00b67f09SDavid van Moolenbroek 	printf("%s got READ lock\n", message);
97*00b67f09SDavid van Moolenbroek 	sleep(1);
98*00b67f09SDavid van Moolenbroek 	printf("%s giving up READ lock\n", message);
99*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
100*00b67f09SDavid van Moolenbroek 	       ISC_R_SUCCESS);
101*00b67f09SDavid van Moolenbroek 	return ((isc_threadresult_t)0);
102*00b67f09SDavid van Moolenbroek }
103*00b67f09SDavid van Moolenbroek 
104*00b67f09SDavid van Moolenbroek int
main(int argc,char * argv[])105*00b67f09SDavid van Moolenbroek main(int argc, char *argv[]) {
106*00b67f09SDavid van Moolenbroek 	unsigned int nworkers;
107*00b67f09SDavid van Moolenbroek 	unsigned int i;
108*00b67f09SDavid van Moolenbroek 	isc_thread_t workers[100];
109*00b67f09SDavid van Moolenbroek 	char name[100];
110*00b67f09SDavid van Moolenbroek 	void *dupname;
111*00b67f09SDavid van Moolenbroek 
112*00b67f09SDavid van Moolenbroek 	if (argc > 1)
113*00b67f09SDavid van Moolenbroek 		nworkers = atoi(argv[1]);
114*00b67f09SDavid van Moolenbroek 	else
115*00b67f09SDavid van Moolenbroek 		nworkers = 2;
116*00b67f09SDavid van Moolenbroek 	if (nworkers > 100)
117*00b67f09SDavid van Moolenbroek 		nworkers = 100;
118*00b67f09SDavid van Moolenbroek 	printf("%d workers\n", nworkers);
119*00b67f09SDavid van Moolenbroek 
120*00b67f09SDavid van Moolenbroek 	RUNTIME_CHECK(isc_rwlock_init(&lock, 5, 10) == ISC_R_SUCCESS);
121*00b67f09SDavid van Moolenbroek 
122*00b67f09SDavid van Moolenbroek 	for (i = 0; i < nworkers; i++) {
123*00b67f09SDavid van Moolenbroek 		sprintf(name, "%02u", i);
124*00b67f09SDavid van Moolenbroek 		dupname = strdup(name);
125*00b67f09SDavid van Moolenbroek 		RUNTIME_CHECK(dupname != NULL);
126*00b67f09SDavid van Moolenbroek 		if (i != 0 && i % 3 == 0)
127*00b67f09SDavid van Moolenbroek 			RUNTIME_CHECK(isc_thread_create(run1, dupname,
128*00b67f09SDavid van Moolenbroek 							&workers[i]) ==
129*00b67f09SDavid van Moolenbroek 			       ISC_R_SUCCESS);
130*00b67f09SDavid van Moolenbroek 		else
131*00b67f09SDavid van Moolenbroek 			RUNTIME_CHECK(isc_thread_create(run2, dupname,
132*00b67f09SDavid van Moolenbroek 							&workers[i]) ==
133*00b67f09SDavid van Moolenbroek 			       ISC_R_SUCCESS);
134*00b67f09SDavid van Moolenbroek 	}
135*00b67f09SDavid van Moolenbroek 
136*00b67f09SDavid van Moolenbroek 	for (i = 0; i < nworkers; i++)
137*00b67f09SDavid van Moolenbroek 		(void)isc_thread_join(workers[i], NULL);
138*00b67f09SDavid van Moolenbroek 
139*00b67f09SDavid van Moolenbroek 	isc_rwlock_destroy(&lock);
140*00b67f09SDavid van Moolenbroek 
141*00b67f09SDavid van Moolenbroek 	return (0);
142*00b67f09SDavid van Moolenbroek }
143*00b67f09SDavid van Moolenbroek 
144*00b67f09SDavid van Moolenbroek #else
145*00b67f09SDavid van Moolenbroek 
146*00b67f09SDavid van Moolenbroek int
main(int argc,char * argv[])147*00b67f09SDavid van Moolenbroek main(int argc, char *argv[]) {
148*00b67f09SDavid van Moolenbroek 	UNUSED(argc);
149*00b67f09SDavid van Moolenbroek 	UNUSED(argv);
150*00b67f09SDavid van Moolenbroek 	fprintf(stderr, "This test requires threads.\n");
151*00b67f09SDavid van Moolenbroek 	return(1);
152*00b67f09SDavid van Moolenbroek }
153*00b67f09SDavid van Moolenbroek 
154*00b67f09SDavid van Moolenbroek #endif
155