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