diff options
Diffstat (limited to 'storage/tokudb/ft-index/portability')
-rw-r--r-- | storage/tokudb/ft-index/portability/CMakeLists.txt | 13 | ||||
-rw-r--r-- | storage/tokudb/ft-index/portability/file.cc | 24 | ||||
-rw-r--r-- | storage/tokudb/ft-index/portability/os_malloc.cc | 1 | ||||
-rw-r--r-- | storage/tokudb/ft-index/portability/tests/test-fair-rwlock.cc | 665 | ||||
-rw-r--r-- | storage/tokudb/ft-index/portability/toku_fair_rwlock.cc | 361 | ||||
-rw-r--r-- | storage/tokudb/ft-index/portability/toku_fair_rwlock.h | 237 |
6 files changed, 34 insertions, 1267 deletions
diff --git a/storage/tokudb/ft-index/portability/CMakeLists.txt b/storage/tokudb/ft-index/portability/CMakeLists.txt index 1d64878675e..e4364357fb0 100644 --- a/storage/tokudb/ft-index/portability/CMakeLists.txt +++ b/storage/tokudb/ft-index/portability/CMakeLists.txt @@ -8,7 +8,6 @@ set(tokuportability_srcs portability toku_assert toku_crash - toku_fair_rwlock toku_path toku_pthread toku_time @@ -20,6 +19,7 @@ target_link_libraries(${LIBTOKUPORTABILITY} LINK_PUBLIC ${CMAKE_THREAD_LIBS_INIT add_library(tokuportability_static_conv STATIC ${tokuportability_srcs}) set_target_properties(tokuportability_static_conv PROPERTIES POSITION_INDEPENDENT_CODE ON) +add_dependencies(tokuportability_static_conv build_jemalloc) set(tokuportability_source_libs tokuportability_static_conv ${LIBJEMALLOC} ${CMAKE_THREAD_LIBS_INIT} ${EXTRA_SYSTEM_LIBS}) toku_merge_static_libs(${LIBTOKUPORTABILITY}_static ${LIBTOKUPORTABILITY}_static "${tokuportability_source_libs}") @@ -34,10 +34,17 @@ if (NOT DEFINED MYSQL_PROJECT_NAME_DOCSTRING) install( FILES toku_os_types.h toku_time.h DESTINATION include + COMPONENT tokukv_headers ) install( - TARGETS ${LIBTOKUPORTABILITY} ${LIBTOKUPORTABILITY}_static - DESTINATION lib + TARGETS ${LIBTOKUPORTABILITY}_static + DESTINATION ${INSTALL_LIBDIR} + COMPONENT tokukv_libs_static + ) + install( + TARGETS ${LIBTOKUPORTABILITY} + DESTINATION ${INSTALL_LIBDIR} + COMPONENT tokukv_libs_shared ) endif () diff --git a/storage/tokudb/ft-index/portability/file.cc b/storage/tokudb/ft-index/portability/file.cc index 99a7d6c64ca..b351141fe29 100644 --- a/storage/tokudb/ft-index/portability/file.cc +++ b/storage/tokudb/ft-index/portability/file.cc @@ -449,6 +449,8 @@ static uint64_t toku_fsync_time; static uint64_t toku_long_fsync_threshold = 1000000; static uint64_t toku_long_fsync_count; static uint64_t toku_long_fsync_time; +static uint64_t toku_long_fsync_eintr_count; +static int toku_fsync_debug = 0; void toku_set_func_fsync(int (*fsync_function)(int)) { t_fsync = fsync_function; @@ -458,6 +460,7 @@ void toku_set_func_fsync(int (*fsync_function)(int)) { static void file_fsync_internal (int fd) { uint64_t tstart = toku_current_time_microsec(); int r = -1; + uint64_t eintr_count = 0; while (r != 0) { if (t_fsync) { r = t_fsync(fd); @@ -466,6 +469,7 @@ static void file_fsync_internal (int fd) { } if (r) { assert(get_error_errno() == EINTR); + eintr_count++; } } toku_sync_fetch_and_add(&toku_fsync_count, 1); @@ -474,6 +478,26 @@ static void file_fsync_internal (int fd) { if (duration >= toku_long_fsync_threshold) { toku_sync_fetch_and_add(&toku_long_fsync_count, 1); toku_sync_fetch_and_add(&toku_long_fsync_time, duration); + toku_sync_fetch_and_add(&toku_long_fsync_eintr_count, eintr_count); + if (toku_fsync_debug) { + const int tstr_length = 26; + char tstr[tstr_length]; + time_t t = time(0); +#if __linux__ + char fdname[256]; + snprintf(fdname, sizeof fdname, "/proc/%d/fd/%d", getpid(), fd); + char lname[256]; + ssize_t s = readlink(fdname, lname, sizeof lname); + if (0 < s && s < (ssize_t) sizeof lname) + lname[s] = 0; + fprintf(stderr, "%.24s toku_file_fsync %s fd=%d %s duration=%" PRIu64 " usec eintr=%" PRIu64 "\n", + ctime_r(&t, tstr), __FUNCTION__, fd, s > 0 ? lname : "?", duration, eintr_count); +#else + fprintf(stderr, "%.24s toku_file_fsync %s fd=%d duration=%" PRIu64 " usec eintr=%" PRIu64 "\n", + ctime_r(&t, tstr), __FUNCTION__, fd, duration, eintr_count); +#endif + fflush(stderr); + } } } diff --git a/storage/tokudb/ft-index/portability/os_malloc.cc b/storage/tokudb/ft-index/portability/os_malloc.cc index 0b2b5f64967..ecc5ea9ca3c 100644 --- a/storage/tokudb/ft-index/portability/os_malloc.cc +++ b/storage/tokudb/ft-index/portability/os_malloc.cc @@ -93,7 +93,6 @@ PATENT RIGHTS GRANT: #include <toku_portability.h> #include <stdlib.h> -//#include <jemalloc/include/jemalloc/jemalloc.h> #if defined(HAVE_MALLOC_H) # include <malloc.h> #elif defined(HAVE_SYS_MALLOC_H) diff --git a/storage/tokudb/ft-index/portability/tests/test-fair-rwlock.cc b/storage/tokudb/ft-index/portability/tests/test-fair-rwlock.cc deleted file mode 100644 index fa549d3f2d8..00000000000 --- a/storage/tokudb/ft-index/portability/tests/test-fair-rwlock.cc +++ /dev/null @@ -1,665 +0,0 @@ -/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ -// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: - -#ident "$Id$" -/* -COPYING CONDITIONS NOTICE: - - This program is free software; you can redistribute it and/or modify - it under the terms of version 2 of the GNU General Public License as - published by the Free Software Foundation, and provided that the - following conditions are met: - - * Redistributions of source code must retain this COPYING - CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the - DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the - PATENT MARKING NOTICE (below), and the PATENT RIGHTS - GRANT (below). - - * Redistributions in binary form must reproduce this COPYING - CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the - DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the - PATENT MARKING NOTICE (below), and the PATENT RIGHTS - GRANT (below) in the documentation and/or other materials - provided with the distribution. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. - -COPYRIGHT NOTICE: - - TokuDB, Tokutek Fractal Tree Indexing Library. - Copyright (C) 2007-2013 Tokutek, Inc. - -DISCLAIMER: - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - -UNIVERSITY PATENT NOTICE: - - The technology is licensed by the Massachusetts Institute of - Technology, Rutgers State University of New Jersey, and the Research - Foundation of State University of New York at Stony Brook under - United States of America Serial No. 11/760379 and to the patents - and/or patent applications resulting from it. - -PATENT MARKING NOTICE: - - This software is covered by US Patent No. 8,185,551. - This software is covered by US Patent No. 8,489,638. - -PATENT RIGHTS GRANT: - - "THIS IMPLEMENTATION" means the copyrightable works distributed by - Tokutek as part of the Fractal Tree project. - - "PATENT CLAIMS" means the claims of patents that are owned or - licensable by Tokutek, both currently or in the future; and that in - the absence of this license would be infringed by THIS - IMPLEMENTATION or by using or running THIS IMPLEMENTATION. - - "PATENT CHALLENGE" shall mean a challenge to the validity, - patentability, enforceability and/or non-infringement of any of the - PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS. - - Tokutek hereby grants to you, for the term and geographical scope of - the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free, - irrevocable (except as stated in this section) patent license to - make, have made, use, offer to sell, sell, import, transfer, and - otherwise run, modify, and propagate the contents of THIS - IMPLEMENTATION, where such license applies only to the PATENT - CLAIMS. This grant does not include claims that would be infringed - only as a consequence of further modifications of THIS - IMPLEMENTATION. If you or your agent or licensee institute or order - or agree to the institution of patent litigation against any entity - (including a cross-claim or counterclaim in a lawsuit) alleging that - THIS IMPLEMENTATION constitutes direct or contributory patent - infringement, or inducement of patent infringement, then any rights - granted to you under this License shall terminate as of the date - such litigation is filed. If you or your agent or exclusive - licensee institute or order or agree to the institution of a PATENT - CHALLENGE, then Tokutek may terminate any rights granted to you - under this License. -*/ - -#ident "Copyright (c) 2010-2013 Tokutek Inc. All rights reserved." - -// Here are some timing numbers: -// (Note: The not-quite-working version with cas can be found in r22519 of https://svn.tokutek.com/tokudb/toku/tokudb.2825/) It's about as fast as "Best cas".) -// -// On ramie (2.53GHz E5540) -// Best nop time= 1.074300ns -// Best cas time= 8.595600ns -// Best mutex time= 19.340201ns -// Best rwlock time= 34.024799ns -// Best ft rwlock time= 38.680500ns -// Best prelocked time= 2.148700ns -// Best fair rwlock time= 45.127600ns -// On laptop -// Best nop time= 2.876000ns -// Best cas time= 15.362500ns -// Best mutex time= 51.951498ns -// Best rwlock time= 97.721201ns -// Best ft rwlock time=110.456800ns -// Best prelocked time= 4.240100ns -// Best fair rwlock time=113.119102ns -// -// Analysis: If the mutex can be prelocked (as cachetable does, it uses the same mutex for the cachetable and for the condition variable protecting the cache table) -// then you can save quite a bit. What does the cachetable do? -// During pin: (In the common case:) It grabs the mutex, grabs a read lock, and releases the mutex. -// During unpin: It grabs the mutex, unlocks the rwlock lock in the pair, and releases the mutex. -// Both actions must acquire a cachetable lock during that time, so definitely saves time to do it that way. - -#include <sys/time.h> -#include <string.h> -#include <stdlib.h> -#include <errno.h> -#include <sys/types.h> - -#include <toku_pthread.h> -#include <toku_portability.h> -#include <toku_time.h> -#include <toku_assert.h> -#include <util/rwlock.h> -#include <util/frwlock.h> -#include <portability/toku_atomic.h> -#include "toku_fair_rwlock.h" - -#include "rwlock_condvar.h" - -static int verbose=1; -static int timing_only=0; - -static void parse_args (int argc, const char *argv[]) { - const char *progname = argv[0]; - argc--; argv++; - while (argc>0) { - if (strcmp(argv[0], "-v")==0) { - verbose++; - } else if (strcmp(argv[0], "-q")==0) { - verbose--; - } else if (strcmp(argv[0], "--timing-only")==0) { - timing_only=1; - } else { - fprintf(stderr, "Usage: %s {-q}* {-v}* {--timing-only}\n", progname); - exit(1); - } - argc--; argv++; - } -} - -static const int T=6; -static const int N=10000000; - -static double best_nop_time=1e12; -static double best_fcall_time=1e12; -static double best_cas_time=1e12; -static double best_mutex_time=1e12; -static double best_rwlock_time=1e12; -static double best_ft_time=1e12; -static double best_prelocked_time=1e12; -static double best_cv_fair_rwlock_time=1e12; // fair from condition variables -static double best_fair_rwlock_time=1e12; -static double mind(double a, double b) { if (a<b) return a; else return b; } - -#if 0 -// gcc 4.4.4 (fedora 12) doesn't introduce memory barriers on these writes, so I think that volatile is not enough for sequential consistency. -// Intel guarantees that writes are seen in the same order as they were performed on one processor. But if there were two processors, funny things could happen. -volatile int sc_a, sc_b; -void sequential_consistency (void) { - sc_a = 1; - sc_b = 0; -} -#endif - -// Declaring val to be volatile produces essentially identical code as putting the asm volatile memory statements in. -// gcc is not introducing memory barriers to force sequential consistency on volatile memory writes. -// That's probably good enough for us, since we'll have a barrier instruction anywhere it matters. -volatile int val = 0; - -static -void time_nop (void) { - struct timeval start,end; - for (int t=0; t<T; t++) { - gettimeofday(&start, NULL); - for (int i=0; i<N; i++) { - if (val!=0) abort(); - val=1; - //__asm__ volatile ("" : : : "memory"); - val=0; - //__asm__ volatile ("" : : : "memory"); - } - gettimeofday(&end, NULL); - double diff = 1e9*toku_tdiff(&end, &start)/N; - if (verbose>1) - fprintf(stderr, "nop = %.6fns/(lock+unlock)\n", diff); - best_nop_time=mind(best_nop_time,diff); - } -} - -static -void time_fcall (void) { - struct timeval start,end; - for (int t=0; t<T; t++) { - gettimeofday(&start, NULL); - for (int i=0; i<N; i++) { - fcall_nop(i); - } - gettimeofday(&end, NULL); - double diff = 1e9*toku_tdiff(&end, &start)/N; - if (verbose>1) - fprintf(stderr, "fcall = %.6fns/(lock+unlock)\n", diff); - best_fcall_time=mind(best_fcall_time,diff); - } -} - -static -void time_cas (void) { - volatile int64_t myval = 0; - struct timeval start,end; - for (int t=0; t<T; t++) { - gettimeofday(&start, NULL); - for (int i=0; i<N; i++) { - { int r = toku_sync_val_compare_and_swap(&myval, 0, 1); assert(r==0); } - { int r = toku_sync_val_compare_and_swap(&myval, 1, 0); assert(r==1); } - } - gettimeofday(&end, NULL); - double diff = 1e9*toku_tdiff(&end, &start)/N; - if (verbose>1) - fprintf(stderr, "cas = %.6fns/(lock+unlock)\n", diff); - best_cas_time=mind(best_cas_time,diff); - } -} - - -static -void time_pthread_mutex (void) { - pthread_mutex_t mutex; - { int r = pthread_mutex_init(&mutex, NULL); assert(r==0); } - struct timeval start,end; - pthread_mutex_lock(&mutex); - pthread_mutex_unlock(&mutex); - for (int t=0; t<T; t++) { - gettimeofday(&start, NULL); - for (int i=0; i<N; i++) { - pthread_mutex_lock(&mutex); - pthread_mutex_unlock(&mutex); - } - gettimeofday(&end, NULL); - double diff = 1e9*toku_tdiff(&end, &start)/N; - if (verbose>1) - fprintf(stderr, "pthread_mutex = %.6fns/(lock+unlock)\n", diff); - best_mutex_time=mind(best_mutex_time,diff); - } - { int r = pthread_mutex_destroy(&mutex); assert(r==0); } -} - -static -void time_pthread_rwlock (void) { - pthread_rwlock_t mutex; - { int r = pthread_rwlock_init(&mutex, NULL); assert(r==0); } - struct timeval start,end; - pthread_rwlock_rdlock(&mutex); - pthread_rwlock_unlock(&mutex); - for (int t=0; t<T; t++) { - gettimeofday(&start, NULL); - for (int i=0; i<N; i++) { - pthread_rwlock_rdlock(&mutex); - pthread_rwlock_unlock(&mutex); - } - gettimeofday(&end, NULL); - double diff = 1e9*toku_tdiff(&end, &start)/N; - if (verbose>1) - fprintf(stderr, "pthread_rwlock(r) = %.6fns/(lock+unlock)\n", diff); - best_rwlock_time=mind(best_rwlock_time,diff); - } - { int r = pthread_rwlock_destroy(&mutex); assert(r==0); } -} - -static void ft_rwlock_lock (RWLOCK rwlock, toku_mutex_t *mutex) { - toku_mutex_lock(mutex); - rwlock_read_lock(rwlock, mutex); - toku_mutex_unlock(mutex); -} - -static void ft_rwlock_unlock (RWLOCK rwlock, toku_mutex_t *mutex) { - toku_mutex_lock(mutex); - rwlock_read_unlock(rwlock); - toku_mutex_unlock(mutex); -} - -// Time the read lock that's in ft/rwlock.h -static -void time_ft_rwlock (void) { - struct rwlock rwlock; - toku_mutex_t external_mutex; - toku_mutex_init(&external_mutex, NULL); - rwlock_init(&rwlock); - struct timeval start,end; - - ft_rwlock_lock(&rwlock, &external_mutex); - ft_rwlock_unlock(&rwlock, &external_mutex); - for (int t=0; t<T; t++) { - gettimeofday(&start, NULL); - for (int i=0; i<N; i++) { - ft_rwlock_lock(&rwlock, &external_mutex); - ft_rwlock_unlock(&rwlock, &external_mutex); - } - gettimeofday(&end, NULL); - double diff = 1e9*toku_tdiff(&end, &start)/N; - if (verbose>1) - fprintf(stderr, "ft_rwlock(r) = %.6fns/(lock+unlock)\n", diff); - best_ft_time=mind(best_ft_time,diff); - } - rwlock_destroy(&rwlock); - toku_mutex_destroy(&external_mutex); -} - -// Time the read lock that's in ft/rwlock.h, assuming the mutex is already held. -static -void time_ft_prelocked_rwlock (void) { - struct rwlock rwlock; - toku_mutex_t external_mutex; - toku_mutex_init(&external_mutex, NULL); - toku_mutex_lock(&external_mutex); - rwlock_init(&rwlock); - struct timeval start,end; - - rwlock_read_lock(&rwlock, &external_mutex); - rwlock_read_unlock(&rwlock); - for (int t=0; t<T; t++) { - gettimeofday(&start, NULL); - for (int i=0; i<N; i++) { - rwlock_read_lock(&rwlock, &external_mutex); - rwlock_read_unlock(&rwlock); - } - gettimeofday(&end, NULL); - double diff = 1e9*toku_tdiff(&end, &start)/N; - if (verbose>1) - fprintf(stderr, "ft_rwlock(r) = %.6fns/(lock+unlock)\n", diff); - best_prelocked_time=mind(best_prelocked_time,diff); - } - rwlock_destroy(&rwlock); - toku_mutex_unlock(&external_mutex); - toku_mutex_destroy(&external_mutex); -} - -static -void time_toku_fair_rwlock (void) { - toku_fair_rwlock_t mutex; - toku_fair_rwlock_init(&mutex); - struct timeval start,end; - toku_fair_rwlock_rdlock(&mutex); - toku_fair_rwlock_unlock(&mutex); - for (int t=0; t<T; t++) { - gettimeofday(&start, NULL); - for (int i=0; i<N; i++) { - toku_fair_rwlock_rdlock(&mutex); - toku_fair_rwlock_unlock(&mutex); - } - gettimeofday(&end, NULL); - double diff = 1e9*toku_tdiff(&end, &start)/N; - if (verbose>1) - fprintf(stderr, "pthread_fair(r) = %.6fns/(lock+unlock)\n", diff); - best_fair_rwlock_time=mind(best_fair_rwlock_time,diff); - } - toku_fair_rwlock_destroy(&mutex); -} - -static -void time_toku_cv_fair_rwlock (void) { - toku_cv_fair_rwlock_t mutex; - toku_cv_fair_rwlock_init(&mutex); - struct timeval start,end; - toku_cv_fair_rwlock_rdlock(&mutex); - toku_cv_fair_rwlock_unlock(&mutex); - for (int t=0; t<T; t++) { - gettimeofday(&start, NULL); - for (int i=0; i<N; i++) { - toku_cv_fair_rwlock_rdlock(&mutex); - toku_cv_fair_rwlock_unlock(&mutex); - } - gettimeofday(&end, NULL); - double diff = 1e9*toku_tdiff(&end, &start)/N; - if (verbose>1) - fprintf(stderr, "pthread_fair(r) = %.6fns/(lock+unlock)\n", diff); - best_cv_fair_rwlock_time=mind(best_cv_fair_rwlock_time,diff); - } - toku_cv_fair_rwlock_destroy(&mutex); -} - -#define N 6 -#define T 100000 -#define L 5 -#define N_LOG_ENTRIES (L*N*4) - -static toku_fair_rwlock_t rwlock; -static toku::frwlock frwlock; -static toku_mutex_t fmutex; - -static bool use_frwlock_for_locking; - -static struct log_s { - int threadid, loopid; - char action; -} actionlog[N_LOG_ENTRIES]; -static int log_counter=0; - -static void logit (int threadid, int loopid, char action) { - //printf("%d %d %c\n", threadid, loopid, action); - int my_log_counter = toku_sync_fetch_and_add(&log_counter, 1); - assert(my_log_counter<N_LOG_ENTRIES); - actionlog[my_log_counter].threadid = threadid; - actionlog[my_log_counter].loopid = loopid; - actionlog[my_log_counter].action = action; -} - -// The action should look like this: -// Threads 0-2 are reader threads. -// Threads 3-6 are writer threads. -// The threads all repeatedly grab the lock, wait T steps, and release. -// If the readers can starve the writers, then most of the writers will be at the end. -// If the writers can starve the readers, then most of the readers will be at the end. -// The reader threads all grab the lock, wait T*2 steps, and release the lock. -// The writer threads -// First the writer threads wait time T while the reader threads all go for the lock. -// Before the first one lets go, the writer threads wake up and try to grab the lock. But the readers are still - -// 3 threads (0-2) try to grab the lock all at once. They'll get it. They each sleep for time T*2 -// 3 threads (3-6) try to grab the write lock. They'll get it one after another. - - -static void grab_rdlock (int threadid, int iteration) { - logit(threadid, iteration, 't'); - if (use_frwlock_for_locking) { - toku_mutex_lock(&fmutex); - frwlock.read_lock(); - toku_mutex_unlock(&fmutex); - } - else { int r = toku_fair_rwlock_rdlock(&rwlock); assert(r==0); } - logit(threadid, iteration, 'R'); -} - -static void release_rdlock (int threadid, int iteration) { - logit(threadid, iteration, 'u'); - if (use_frwlock_for_locking) { - toku_mutex_lock(&fmutex); - frwlock.read_unlock(); - toku_mutex_unlock(&fmutex); - } - else { int r = toku_fair_rwlock_unlock(&rwlock); assert(r==0); } -} - -static void grab_wrlock (int threadid, int iteration) { - logit(threadid, iteration, 'T'); - if (use_frwlock_for_locking) { - toku_mutex_lock(&fmutex); - frwlock.write_lock(true); - toku_mutex_unlock(&fmutex); - } - else { int r = toku_fair_rwlock_wrlock(&rwlock); assert(r==0); } - logit(threadid, iteration, 'W'); -} - -static void release_wrlock (int threadid, int iteration) { - logit(threadid, iteration, 'U'); - if (use_frwlock_for_locking) { - toku_mutex_lock(&fmutex); - frwlock.write_unlock(); - toku_mutex_unlock(&fmutex); - } - else { int r = toku_fair_rwlock_unlock(&rwlock); assert(r==0);} -} - -static void *start_thread (void *vv) { - int *vp=(int*)vv; - int v=*vp; - - //printf("T%d=%ld\n", v, pthread_self()); - switch(v) { - case 0: - case 1: - case 2: - for (int i=0; i<L; i++) { - grab_rdlock(v, i); - usleep(T); - release_rdlock(v, i); - } - break; - case 3: - case 4: - case 5: - for (int i=0; i<L; i++) { - grab_wrlock(v, i); - usleep(T); - release_wrlock(v, i); - } - } - return NULL; -} - -static void *start_thread_random (void *vv) { - int *vp=(int*)vv; - int v=*vp; - int wait; - - for (int i=0; i<L; i++) { - if (random()%2==0) { - grab_rdlock(v, i); - wait = random() % 20; - for (int j=0; j<wait; j++) sched_yield(); - release_rdlock(v, i); - wait = random() % 20; - for (int j=0; j<wait; j++) sched_yield(); - } else { - grab_wrlock(v, i); - wait = random() % 20; - for (int j=0; j<wait; j++) sched_yield(); - release_wrlock(v, i); - wait = random() % 20; - for (int j=0; j<wait; j++) sched_yield(); - } - } - return NULL; -} - -static void check_actionlog (int expected_writer_max_count, - int expected_reader_parallelism_min, - int expected_reader_parallelism_max) -// Effect: -// Make sure that writers are exclusive. -// Make sure that anyone who asks for a lock doesn't have one. -// Make sure that anyone granted a lock actually asked for a lock. -// Make sure that anyone who releases a lock has it. -// Make sure that readers don't starve writers, and writers don't starve readers. (Not sure how to code this up...) -{ - int reader_max=0; - int writer_max=0; - int state=0; - char tstate[N]; - for (int i=0; i<N; i++) tstate[i]=0; - for (int i=0; i<log_counter; i++) { - switch (actionlog[i].action) { - case 't': // fall through to 'T' - case 'T': - assert(tstate[actionlog[i].threadid]==0); - tstate[actionlog[i].threadid]=actionlog[i].action; - break; - case 'W': - assert(tstate[actionlog[i].threadid]=='T'); - tstate[actionlog[i].threadid]=actionlog[i].action; - assert(state==0); - state=-1; - writer_max = 1; - break; - case 'U': - assert(tstate[actionlog[i].threadid]=='W'); - tstate[actionlog[i].threadid]=0; - assert(state==-1); - state=0; - break; - case 'R': - assert(tstate[actionlog[i].threadid]=='t'); - tstate[actionlog[i].threadid]=actionlog[i].action; - if (state<0) { printf("On step %d\n", i); } - assert(state>=0); - state++; - if (state>reader_max) reader_max=state; - break; - case 'u': - assert(tstate[actionlog[i].threadid]=='R'); - tstate[actionlog[i].threadid]=0; - assert(state>=0); - state--; - break; - default: - abort(); - } - } - assert(reader_max>=expected_reader_parallelism_min); - assert(reader_max<=expected_reader_parallelism_max); - assert(writer_max==expected_writer_max_count); -} - - -static void test_rwlock_internal (void *(*start_th)(void*), bool use_frwlock, int max_wr, int min_rd, int max_rd) { - if (verbose>=2) printf("Running threads:\n"); - log_counter=0; - pthread_t threads[N]; - int v[N]; - use_frwlock_for_locking = use_frwlock; - if (use_frwlock_for_locking) { - fmutex = TOKU_MUTEX_INITIALIZER; - frwlock.init(&fmutex); - } - else { - toku_fair_rwlock_init(&rwlock); - } - for (int i=0; i<N; i++) { - v[i]=i; - int r = pthread_create(&threads[i], NULL, start_th, &v[i]); - assert(r==0); - } - for (int i=0; i<N; i++) { - void *rv; - int r = pthread_join(threads[i], &rv); - assert(rv==NULL); - assert(r==0); - } - if (verbose>1) { - for (int i=0; i<log_counter; i++) { - printf("%d: %*s%c%d\n", i, actionlog[i].threadid*4, "", actionlog[i].action, actionlog[i].loopid); - } - } - check_actionlog(max_wr, min_rd, max_rd); - if (use_frwlock_for_locking) { - frwlock.deinit(); - toku_mutex_destroy(&fmutex); - } - else { - toku_fair_rwlock_destroy(&rwlock); - } - if (verbose>2) printf("OK\n"); -} - -static void test_rwlock (bool use_frwlock) { - test_rwlock_internal(start_thread, use_frwlock, 1, 2, 3); - for (int i=0; i<10; i++) { - test_rwlock_internal(start_thread_random, use_frwlock, 1, 0, N); - } -} -int main (int argc, const char *argv[]) { - parse_args(argc, argv); - if (timing_only) { - time_nop(); - time_fcall(); - time_cas(); - time_pthread_mutex(); - time_pthread_rwlock(); - time_ft_rwlock(); - time_ft_prelocked_rwlock(); - time_toku_cv_fair_rwlock(); - time_toku_fair_rwlock(); - if (verbose>0) { - printf("// Best nop time=%10.6fns\n", best_nop_time); - printf("// Best fcall time=%10.6fns\n", best_fcall_time); - printf("// Best cas time=%10.6fns\n", best_cas_time); - printf("// Best mutex time=%10.6fns\n", best_mutex_time); - printf("// Best rwlock time=%10.6fns\n", best_rwlock_time); - printf("// Best ft rwlock time=%10.6fns\n", best_ft_time); - printf("// Best prelocked time=%10.6fns\n", best_prelocked_time); - printf("// Best fair cv rwlock time=%10.6fns\n", best_cv_fair_rwlock_time); - printf("// Best fair fast rwlock time=%10.6fns\n", best_fair_rwlock_time); - } - } else { - test_rwlock(true); - test_rwlock(false); - } - return 0; -} - diff --git a/storage/tokudb/ft-index/portability/toku_fair_rwlock.cc b/storage/tokudb/ft-index/portability/toku_fair_rwlock.cc deleted file mode 100644 index d89b49307d1..00000000000 --- a/storage/tokudb/ft-index/portability/toku_fair_rwlock.cc +++ /dev/null @@ -1,361 +0,0 @@ -/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ -// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: - -#ident "$Id$" -/* -COPYING CONDITIONS NOTICE: - - This program is free software; you can redistribute it and/or modify - it under the terms of version 2 of the GNU General Public License as - published by the Free Software Foundation, and provided that the - following conditions are met: - - * Redistributions of source code must retain this COPYING - CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the - DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the - PATENT MARKING NOTICE (below), and the PATENT RIGHTS - GRANT (below). - - * Redistributions in binary form must reproduce this COPYING - CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the - DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the - PATENT MARKING NOTICE (below), and the PATENT RIGHTS - GRANT (below) in the documentation and/or other materials - provided with the distribution. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. - -COPYRIGHT NOTICE: - - TokuDB, Tokutek Fractal Tree Indexing Library. - Copyright (C) 2007-2013 Tokutek, Inc. - -DISCLAIMER: - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - -UNIVERSITY PATENT NOTICE: - - The technology is licensed by the Massachusetts Institute of - Technology, Rutgers State University of New Jersey, and the Research - Foundation of State University of New York at Stony Brook under - United States of America Serial No. 11/760379 and to the patents - and/or patent applications resulting from it. - -PATENT MARKING NOTICE: - - This software is covered by US Patent No. 8,185,551. - This software is covered by US Patent No. 8,489,638. - -PATENT RIGHTS GRANT: - - "THIS IMPLEMENTATION" means the copyrightable works distributed by - Tokutek as part of the Fractal Tree project. - - "PATENT CLAIMS" means the claims of patents that are owned or - licensable by Tokutek, both currently or in the future; and that in - the absence of this license would be infringed by THIS - IMPLEMENTATION or by using or running THIS IMPLEMENTATION. - - "PATENT CHALLENGE" shall mean a challenge to the validity, - patentability, enforceability and/or non-infringement of any of the - PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS. - - Tokutek hereby grants to you, for the term and geographical scope of - the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free, - irrevocable (except as stated in this section) patent license to - make, have made, use, offer to sell, sell, import, transfer, and - otherwise run, modify, and propagate the contents of THIS - IMPLEMENTATION, where such license applies only to the PATENT - CLAIMS. This grant does not include claims that would be infringed - only as a consequence of further modifications of THIS - IMPLEMENTATION. If you or your agent or licensee institute or order - or agree to the institution of patent litigation against any entity - (including a cross-claim or counterclaim in a lawsuit) alleging that - THIS IMPLEMENTATION constitutes direct or contributory patent - infringement, or inducement of patent infringement, then any rights - granted to you under this License shall terminate as of the date - such litigation is filed. If you or your agent or exclusive - licensee institute or order or agree to the institution of a PATENT - CHALLENGE, then Tokutek may terminate any rights granted to you - under this License. -*/ - -#ident "Copyright (c) 2010-2013 Tokutek Inc. All rights reserved." - -#include "toku_config.h" - -#include <pthread.h> -#include <toku_assert.h> -#include "toku_pthread.h" -#include "toku_fair_rwlock.h" - -#include <stdio.h> -#include <memory.h> -#include <portability/toku_atomic.h> - -struct toku_fair_rwlock_waiter_state { - char is_read; - struct toku_fair_rwlock_waiter_state *next; - toku_cond_t cond; -}; - -#if defined(HAVE_GNU_TLS) -static __thread struct toku_fair_rwlock_waiter_state waitstate_var = {0, NULL, { PTHREAD_COND_INITIALIZER } }; -#define GET_WAITSTATE(name) name = &waitstate_var -#else -static pthread_key_t waitstate_key; -static bool waitstate_key_initialized = false; - -void -toku_rwlock_init(void) -{ - assert(!waitstate_key_initialized); - int r = toku_pthread_key_create(&waitstate_key, toku_free); - assert_zero(r); - waitstate_key_initialized = true; -} - -void -toku_rwlock_destroy(void) -{ - assert(waitstate_key_initialized); - int r = toku_pthread_key_delete(waitstate_key); - assert_zero(r); - waitstate_key_initialized = false; -} - -static struct toku_fair_rwlock_waiter_state * -get_waitstate(void) -{ - assert(waitstate_key_initialized); - struct toku_fair_rwlock_waiter_state *waitstate = NULL; - void *p = toku_pthread_getspecific(waitstate_key); - if (!p) { - p = toku_xmalloc(sizeof *waitstate); - int r = toku_pthread_setspecific(waitstate_key, p); - assert_zero(r); - } - waitstate = static_cast<struct toku_fair_rwlock_waiter_state *>(p); - return waitstate; -} - -#define GET_WAITSTATE(name) name = get_waitstate() -#endif - -void toku_fair_rwlock_init (toku_fair_rwlock_t *rwlock) { - rwlock->state=0LL; - rwlock->waiters_head = NULL; - rwlock->waiters_tail = NULL; - toku_mutex_init(&rwlock->mutex, NULL); -} - -void toku_fair_rwlock_destroy (toku_fair_rwlock_t *rwlock) { - assert(rwlock->state==0); // no one can hold the mutex, and no one can hold any lock. - toku_mutex_destroy(&rwlock->mutex); -} - -#ifdef RW_DEBUG -static __thread int tid=-1; -static int next_tid=0; -static int get_tid (void) { - if (tid==-1) { - tid = toku_sync_fetch_and_add(&next_tid, 1); - } - return tid; -} -#define L(l) printf("t%02d %s:%d %s\n", get_tid(), __FILE__, __LINE__, #l) -#define LP(l,s) printf("t%02d %s:%d %s %lx (wlock=%d rcount=%d qcount=%d)\n", get_tid(), __FILE__, __LINE__, #l, s, s_get_wlock(s), s_get_rcount(s), s_get_qcount(s)) -#else -#define L(l) ((void)0) -#define LP(l,s) ((void)s) -#endif - -void foo (void); -void foo (void) { - printf("%llx\n", RWS_QCOUNT_MASK|RWS_WLOCK_MASK); -} - -int toku_fair_rwlock_rdlock_slow (toku_fair_rwlock_t *rwlock) { - uint64_t s; - struct toku_fair_rwlock_waiter_state *GET_WAITSTATE(waitstate); - goto ML; // we start in the ML state. - ML: - toku_mutex_lock(&rwlock->mutex); - goto R2; - R2: - s = rwlock->state; - if (s_get_qcount(s)==0 && !s_get_wlock(s)) goto C2; - else goto C3; - C2: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_incr_rcount(s))) goto MU; - else goto R2; - C3: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_incr_qcount(s))) goto E; - else goto R2; - E: - // Put me into the queue. - if (rwlock->waiters_tail) { - rwlock->waiters_tail->next = waitstate; - } else { - rwlock->waiters_head = waitstate; - } - rwlock->waiters_tail = waitstate; - waitstate->next = NULL; - waitstate->is_read = 1; - goto W; - W: - toku_cond_wait(&waitstate->cond, &rwlock->mutex); - // must wait till we are at the head of the queue because of the possiblity of spurious wakeups. - if (rwlock->waiters_head==waitstate) goto D; - else goto W; - D: - rwlock->waiters_head = waitstate->next; - if (waitstate->next==NULL) { - rwlock->waiters_tail = NULL; - } - goto WN; - WN: - // If the next guy is a reader then wake him up. - if (waitstate->next!=NULL && waitstate->next->is_read) { - toku_cond_signal(&rwlock->waiters_head->cond); - } - goto R4; - R4: - s = rwlock->state; - goto C4; - C4: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_incr_rcount(s_decr_qcount(s)))) goto MU; - else goto R4; - MU: - toku_mutex_unlock(&rwlock->mutex); - goto DONE; - DONE: - return 0; -} - -int toku_fair_rwlock_wrlock_slow (toku_fair_rwlock_t *rwlock) { - uint64_t s; - struct toku_fair_rwlock_waiter_state *GET_WAITSTATE(waitstate); - goto ML; - ML: - L(ML); - toku_mutex_lock(&rwlock->mutex); - goto R2; - R2: - s = rwlock->state; - LP(R2, s); - if (s_get_qcount(s)==0 && !s_get_wlock(s) && s_get_rcount(s)==0) goto C2; - else goto C3; - C2: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_set_wlock(s))) goto MU; - else goto R2; - C3: - L(C3); - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_incr_qcount(s))) goto E; - else goto R2; - E: - LP(E, rwlock->state); - // Put me into the queue. - if (rwlock->waiters_tail) { - rwlock->waiters_tail->next = waitstate; - } else { - rwlock->waiters_head = waitstate; - } - rwlock->waiters_tail = waitstate; - waitstate->next = NULL; - waitstate->is_read = 0; - goto W; - W: - toku_cond_wait(&waitstate->cond, &rwlock->mutex); - // must wait till we are at the head of the queue because of the possiblity of spurious wakeups. - if (rwlock->waiters_head==waitstate) goto D; - else goto W; - D: - rwlock->waiters_head = waitstate->next; - if (waitstate->next==NULL) { - rwlock->waiters_tail = NULL; - } - goto R4; - R4: - s = rwlock->state; - assert(!s_get_wlock(s)); - goto C4; - C4: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_set_wlock(s_decr_qcount(s)))) goto MU; - else goto R4; - MU: - toku_mutex_unlock(&rwlock->mutex); - goto DONE; - DONE: - return 0; -} - -int toku_fair_rwlock_unlock_r_slow (toku_fair_rwlock_t *rwlock) { - uint64_t s; - goto ML; - ML: - toku_mutex_lock(&rwlock->mutex); - goto R2; - R2: - s = rwlock->state; - LP(R2, s); - if (s_get_rcount(s)>1 || s_get_qcount(s)==0) goto C2; - else goto C3; - C2: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_decr_rcount(s))) goto MU; - else goto R2; - C3: - // rcount==1 and qcount>0 - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_decr_rcount(s))) goto WN; - else goto R2; - WN: - LP(WN, rwlock->state); - toku_cond_signal(&rwlock->waiters_head->cond); - goto MU; - MU: - toku_mutex_unlock(&rwlock->mutex); - goto DONE; - DONE: - return 0; -} - -int toku_fair_rwlock_unlock_w_slow (toku_fair_rwlock_t *rwlock) { - uint64_t s; - //assert(s_get_rcount(s)==0 && s_get_wlock(s)); - goto ML; - ML: - toku_mutex_lock(&rwlock->mutex); - goto R2; - R2: - LP(R2, rwlock->state); - s = rwlock->state; - if (s_get_qcount(s)==0) goto C2; - else goto C3; - C2: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_clear_wlock(s))) goto MU; - else goto R2; - C3: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_clear_wlock(s))) goto WN; - else goto R2; - WN: - LP(WN, rwlock->state); - toku_cond_signal(&rwlock->waiters_head->cond); - goto MU; - MU: - toku_mutex_unlock(&rwlock->mutex); - goto DONE; - DONE: - return 0; -} - -// This function is defined so we can measure the cost of a function call. -int fcall_nop (int i) { - return i; -} diff --git a/storage/tokudb/ft-index/portability/toku_fair_rwlock.h b/storage/tokudb/ft-index/portability/toku_fair_rwlock.h deleted file mode 100644 index 92793b1444d..00000000000 --- a/storage/tokudb/ft-index/portability/toku_fair_rwlock.h +++ /dev/null @@ -1,237 +0,0 @@ -/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ -// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: -#ident "$Id$" -/* -COPYING CONDITIONS NOTICE: - - This program is free software; you can redistribute it and/or modify - it under the terms of version 2 of the GNU General Public License as - published by the Free Software Foundation, and provided that the - following conditions are met: - - * Redistributions of source code must retain this COPYING - CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the - DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the - PATENT MARKING NOTICE (below), and the PATENT RIGHTS - GRANT (below). - - * Redistributions in binary form must reproduce this COPYING - CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the - DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the - PATENT MARKING NOTICE (below), and the PATENT RIGHTS - GRANT (below) in the documentation and/or other materials - provided with the distribution. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. - -COPYRIGHT NOTICE: - - TokuDB, Tokutek Fractal Tree Indexing Library. - Copyright (C) 2007-2013 Tokutek, Inc. - -DISCLAIMER: - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - -UNIVERSITY PATENT NOTICE: - - The technology is licensed by the Massachusetts Institute of - Technology, Rutgers State University of New Jersey, and the Research - Foundation of State University of New York at Stony Brook under - United States of America Serial No. 11/760379 and to the patents - and/or patent applications resulting from it. - -PATENT MARKING NOTICE: - - This software is covered by US Patent No. 8,185,551. - This software is covered by US Patent No. 8,489,638. - -PATENT RIGHTS GRANT: - - "THIS IMPLEMENTATION" means the copyrightable works distributed by - Tokutek as part of the Fractal Tree project. - - "PATENT CLAIMS" means the claims of patents that are owned or - licensable by Tokutek, both currently or in the future; and that in - the absence of this license would be infringed by THIS - IMPLEMENTATION or by using or running THIS IMPLEMENTATION. - - "PATENT CHALLENGE" shall mean a challenge to the validity, - patentability, enforceability and/or non-infringement of any of the - PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS. - - Tokutek hereby grants to you, for the term and geographical scope of - the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free, - irrevocable (except as stated in this section) patent license to - make, have made, use, offer to sell, sell, import, transfer, and - otherwise run, modify, and propagate the contents of THIS - IMPLEMENTATION, where such license applies only to the PATENT - CLAIMS. This grant does not include claims that would be infringed - only as a consequence of further modifications of THIS - IMPLEMENTATION. If you or your agent or licensee institute or order - or agree to the institution of patent litigation against any entity - (including a cross-claim or counterclaim in a lawsuit) alleging that - THIS IMPLEMENTATION constitutes direct or contributory patent - infringement, or inducement of patent infringement, then any rights - granted to you under this License shall terminate as of the date - such litigation is filed. If you or your agent or exclusive - licensee institute or order or agree to the institution of a PATENT - CHALLENGE, then Tokutek may terminate any rights granted to you - under this License. -*/ - -#ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved." -#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it." - -#include "toku_pthread.h" -#include <portability/toku_atomic.h> - -// Fair readers/writer locks. These are fair (meaning first-come first-served. No reader starvation, and no writer starvation). And they are -// probably faster than the linux readers/writer locks (pthread_rwlock_t). -struct toku_fair_rwlock_waiter_state; // this structure is used internally. -typedef struct toku_fair_rwlock_s { - // Try to put enough state into STATE so that in many cases, a compare-and-swap will work. - // The 64-bit state bits are as follows: - // bit 0: 1 iff someone has exclusive ownership of the entire state. (This is a spin lock). - // bit 1: 1 iff someone holds a write lock. - // bit 2: 1 iff the queue is not empty (if the queue is not empty, then you cannot use the fast path.) - // bits 3--31 how many read locks are held. - uint64_t volatile state; - // The waiters each provide a condition variable. This is the mutex they are all using. - // If anyone holds this mutex, they must set the RWS_MUTEXED bit first. Then they grab the mutex. Then they clear the bit indicating that they don't have the mutex. No one else can change anything while the RWS_MUTEXED bit is set. - toku_mutex_t mutex; - struct toku_fair_rwlock_waiter_state *waiters_head, *waiters_tail; -} toku_fair_rwlock_t; - -void toku_rwlock_init(void) __attribute__((constructor)); -void toku_rwlock_destroy(void) __attribute__((destructor)); - -#define RWS_WLOCK_MASK 1LL - -#define RWS_QCOUNT_OFF 1 -#define RWS_QCOUNT_LEN 31 -#define RWS_QCOUNT_INCR 2LL -#define RWS_QCOUNT_MASK (((1L<<RWS_QCOUNT_LEN)-1)<<RWS_QCOUNT_OFF) - -#define RWS_RCOUNT_OFF (RWS_QCOUNT_OFF+RWS_QCOUNT_LEN) -#define RWS_RCOUNT_LEN 31 -#define RWS_RCOUNT_INCR (1LL<<32) - -static inline int s_get_wlock(uint64_t s) { - return (s&RWS_WLOCK_MASK)!=0; -} -static inline unsigned int s_get_qcount(uint64_t s) { - return (s>>RWS_QCOUNT_OFF)&((1LL<<RWS_QCOUNT_LEN)-1); -} -static inline unsigned int s_get_rcount(uint64_t s) { - return (s>>RWS_RCOUNT_OFF)&((1LL<<RWS_RCOUNT_LEN)-1); -} - -static inline uint64_t s_set_wlock (uint64_t s) { - return s | RWS_WLOCK_MASK; -} -static inline uint64_t s_clear_wlock (uint64_t s) { - return s & ~RWS_WLOCK_MASK; -} -static inline uint64_t s_incr_qcount (uint64_t s) { - //printf("%s:%d (%s) s=%lx, get_qcount=%d 1u<<%d=%u\n", __FILE__, __LINE__, __FUNCTION__, s, s_get_qcount(s), RWS_QCOUNT_LEN, 1u<<RWS_QCOUNT_LEN); - //assert(s_get_qcount(s)+1 < (1u<<RWS_QCOUNT_LEN)); - return s+RWS_QCOUNT_INCR; -} -static inline uint64_t s_decr_qcount (uint64_t s) { - //assert(s_get_qcount(s) > 0); - return s-RWS_QCOUNT_INCR; -} -static inline uint64_t s_incr_rcount (uint64_t s) { - //assert(s_get_rcount(s)+1 < (1u<<RWS_RCOUNT_LEN)); - return s+RWS_RCOUNT_INCR; -} -static inline uint64_t s_decr_rcount (uint64_t s) { - //assert(s_get_rcount(s) > 0); - return s-RWS_RCOUNT_INCR; -} - -void toku_fair_rwlock_init (toku_fair_rwlock_t *rwlock); -void toku_fair_rwlock_destroy (toku_fair_rwlock_t *rwlock); -int toku_fair_rwlock_rdlock_slow (toku_fair_rwlock_t *rwlock); // this is the slow internal version that grabs the mutex. - -// Inline the fast path to avoid function call overhead. -static inline int toku_fair_rwlock_rdlock (toku_fair_rwlock_t *rwlock) { - uint64_t s = rwlock->state; - START: - s = rwlock->state; - if (0==(s&(RWS_QCOUNT_MASK | RWS_WLOCK_MASK))) goto C1; - //if (s_get_qcount(s)==0 && !s_get_wlock(s)) goto C1; - else goto ML; - C1: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_incr_rcount(s))) goto DONE; - else goto START; - DONE: - return 0; - ML: - return toku_fair_rwlock_rdlock_slow(rwlock); -} - -int toku_fair_rwlock_wrlock_slow (toku_fair_rwlock_t *rwlock); - -// Inline the fast path to avoid function call overhead. -static inline int toku_fair_rwlock_wrlock (toku_fair_rwlock_t *rwlock) { - uint64_t s; - START: - s = rwlock->state; - if (s_get_qcount(s)==0 && !s_get_wlock(s) && s_get_rcount(s)==0) goto C1; - else goto ML; - C1: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_set_wlock(s))) goto DONE; - else goto START; - DONE: - return 0; - ML: - return toku_fair_rwlock_wrlock_slow(rwlock); -} - -int toku_fair_rwlock_unlock_r_slow (toku_fair_rwlock_t *rwlock); -int toku_fair_rwlock_unlock_w_slow (toku_fair_rwlock_t *rwlock); - -static inline int toku_fair_rwlock_unlock (toku_fair_rwlock_t *rwlock) { - uint64_t s; - s = rwlock->state; - if (s_get_wlock(s)) { - goto wSTART0; // we already have s. - wSTART: - s = rwlock->state; - goto wSTART0; - wSTART0: - if (s_get_qcount(s)==0) goto wC1; - else goto wML; - wC1: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_clear_wlock(s))) goto wDONE; - else goto wSTART; - wDONE: - return 0; - wML: - return toku_fair_rwlock_unlock_w_slow (rwlock); - } else { - goto rSTART0; // we already have s. - rSTART: - s = rwlock->state; - goto rSTART0; - rSTART0: - if (s_get_rcount(s)>1 || s_get_qcount(s)==0) goto rC1; - else goto rML; - rC1: - if (toku_sync_bool_compare_and_swap(&rwlock->state, s, s_decr_rcount(s))) goto rDONE; - else goto rSTART; - rDONE: - return 0; - rML: - return toku_fair_rwlock_unlock_r_slow (rwlock); - } -} -int fcall_nop(int); |