TDengine/include/util/tlockfree.h
Pan Wei 9a1694b41a
fix: memory leak and trigger redeploy issue (#32398)
* enh: add operator reset func

* fix: merge join reset issue

* fix: memory issues

* fix: add debug assert

* fix: memory issues

* fix: memory leak

* fix: memory issues

* fix taos log miss

* fix: case issue

* fix: case issue

* fix: case issues

* fix: drop dnode issue

* fix: memory issues

* fix: memory issues

* fix: memory leak issues

* fix: recalculate time range issue

* fix: add debug log

* fix: memory issues

* fix: enable case asan

* Update streamlist_for_ci.task

* fix: case asan issue

* fix: stream name issue

* fix: external window compile issues

* fix: deploy memory issue

* fix: ahandle issue

* fix: ahandle issue

* fix: ahandle issue

* fix: virtual table reader list issue

* fix: log info

* fix: msg error

* fix: virtual table addr list issue

* fix: memory issues

* fix: memory leak issue

* fix: memory issues

* fix: memory free issues

* fix: memory issues

* fix: snode deploy issue

* fix: mnode reader issue

* fix: memory issues

* fix: add debug test

* enh: add ignore nodata trigger

* fix: memory leaks

* fix: configuration issue

* fix: memory issue

* fix: external window issue

* fix: external window issues

* fix: external window placeholder issue

* fix: placeholder function init issues

* fix: memory leak issue

* fix: add debug log

* fix: compile issues

* fix: double free issue

* fix: runner addr update issue

* fix: msg rsp issue

* fix: external window reset issue

* fix: configuration issue

* fix: deploy msg issue

* fix: compile issue

* fix: external window idx issue

* fix: ci issues

* fix: ci case issues

* fix: drop dnode issue

* fix: add debug log

* fix: conflict

* fix: create stream if not exists issue

* fix: ahandle memory leak

* fix: case issue

* fix: exchange issues

* fix: crash issue

* fix: exchange prefetch issue

* fix: snode quit issue

* enh: support indef rows func

* fix: crash issues

* Fix external window collect vector function

* fix: external window indef rows issues

* fix: external window issue

* enh: support count always return value in external window

* fix: force output when has more result block

* fix: runner block retrieve issue

* fix: crash issue

* fix: count cases issue

* fix: reader deploy message issue

* fix: task deploy issue

* fix: external window scalar issue

* fix: compile issue

* fix: group cache reset issue

* fix: add protection check

* fix: add grant check

* fix: add disableStream config

* fix: notify free issue

* fix: case issue

* fix: grant issues

* fix: memory leak issue

* fix: memory leak issue

* fix: memory leak issue

* fix: stbJoin issue

* fix: rpc send issue

* fix: rsp stream group id issue

* fix: redeploy stream issue

* fix: cases issues

* fix: memory leak issue

* fix: snode quit issue

* fix: invalid read issue

* fix: memory leak issue

* fix: split dead loop issue

* fix: crash issue

* fix: acquire task issue

* fix: memory leak and trigger redeploy issue

* fix: taosd crash issue

* fix: return code issues

* fix: return code issues

* fix: return code issue

* fix: return code issues

* fix: return code issue

* fix: mnode crash and dead lock issue

* fix: memory and crash issues

---------

Co-authored-by: huohong <sallyhuo@taosdata.com>
Co-authored-by: Jing Sima <simondominic9997@outlook.com>
Co-authored-by: facetosea <285808407@qq.com>
2025-08-12 08:39:14 +08:00

113 lines
3.7 KiB
C

/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_UTIL_LOCK_FREE_H_
#define _TD_UTIL_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() \
struct { \
volatile int32_t val; \
} _ref;
#define T_REF_REGISTER_FUNC(s, e) \
struct { \
_ref_fn_t start; \
_ref_fn_t end; \
} _ref_func = {.begin = (s), .end = (e)};
// set the initial reference count value
#define T_REF_INIT_VAL(x, _v) \
do { \
atomic_store_32(&((x)->_ref.val), (_v)); \
} while (0)
// increase the reference count by 1
#define T_REF_INC(x) (atomic_add_fetch_32(&((x)->_ref.val), 1))
#define T_REF_INC_WITH_CB(x, p) \
do { \
int32_t v = atomic_add_fetch_32(&((x)->_ref.val), 1); \
if (v == 1 && (p)->_ref_func.begin != NULL) { \
(p)->_ref_func.begin((x)); \
} \
} while (0)
#define T_REF_DEC(x) (atomic_sub_fetch_32(&((x)->_ref.val), 1))
#define T_REF_DEC_WITH_CB(x, p) \
do { \
int32_t v = atomic_sub_fetch_32(&((x)->_ref.val), 1); \
if (v == 0 && (p)->_ref_func.end != NULL) { \
(p)->_ref_func.end((x)); \
} \
} while (0)
#define T_REF_VAL_GET(x) (x)->_ref.val
// single writer multiple reader lock
typedef volatile int32_t SRWLatch;
void taosInitRWLatch(SRWLatch *pLatch);
void taosWLockLatch(SRWLatch *pLatch);
void taosWWaitLockLatch(SRWLatch *pLatch);
void taosWUnLockLatch(SRWLatch *pLatch);
void taosRLockLatch(SRWLatch *pLatch);
int32_t taosRTryLockLatch(SRWLatch *pLatch);
void taosRUnLockLatch(SRWLatch *pLatch);
int32_t taosRUnLockLatch_r(SRWLatch *pLatch);
int32_t taosWTryLockLatch(SRWLatch *pLatch);
bool taosIsOnlyWLocked(SRWLatch *pLatch);
bool taosHasRWWFlag(SRWLatch *pLatch);
int32_t taosWTryForceLockLatch(SRWLatch *pLatch);
// copy on read
#define taosCorBeginRead(x) \
for (uint32_t i_ = 1; 1; ++i_) { \
int32_t old_ = atomic_add_fetch_32((x), 0); \
if (old_ & 0x00000001) { \
if (i_ % 1000 == 0) { \
(void)sched_yield(); \
} \
continue; \
}
#define taosCorEndRead(x) \
if (atomic_add_fetch_32((x), 0) == old_) { \
break; \
} \
}
#define taosCorBeginWrite(x) \
taosCorBeginRead(x) if (atomic_val_compare_exchange_32((x), old_, old_ + 1) != old_) { continue; }
#define taosCorEndWrite(x) \
(void)atomic_add_fetch_32((x), 1); \
break; \
}
#ifdef __cplusplus
}
#endif
#endif /*_TD_UTIL_LOCK_FREE_H_*/