From 470cf1f668f81f8f63fef4b1df36855585ec2d22 Mon Sep 17 00:00:00 2001 From: Bomin Zhang Date: Wed, 15 Jul 2020 13:53:49 +0800 Subject: [PATCH] td-928: thread-safe 'copy on read' also merge 'reference counting' and 'rwlatch' into 'lock-free' --- src/query/inc/qExecutor.h | 2 +- src/tsdb/inc/tsdbMain.h | 3 +- src/util/inc/tcache.h | 2 +- src/util/inc/{tref.h => tlockfree.h} | 56 ++++++++++++++++++++++-- src/util/inc/trwlatch.h | 36 --------------- src/util/src/{trwlatch.c => tlockfree.c} | 3 +- 6 files changed, 56 insertions(+), 46 deletions(-) rename src/util/inc/{tref.h => tlockfree.h} (66%) delete mode 100644 src/util/inc/trwlatch.h rename src/util/src/{trwlatch.c => tlockfree.c} (97%) diff --git a/src/query/inc/qExecutor.h b/src/query/inc/qExecutor.h index cdf5f9612c..f77036ef9d 100644 --- a/src/query/inc/qExecutor.h +++ b/src/query/inc/qExecutor.h @@ -24,7 +24,7 @@ #include "qtsbuf.h" #include "taosdef.h" #include "tarray.h" -#include "tref.h" +#include "tlockfree.h" #include "tsdb.h" #include "tsqlfunction.h" #include "query.h" diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index bfc1aa8932..f178383767 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -21,11 +21,10 @@ #include "tkvstore.h" #include "tlist.h" #include "tlog.h" -#include "tref.h" +#include "tlockfree.h" #include "tsdb.h" #include "tskiplist.h" #include "tutil.h" -#include "trwlatch.h" #ifdef __cplusplus extern "C" { diff --git a/src/util/inc/tcache.h b/src/util/inc/tcache.h index 3da604d152..5a3545fd8f 100644 --- a/src/util/inc/tcache.h +++ b/src/util/inc/tcache.h @@ -21,7 +21,7 @@ extern "C" { #endif #include "os.h" -#include "tref.h" +#include "tlockfree.h" #include "hash.h" typedef void (*__cache_free_fn_t)(void*); diff --git a/src/util/inc/tref.h b/src/util/inc/tlockfree.h similarity index 66% rename from src/util/inc/tref.h rename to src/util/inc/tlockfree.h index 0503325326..e425d71d27 100644 --- a/src/util/inc/tref.h +++ b/src/util/inc/tlockfree.h @@ -12,12 +12,17 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - -#ifndef TDENGINE_TREF_H -#define TDENGINE_TREF_H +#ifndef __TD_LOCK_FREE_H__ +#define __TD_LOCK_FREE_H__ #include "os.h" +#ifdef __cplusplus +extern "C" { +#endif + + +// reference counting typedef void (*_ref_fn_t)(const void* pObj); #define T_REF_DECLARE() \ @@ -55,4 +60,47 @@ typedef void (*_ref_fn_t)(const void* pObj); #define T_REF_VAL_GET(x) (x)->_ref.val -#endif // TDENGINE_TREF_H + + +// single writer multiple reader lock +typedef int32_t SRWLatch; + +void taosInitRWLatch(SRWLatch *pLatch); +void taosWLockLatch(SRWLatch *pLatch); +void taosWUnLockLatch(SRWLatch *pLatch); +void taosRLockLatch(SRWLatch *pLatch); +void taosRUnLockLatch(SRWLatch *pLatch); + + + +// copy on read +#define taosCorBeginRead(x) for (uint32_t i_ = 1; 1; ++i_) { \ + int32_t old_ = atomic_load_32(x); \ + if (old_ & 0x00000001) { \ + if (i_ % 1000 == 0) { \ + sched_yield(); \ + } \ + continue; \ + } + +#define taosCorEndRead(x) \ + if (atomic_load_32(x) == old_) { \ + break; \ + } \ + } + +#define taosCorBeginWrite(x) taosCorBeginRead(x) \ + if (atomic_val_compare_exchange_32((x), old_, old_ + 1) != old_) { \ + continue; \ + } + +#define taosCorEndWrite(x) atomic_add_fetch_32((x), 1); \ + break; \ + } + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/util/inc/trwlatch.h b/src/util/inc/trwlatch.h deleted file mode 100644 index c6923f0e90..0000000000 --- a/src/util/inc/trwlatch.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * 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. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -#ifndef __TD_RWLATCH_H__ -#define __TD_RWLATCH_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -typedef int32_t SRWLatch; - -void taosInitRWLatch(SRWLatch *pLatch); -void taosWLockLatch(SRWLatch *pLatch); -void taosWUnLockLatch(SRWLatch *pLatch); -void taosRLockLatch(SRWLatch *pLatch); -void taosRUnLockLatch(SRWLatch *pLatch); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/util/src/trwlatch.c b/src/util/src/tlockfree.c similarity index 97% rename from src/util/src/trwlatch.c rename to src/util/src/tlockfree.c index cc027aa3df..3161518a36 100644 --- a/src/util/src/trwlatch.c +++ b/src/util/src/tlockfree.c @@ -15,8 +15,7 @@ // #define _GNU_SOURCE // #include -#include "trwlatch.h" -#include "os.h" +#include "tlockfree.h" #define TD_RWLATCH_WRITE_FLAG 0x40000000 -- GitLab