diff --git a/src/query/inc/qExecutor.h b/src/query/inc/qExecutor.h index cdf5f9612cf192cc5ba445f232e408531cfc3afa..f77036ef9d75d8297534a3522a1988d685de144e 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 bfc1aa89322353316783cfc37f0219e6f601b847..f17838376724733b9f0980b5f2f94c063e71a6b1 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 3da604d15298ffeec085c5efcfc3cc8ed856fdea..5a3545fd8f20169f7dc5a7a57f2994db40337ef2 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 0503325326ab90837de9c1990de44db45548b946..e425d71d270bd53a891e3585e12a2b762e8a81f6 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 c6923f0e9028bb2ee062a6dd9314b75ef952d81a..0000000000000000000000000000000000000000 --- 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 cc027aa3df4f14064ba668271a872a2f967ded39..3161518a3642632486a1a746e4cebb30f2e81b72 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