1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2016-06-27 14:56:38 +10:00
|
|
|
* Copyright (C) 1998-2001, 2004, 2005, 2007, 2013, 2016 Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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/.
|
1998-12-12 20:48:14 +00:00
|
|
|
*/
|
1998-11-12 02:02:52 +00:00
|
|
|
|
2007-06-19 23:47:24 +00:00
|
|
|
/* $Id: rwlock_test.c,v 1.26 2007/06/19 23:46:59 tbox Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1998-12-12 19:25:20 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1998-11-12 02:02:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2004-08-28 06:20:14 +00:00
|
|
|
#include <isc/print.h>
|
1998-11-12 02:02:52 +00:00
|
|
|
#include <isc/thread.h>
|
|
|
|
#include <isc/rwlock.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-04-28 02:12:16 +00:00
|
|
|
#include <isc/util.h>
|
1998-11-12 02:02:52 +00:00
|
|
|
|
2013-12-04 12:47:23 +11:00
|
|
|
#ifdef WIN32
|
|
|
|
#define sleep(x) Sleep(1000 * x)
|
|
|
|
#endif
|
|
|
|
|
2001-05-02 17:35:52 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
|
1998-11-12 02:02:52 +00:00
|
|
|
isc_rwlock_t lock;
|
|
|
|
|
2013-12-04 12:47:23 +11:00
|
|
|
static isc_threadresult_t
|
|
|
|
#ifdef WIN32
|
|
|
|
WINAPI
|
|
|
|
#endif
|
1998-11-12 02:02:52 +00:00
|
|
|
run1(void *arg) {
|
|
|
|
char *message = arg;
|
|
|
|
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-11-12 02:02:52 +00:00
|
|
|
printf("%s got READ lock\n", message);
|
|
|
|
sleep(1);
|
|
|
|
printf("%s giving up READ lock\n", message);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
|
1998-11-12 02:02:52 +00:00
|
|
|
ISC_R_SUCCESS);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-11-12 02:02:52 +00:00
|
|
|
printf("%s got READ lock\n", message);
|
|
|
|
sleep(1);
|
|
|
|
printf("%s giving up READ lock\n", message);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
|
1998-11-12 02:02:52 +00:00
|
|
|
ISC_R_SUCCESS);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-11-12 02:02:52 +00:00
|
|
|
printf("%s got WRITE lock\n", message);
|
|
|
|
sleep(1);
|
|
|
|
printf("%s giving up WRITE lock\n", message);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
|
1998-11-12 02:02:52 +00:00
|
|
|
ISC_R_SUCCESS);
|
2013-12-04 12:47:23 +11:00
|
|
|
return ((isc_threadresult_t)0);
|
1998-11-12 02:02:52 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 12:47:23 +11:00
|
|
|
static isc_threadresult_t
|
|
|
|
#ifdef WIN32
|
|
|
|
WINAPI
|
|
|
|
#endif
|
1998-11-12 02:02:52 +00:00
|
|
|
run2(void *arg) {
|
|
|
|
char *message = arg;
|
|
|
|
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-11-12 02:02:52 +00:00
|
|
|
printf("%s got WRITE lock\n", message);
|
|
|
|
sleep(1);
|
|
|
|
printf("%s giving up WRITE lock\n", message);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
|
1998-11-12 02:02:52 +00:00
|
|
|
ISC_R_SUCCESS);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-11-12 02:02:52 +00:00
|
|
|
printf("%s got WRITE lock\n", message);
|
|
|
|
sleep(1);
|
|
|
|
printf("%s giving up WRITE lock\n", message);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) ==
|
1998-11-12 02:02:52 +00:00
|
|
|
ISC_R_SUCCESS);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-11-12 02:02:52 +00:00
|
|
|
printf("%s got READ lock\n", message);
|
|
|
|
sleep(1);
|
|
|
|
printf("%s giving up READ lock\n", message);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) ==
|
1998-11-12 02:02:52 +00:00
|
|
|
ISC_R_SUCCESS);
|
2013-12-04 12:47:23 +11:00
|
|
|
return ((isc_threadresult_t)0);
|
1998-11-12 02:02:52 +00:00
|
|
|
}
|
|
|
|
|
1999-07-03 21:15:06 +00:00
|
|
|
int
|
1998-11-12 02:02:52 +00:00
|
|
|
main(int argc, char *argv[]) {
|
|
|
|
unsigned int nworkers;
|
|
|
|
unsigned int i;
|
1998-11-12 23:31:42 +00:00
|
|
|
isc_thread_t workers[100];
|
1998-11-12 02:02:52 +00:00
|
|
|
char name[100];
|
|
|
|
void *dupname;
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
nworkers = atoi(argv[1]);
|
|
|
|
else
|
|
|
|
nworkers = 2;
|
1998-11-12 23:31:42 +00:00
|
|
|
if (nworkers > 100)
|
|
|
|
nworkers = 100;
|
1998-11-12 02:02:52 +00:00
|
|
|
printf("%d workers\n", nworkers);
|
|
|
|
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_rwlock_init(&lock, 5, 10) == ISC_R_SUCCESS);
|
1998-11-12 02:02:52 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nworkers; i++) {
|
|
|
|
sprintf(name, "%02u", i);
|
|
|
|
dupname = strdup(name);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(dupname != NULL);
|
1998-11-12 02:02:52 +00:00
|
|
|
if (i != 0 && i % 3 == 0)
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_thread_create(run1, dupname,
|
|
|
|
&workers[i]) ==
|
1998-11-12 02:02:52 +00:00
|
|
|
ISC_R_SUCCESS);
|
|
|
|
else
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_thread_create(run2, dupname,
|
|
|
|
&workers[i]) ==
|
1998-11-12 02:02:52 +00:00
|
|
|
ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nworkers; i++)
|
|
|
|
(void)isc_thread_join(workers[i], NULL);
|
|
|
|
|
|
|
|
isc_rwlock_destroy(&lock);
|
1999-07-03 21:15:06 +00:00
|
|
|
|
|
|
|
return (0);
|
1998-11-12 02:02:52 +00:00
|
|
|
}
|
2001-05-02 17:35:52 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
fprintf(stderr, "This test requires threads.\n");
|
2005-03-16 22:22:31 +00:00
|
|
|
return(1);
|
2001-05-02 17:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|