bind9/bin/tests/optional/rwlock_test.c

145 lines
3.5 KiB
C
Raw Normal View History

1998-12-12 15:48:14 -05:00
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
1998-12-12 15:48:14 -05:00
*/
1998-11-11 21:02:52 -05:00
1998-12-12 14:25:20 -05:00
#include <config.h>
1998-11-11 21:02:52 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <isc/print.h>
1998-11-11 21:02:52 -05:00
#include <isc/thread.h>
#include <isc/rwlock.h>
#include <isc/string.h>
2000-04-27 22:12:16 -04:00
#include <isc/util.h>
1998-11-11 21:02:52 -05:00
#ifdef WIN32
#define sleep(x) Sleep(1000 * x)
#endif
#ifdef ISC_PLATFORM_USETHREADS
1998-11-11 21:02:52 -05:00
isc_rwlock_t lock;
static isc_threadresult_t
#ifdef WIN32
WINAPI
#endif
1998-11-11 21:02:52 -05:00
run1(void *arg) {
char *message = arg;
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
ISC_R_SUCCESS);
1998-11-11 21:02:52 -05:00
printf("%s got READ lock\n", message);
sleep(1);
printf("%s giving up READ lock\n", message);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
1998-11-11 21:02:52 -05:00
ISC_R_SUCCESS);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
ISC_R_SUCCESS);
1998-11-11 21:02:52 -05:00
printf("%s got READ lock\n", message);
sleep(1);
printf("%s giving up READ lock\n", message);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
1998-11-11 21:02:52 -05:00
ISC_R_SUCCESS);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
ISC_R_SUCCESS);
1998-11-11 21:02:52 -05:00
printf("%s got WRITE lock\n", message);
sleep(1);
printf("%s giving up WRITE lock\n", message);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
1998-11-11 21:02:52 -05:00
ISC_R_SUCCESS);
return ((isc_threadresult_t)0);
1998-11-11 21:02:52 -05:00
}
static isc_threadresult_t
#ifdef WIN32
WINAPI
#endif
1998-11-11 21:02:52 -05:00
run2(void *arg) {
char *message = arg;
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
ISC_R_SUCCESS);
1998-11-11 21:02:52 -05:00
printf("%s got WRITE lock\n", message);
sleep(1);
printf("%s giving up WRITE lock\n", message);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
1998-11-11 21:02:52 -05:00
ISC_R_SUCCESS);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
ISC_R_SUCCESS);
1998-11-11 21:02:52 -05:00
printf("%s got WRITE lock\n", message);
sleep(1);
printf("%s giving up WRITE lock\n", message);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
1998-11-11 21:02:52 -05:00
ISC_R_SUCCESS);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
ISC_R_SUCCESS);
1998-11-11 21:02:52 -05:00
printf("%s got READ lock\n", message);
sleep(1);
printf("%s giving up READ lock\n", message);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
1998-11-11 21:02:52 -05:00
ISC_R_SUCCESS);
return ((isc_threadresult_t)0);
1998-11-11 21:02:52 -05:00
}
1999-07-03 17:15:06 -04:00
int
1998-11-11 21:02:52 -05:00
main(int argc, char *argv[]) {
unsigned int nworkers;
unsigned int i;
isc_thread_t workers[100];
1998-11-11 21:02:52 -05:00
char name[100];
void *dupname;
if (argc > 1)
nworkers = atoi(argv[1]);
else
nworkers = 2;
if (nworkers > 100)
nworkers = 100;
2018-02-14 02:39:31 -05:00
printf("%u workers\n", nworkers);
1998-11-11 21:02:52 -05:00
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_rwlock_init(&lock, 5, 10) == ISC_R_SUCCESS);
1998-11-11 21:02:52 -05:00
for (i = 0; i < nworkers; i++) {
snprintf(name, sizeof(name), "%02u", i);
1998-11-11 21:02:52 -05:00
dupname = strdup(name);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(dupname != NULL);
1998-11-11 21:02:52 -05:00
if (i != 0 && i % 3 == 0)
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_thread_create(run1, dupname,
&workers[i]) ==
1998-11-11 21:02:52 -05:00
ISC_R_SUCCESS);
else
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_thread_create(run2, dupname,
&workers[i]) ==
1998-11-11 21:02:52 -05:00
ISC_R_SUCCESS);
}
for (i = 0; i < nworkers; i++)
(void)isc_thread_join(workers[i], NULL);
isc_rwlock_destroy(&lock);
1999-07-03 17:15:06 -04:00
return (0);
1998-11-11 21:02:52 -05:00
}
#else
int
main(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
fprintf(stderr, "This test requires threads.\n");
2005-03-16 17:22:31 -05:00
return(1);
}
#endif