From 64fefba66ba615f862e69e7ee6076aaf36dbe794 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Thu, 26 Jan 2012 23:45:51 +0600 Subject: [PATCH] refcounter: remove unexpected behaviour parts * bplus: ref page_copy in compact --- src/bplus.c | 6 +++++- src/refcounter.c | 6 ------ test/test-threaded-rw.cc | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/bplus.c b/src/bplus.c index 70f7eb6..185f441 100644 --- a/src/bplus.c +++ b/src/bplus.c @@ -154,8 +154,13 @@ int bp_compact(bp_tree_t* tree) { free(compacted_name); if (ret != BP_OK) return ret; + bp__ref((bp__ref_t*) tree); + /* copy all pages starting from root */ ret = bp__page_copy(tree, &compacted, tree->head.page); + + bp__unref((bp__ref_t*) tree); + if (ret != BP_OK) return ret; /* compacted tree already has a head page, free it first */ @@ -173,7 +178,6 @@ int bp_compact(bp_tree_t* tree) { (bp__writer_t*) &compacted); bp__ref_open((bp__ref_t*) tree); - return ret; } diff --git a/src/refcounter.c b/src/refcounter.c index f360086..d8bb1c7 100644 --- a/src/refcounter.c +++ b/src/refcounter.c @@ -31,12 +31,6 @@ void bp__ref_destroy(bp__ref_t* handle) { void bp__ref(bp__ref_t* handle) { SCOPED_LOCK(handle->ref_lock, { - if (handle->ref_state != kOpen) { - bp__mutex_unlock(&handle->ref_lock); - bp__mutex_lock(&handle->ref_state_lock); - bp__mutex_unlock(&handle->ref_state_lock); - bp__mutex_lock(&handle->ref_lock); - } /* * pause close requests until handle will have ref == 0 */ diff --git a/test/test-threaded-rw.cc b/test/test-threaded-rw.cc index 70e9353..acdf665 100644 --- a/test/test-threaded-rw.cc +++ b/test/test-threaded-rw.cc @@ -37,20 +37,37 @@ void* test_writer(void* db_) { return NULL; } +void* test_compact(void* db_) { + bp_tree_t* db = (bp_tree_t*) db_; + int ret; + + for (int i = 0; i < items; i++) { + usleep(33000); + ret = bp_compact(db); + fprintf(stdout, "%x\n", ret); + assert(ret == BP_OK); + } + + return NULL; +} + TEST_START("threaded read/write test", "threaded-rw") const int n = 10; pthread_t readers[n]; pthread_t writers[n]; + pthread_t compact; for (int i = 0; i < n; i++) { assert(pthread_create(&readers[i], NULL, test_reader, (void*) &db) == 0); assert(pthread_create(&writers[i], NULL, test_writer, (void*) &db) == 0); } +// assert(pthread_create(&compact, NULL, test_compact, (void*) &db) == 0); for (int i = 0; i < n; i++) { assert(pthread_join(readers[i], NULL) == 0); assert(pthread_join(writers[i], NULL) == 0); } +// assert(pthread_join(compact, NULL) == 0); TEST_END("threaded read/write test", "threaded-rw")