From d8f767c12f18c229627a49d709ba08fa19bf1e10 Mon Sep 17 00:00:00 2001 From: Justin Anderson Date: Wed, 10 Oct 2007 19:02:54 +0000 Subject: [PATCH] simple multithreaded locking test for ccapi ticket: 5459 git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20112 dc483132-0cff-0310-8789-dd5450dbe970 --- src/ccapi/test/Makefile | 7 +++- src/ccapi/test/simple_lock_test.c | 69 +++++++++++++++++++++++++++++++ src/ccapi/test/test_ccapi.sh | 2 + 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/ccapi/test/simple_lock_test.c diff --git a/src/ccapi/test/Makefile b/src/ccapi/test/Makefile index 31655f99d..bd7d743ad 100644 --- a/src/ccapi/test/Makefile +++ b/src/ccapi/test/Makefile @@ -20,7 +20,7 @@ TEST_NAMES = test_constants test_cc_initialize test_cc_context_get_version test_ TEST_OBJECTS = $(patsubst %,$(OBJDIR)/%.o,$(TEST_NAMES)) -all: setup-test-dir build-base build-tests link-tests copy-script success-message +all: setup-test-dir simple_lock_test build-base build-tests link-tests copy-script success-message # compile base files used by all tests build-base: $(OBJECTS) @@ -28,7 +28,7 @@ build-base: $(OBJECTS) # compile each test build-tests: $(TEST_OBJECTS) -# rule to compile files in src directory +# rule to compile src files $(OBJDIR)/%.o: %.c $(CC) -c -o $@ $< @@ -45,6 +45,9 @@ link-tests: $(TEST_NAMES) $(TEST_NAMES): $(TEST_OBJECTS) $(CC) -o $(TESTDIR)/$@ $(OBJDIR)/$@.o $(OBJECTS) $(LIBS) +simple_lock_test: + $(CC) -o $(TESTDIR)/simple_lock_test simple_lock_test.c $(LIBS) + copy-script: cp $(SCRIPT_NAME) $(DSTDIR)/$(SCRIPT_NAME) diff --git a/src/ccapi/test/simple_lock_test.c b/src/ccapi/test/simple_lock_test.c new file mode 100644 index 000000000..6674a1ef6 --- /dev/null +++ b/src/ccapi/test/simple_lock_test.c @@ -0,0 +1,69 @@ +/* + simple_lock_test.c + + Initializes two contexts in two different threads and tries to get read locks on both at the same time. + Hangs at line 24. +*/ +#include +#include +#include +#include +#include + +void * other_thread () +{ + cc_int32 err; + cc_context_t context = NULL; + + err = cc_initialize(&context, ccapi_version_7, NULL, NULL); + + fprintf(stderr, "thread: attempting lock. may hang. err == %d\n", err); + + if (!err) { + // hangs with cc_lock_read which should succeed immediately, but does not hang with write, upgrade, and downgrade, which fail immediately + err = cc_context_lock(context, cc_lock_read, cc_lock_noblock); + } + + if (context) { + cc_context_unlock(context); + cc_context_release(context); + context = NULL; + } + fprintf(stderr, "thread: return. err == %d\n", err); +} + + +int main (int argc, char *argv[]) +{ + cc_int32 err; + int status; + pthread_t thread_id; + cc_context_t context = NULL; + + err = cc_initialize(&context, ccapi_version_7, NULL, NULL); + if (!err) { + err = cc_context_lock(context, cc_lock_read, cc_lock_noblock); + } + + fprintf(stderr, "main: initialized and read locked context. err == %d\n", err); + + status = pthread_create (&thread_id, NULL, (void *) other_thread, NULL); + if (status != 0) { + fprintf(stderr,"Create error!\n"); + exit(-1); + } + + pthread_join(thread_id, NULL); + + fprintf(stderr, "main: unlocking and releasing context. err == %d\n", err); + + if (context) { + cc_context_unlock(context); + cc_context_release(context); + context = NULL; + } + + fprintf(stderr, "main: return. err == %d\n", err); + + return 0; +} \ No newline at end of file diff --git a/src/ccapi/test/test_ccapi.sh b/src/ccapi/test/test_ccapi.sh index a59aa0c28..ed833c162 100644 --- a/src/ccapi/test/test_ccapi.sh +++ b/src/ccapi/test/test_ccapi.sh @@ -15,6 +15,8 @@ function run_test { printf "\nBeginning test of CCAPI...\n" printf "\nThese tests are based on the CCAPI v3 revision 8 draft documentation.\n" +run_test simple_lock_test + run_test test_constants run_test test_cc_initialize -- 2.26.2