1 /* $NetBSD: rwlock_test.c,v 1.7 2014/12/10 04:37:53 christos Exp $ */
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 */
28 #include <isc/print.h>
29 #include <isc/thread.h>
30 #include <isc/rwlock.h>
31 #include <isc/string.h>
35 #define sleep(x) Sleep(1000 * x)
38 #ifdef ISC_PLATFORM_USETHREADS
42 static isc_threadresult_t
49 RUNTIME_CHECK(isc_rwlock_lock(&lock
, isc_rwlocktype_read
) ==
51 printf("%s got READ lock\n", message
);
53 printf("%s giving up READ lock\n", message
);
54 RUNTIME_CHECK(isc_rwlock_unlock(&lock
, isc_rwlocktype_read
) ==
56 RUNTIME_CHECK(isc_rwlock_lock(&lock
, isc_rwlocktype_read
) ==
58 printf("%s got READ lock\n", message
);
60 printf("%s giving up READ lock\n", message
);
61 RUNTIME_CHECK(isc_rwlock_unlock(&lock
, isc_rwlocktype_read
) ==
63 RUNTIME_CHECK(isc_rwlock_lock(&lock
, isc_rwlocktype_write
) ==
65 printf("%s got WRITE lock\n", message
);
67 printf("%s giving up WRITE lock\n", message
);
68 RUNTIME_CHECK(isc_rwlock_unlock(&lock
, isc_rwlocktype_write
) ==
70 return ((isc_threadresult_t
)0);
73 static isc_threadresult_t
80 RUNTIME_CHECK(isc_rwlock_lock(&lock
, isc_rwlocktype_write
) ==
82 printf("%s got WRITE lock\n", message
);
84 printf("%s giving up WRITE lock\n", message
);
85 RUNTIME_CHECK(isc_rwlock_unlock(&lock
, isc_rwlocktype_write
) ==
87 RUNTIME_CHECK(isc_rwlock_lock(&lock
, isc_rwlocktype_write
) ==
89 printf("%s got WRITE lock\n", message
);
91 printf("%s giving up WRITE lock\n", message
);
92 RUNTIME_CHECK(isc_rwlock_unlock(&lock
, isc_rwlocktype_write
) ==
94 RUNTIME_CHECK(isc_rwlock_lock(&lock
, isc_rwlocktype_read
) ==
96 printf("%s got READ lock\n", message
);
98 printf("%s giving up READ lock\n", message
);
99 RUNTIME_CHECK(isc_rwlock_unlock(&lock
, isc_rwlocktype_read
) ==
101 return ((isc_threadresult_t
)0);
105 main(int argc
, char *argv
[]) {
106 unsigned int nworkers
;
108 isc_thread_t workers
[100];
113 nworkers
= atoi(argv
[1]);
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
,
131 RUNTIME_CHECK(isc_thread_create(run2
, dupname
,
136 for (i
= 0; i
< nworkers
; i
++)
137 (void)isc_thread_join(workers
[i
], NULL
);
139 isc_rwlock_destroy(&lock
);
147 main(int argc
, char *argv
[]) {
150 fprintf(stderr
, "This test requires threads.\n");