No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tests / rwlock_test.c
bloba474c1c7e9b35b3cb9d5b64d36af283063ca8b3a
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 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 ISC_PLATFORM_USETHREADS
36 isc_rwlock_t lock;
38 static void *
39 run1(void *arg) {
40 char *message = arg;
42 RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
43 ISC_R_SUCCESS);
44 printf("%s got READ lock\n", message);
45 sleep(1);
46 printf("%s giving up READ lock\n", message);
47 RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
48 ISC_R_SUCCESS);
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_write) ==
57 ISC_R_SUCCESS);
58 printf("%s got WRITE lock\n", message);
59 sleep(1);
60 printf("%s giving up WRITE lock\n", message);
61 RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
62 ISC_R_SUCCESS);
63 return (NULL);
66 static void *
67 run2(void *arg) {
68 char *message = arg;
70 RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
71 ISC_R_SUCCESS);
72 printf("%s got WRITE lock\n", message);
73 sleep(1);
74 printf("%s giving up WRITE lock\n", message);
75 RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
76 ISC_R_SUCCESS);
77 RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
78 ISC_R_SUCCESS);
79 printf("%s got WRITE lock\n", message);
80 sleep(1);
81 printf("%s giving up WRITE lock\n", message);
82 RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
83 ISC_R_SUCCESS);
84 RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
85 ISC_R_SUCCESS);
86 printf("%s got READ lock\n", message);
87 sleep(1);
88 printf("%s giving up READ lock\n", message);
89 RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
90 ISC_R_SUCCESS);
91 return (NULL);
94 int
95 main(int argc, char *argv[]) {
96 unsigned int nworkers;
97 unsigned int i;
98 isc_thread_t workers[100];
99 char name[100];
100 void *dupname;
102 if (argc > 1)
103 nworkers = atoi(argv[1]);
104 else
105 nworkers = 2;
106 if (nworkers > 100)
107 nworkers = 100;
108 printf("%d workers\n", nworkers);
110 RUNTIME_CHECK(isc_rwlock_init(&lock, 5, 10) == ISC_R_SUCCESS);
112 for (i = 0; i < nworkers; i++) {
113 sprintf(name, "%02u", i);
114 dupname = strdup(name);
115 RUNTIME_CHECK(dupname != NULL);
116 if (i != 0 && i % 3 == 0)
117 RUNTIME_CHECK(isc_thread_create(run1, dupname,
118 &workers[i]) ==
119 ISC_R_SUCCESS);
120 else
121 RUNTIME_CHECK(isc_thread_create(run2, dupname,
122 &workers[i]) ==
123 ISC_R_SUCCESS);
126 for (i = 0; i < nworkers; i++)
127 (void)isc_thread_join(workers[i], NULL);
129 isc_rwlock_destroy(&lock);
131 return (0);
134 #else
137 main(int argc, char *argv[]) {
138 UNUSED(argc);
139 UNUSED(argv);
140 fprintf(stderr, "This test requires threads.\n");
141 return(1);
144 #endif