From 7747ab232401bc1e935e667d22f8c0c05537b613 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 28 Feb 2022 14:18:13 +0800 Subject: [PATCH] ref --- include/util/tref.h | 30 ++++---- source/util/src/tref.c | 154 +++++++++++++++++++---------------------- 2 files changed, 85 insertions(+), 99 deletions(-) diff --git a/include/util/tref.h b/include/util/tref.h index 6680204d63..7e08bb045b 100644 --- a/include/util/tref.h +++ b/include/util/tref.h @@ -14,8 +14,8 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_REF_H -#define _TD_UTIL_REF_H +#ifndef _TD_UTIL_REF_H_ +#define _TD_UTIL_REF_H_ #include "os.h" @@ -25,45 +25,45 @@ extern "C" { // open a reference set, max is the mod used by hash, fp is the pointer to free resource function // return rsetId which will be used by other APIs. On error, -1 is returned, and terrno is set appropriately -int taosOpenRef(int max, void (*fp)(void *)); +int32_t taosOpenRef(int32_t max, void (*fp)(void *)); // close the reference set, refId is the return value by taosOpenRef // return 0 if success. On error, -1 is returned, and terrno is set appropriately -int taosCloseRef(int refId); +int32_t taosCloseRef(int32_t refId); // add ref, p is the pointer to resource or pointer ID // return Reference ID(rid) allocated. On error, -1 is returned, and terrno is set appropriately -int64_t taosAddRef(int refId, void *p); +int64_t taosAddRef(int32_t refId, void *p); // remove ref, rid is the reference ID returned by taosAddRef // return 0 if success. On error, -1 is returned, and terrno is set appropriately -int taosRemoveRef(int rsetId, int64_t rid); +int32_t taosRemoveRef(int32_t rsetId, int64_t rid); // acquire ref, rid is the reference ID returned by taosAddRef // return the resource p. On error, NULL is returned, and terrno is set appropriately -void *taosAcquireRef(int rsetId, int64_t rid); +void *taosAcquireRef(int32_t rsetId, int64_t rid); // release ref, rid is the reference ID returned by taosAddRef // return 0 if success. On error, -1 is returned, and terrno is set appropriately -int taosReleaseRef(int rsetId, int64_t rid); +int32_t taosReleaseRef(int32_t rsetId, int64_t rid); -// return the first reference if rid is 0, otherwise return the next after current reference. +// return the first reference if rid is 0, otherwise return the next after current reference. // if return value is NULL, it means list is over(if terrno is set, it means error happens) -void *taosIterateRef(int rsetId, int64_t rid); +void *taosIterateRef(int32_t rsetId, int64_t rid); // return the number of references in system -int taosListRef(); +int32_t taosListRef(); #define RID_VALID(x) ((x) > 0) -/* sample code to iterate the refs +/* sample code to iterate the refs -void demoIterateRefs(int rsetId) { +void demoIterateRefs(int32_t rsetId) { void *p = taosIterateRef(refId, 0); while (p) { // process P - + // get the rid from p p = taosIterateRef(rsetId, rid); @@ -76,4 +76,4 @@ void demoIterateRefs(int rsetId) { } #endif -#endif /*_TD_UTIL_REF_H*/ +#endif /*_TD_UTIL_REF_H_*/ diff --git a/source/util/src/tref.c b/source/util/src/tref.c index ca4388ec26..a9f6c21bf8 100644 --- a/source/util/src/tref.c +++ b/source/util/src/tref.c @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#include "os.h" - +#define _DEFAULT_SOURCE +#include "tref.h" #include "taoserror.h" #include "tlog.h" #include "tutil.h" @@ -25,49 +25,48 @@ #define TSDB_REF_STATE_DELETED 2 typedef struct SRefNode { - struct SRefNode *prev; // previous node - struct SRefNode *next; // next node - void *p; // pointer to resource protected, - int64_t rid; // reference ID - int32_t count; // number of references - int removed; // 1: removed + struct SRefNode *prev; // previous node + struct SRefNode *next; // next node + void *p; // pointer to resource protected, + int64_t rid; // reference ID + int32_t count; // number of references + int32_t removed; // 1: removed } SRefNode; - + typedef struct { - SRefNode **nodeList; // array of SRefNode linked list - int state; // 0: empty, 1: active; 2: deleted - int rsetId; // refSet ID, global unique - int64_t rid; // increase by one for each new reference - int max; // mod - int32_t count; // total number of SRefNodes in this set + SRefNode **nodeList; // array of SRefNode linked list + int32_t state; // 0: empty, 1: active; 2: deleted + int32_t rsetId; // refSet ID, global unique + int64_t rid; // increase by one for each new reference + int32_t max; // mod + int32_t count; // total number of SRefNodes in this set int64_t *lockedBy; - void (*fp)(void *); + void (*fp)(void *); } SRefSet; static SRefSet tsRefSetList[TSDB_REF_OBJECTS]; static pthread_once_t tsRefModuleInit = PTHREAD_ONCE_INIT; static pthread_mutex_t tsRefMutex; -static int tsRefSetNum = 0; -static int tsNextId = 0; +static int32_t tsRefSetNum = 0; +static int32_t tsNextId = 0; -static void taosInitRefModule(void); -static void taosLockList(int64_t *lockedBy); -static void taosUnlockList(int64_t *lockedBy); -static void taosIncRsetCount(SRefSet *pSet); -static void taosDecRsetCount(SRefSet *pSet); -static int taosDecRefCount(int rsetId, int64_t rid, int remove); +static void taosInitRefModule(void); +static void taosLockList(int64_t *lockedBy); +static void taosUnlockList(int64_t *lockedBy); +static void taosIncRsetCount(SRefSet *pSet); +static void taosDecRsetCount(SRefSet *pSet); +static int32_t taosDecRefCount(int32_t rsetId, int64_t rid, int32_t remove); -int taosOpenRef(int max, void (*fp)(void *)) -{ +int32_t taosOpenRef(int32_t max, void (*fp)(void *)) { SRefNode **nodeList; SRefSet *pSet; int64_t *lockedBy; - int i, rsetId; + int32_t i, rsetId; pthread_once(&tsRefModuleInit, taosInitRefModule); nodeList = calloc(sizeof(SRefNode *), (size_t)max); - if (nodeList == NULL) { + if (nodeList == NULL) { terrno = TSDB_CODE_REF_NO_MEMORY; return -1; } @@ -83,7 +82,7 @@ int taosOpenRef(int max, void (*fp)(void *)) for (i = 0; i < TSDB_REF_OBJECTS; ++i) { tsNextId = (tsNextId + 1) % TSDB_REF_OBJECTS; - if (tsNextId == 0) tsNextId = 1; // dont use 0 as rsetId + if (tsNextId == 0) tsNextId = 1; // dont use 0 as rsetId if (tsRefSetList[tsNextId].state == TSDB_REF_STATE_EMPTY) break; } @@ -103,8 +102,8 @@ int taosOpenRef(int max, void (*fp)(void *)) uTrace("rsetId:%d is opened, max:%d, fp:%p refSetNum:%d", rsetId, max, fp, tsRefSetNum); } else { rsetId = TSDB_CODE_REF_FULL; - free (nodeList); - free (lockedBy); + free(nodeList); + free(lockedBy); uTrace("run out of Ref ID, maximum:%d refSetNum:%d", TSDB_REF_OBJECTS, tsRefSetNum); } @@ -113,10 +112,9 @@ int taosOpenRef(int max, void (*fp)(void *)) return rsetId; } -int taosCloseRef(int rsetId) -{ - SRefSet *pSet; - int deleted = 0; +int32_t taosCloseRef(int32_t rsetId) { + SRefSet *pSet; + int32_t deleted = 0; if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) { uTrace("rsetId:%d is invalid, out of range", rsetId); @@ -143,9 +141,8 @@ int taosCloseRef(int rsetId) return 0; } -int64_t taosAddRef(int rsetId, void *p) -{ - int hash; +int64_t taosAddRef(int32_t rsetId, void *p) { + int32_t hash; SRefNode *pNode; SRefSet *pSet; int64_t rid = 0; @@ -173,7 +170,7 @@ int64_t taosAddRef(int rsetId, void *p) rid = atomic_add_fetch_64(&pSet->rid, 1); hash = rid % pSet->max; - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode->p = p; pNode->rid = rid; @@ -186,26 +183,22 @@ int64_t taosAddRef(int rsetId, void *p) uTrace("rsetId:%d p:%p rid:%" PRId64 " is added, count:%d", rsetId, p, rid, pSet->count); - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); return rid; } -int taosRemoveRef(int rsetId, int64_t rid) -{ - return taosDecRefCount(rsetId, rid, 1); -} +int32_t taosRemoveRef(int32_t rsetId, int64_t rid) { return taosDecRefCount(rsetId, rid, 1); } // if rid is 0, return the first p in hash list, otherwise, return the next after current rid -void *taosAcquireRef(int rsetId, int64_t rid) -{ - int hash; +void *taosAcquireRef(int32_t rsetId, int64_t rid) { + int32_t hash; SRefNode *pNode; SRefSet *pSet; void *p = NULL; if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) { - //uTrace("rsetId:%d rid:%" PRId64 " failed to acquire, rsetId not valid", rsetId, rid); + // uTrace("rsetId:%d rid:%" PRId64 " failed to acquire, rsetId not valid", rsetId, rid); terrno = TSDB_CODE_REF_INVALID_ID; return NULL; } @@ -226,7 +219,7 @@ void *taosAcquireRef(int rsetId, int64_t rid) } hash = rid % pSet->max; - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode = pSet->nodeList[hash]; @@ -252,20 +245,17 @@ void *taosAcquireRef(int rsetId, int64_t rid) uTrace("rsetId:%d rid:%" PRId64 " is not there, failed to acquire", rsetId, rid); } - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); taosDecRsetCount(pSet); return p; } -int taosReleaseRef(int rsetId, int64_t rid) -{ - return taosDecRefCount(rsetId, rid, 0); -} +int32_t taosReleaseRef(int32_t rsetId, int64_t rid) { return taosDecRefCount(rsetId, rid, 0); } // if rid is 0, return the first p in hash list, otherwise, return the next after current rid -void *taosIterateRef(int rsetId, int64_t rid) { +void *taosIterateRef(int32_t rsetId, int64_t rid) { SRefNode *pNode = NULL; SRefSet *pSet; @@ -293,10 +283,10 @@ void *taosIterateRef(int rsetId, int64_t rid) { do { newP = NULL; - int hash = 0; + int32_t hash = 0; if (rid > 0) { hash = rid % pSet->max; - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode = pSet->nodeList[hash]; while (pNode) { @@ -307,7 +297,7 @@ void *taosIterateRef(int rsetId, int64_t rid) { if (pNode == NULL) { uError("rsetId:%d rid:%" PRId64 " not there, quit", rsetId, rid); terrno = TSDB_CODE_REF_NOT_EXIST; - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); taosDecRsetCount(pSet); return NULL; } @@ -320,14 +310,14 @@ void *taosIterateRef(int rsetId, int64_t rid) { pNode = pNode->next; } if (pNode == NULL) { - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); hash++; } } if (pNode == NULL) { for (; hash < pSet->max; ++hash) { - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode = pSet->nodeList[hash]; if (pNode) { // check first place @@ -337,14 +327,14 @@ void *taosIterateRef(int rsetId, int64_t rid) { } if (pNode) break; } - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); } } if (pNode) { pNode->count++; // acquire it newP = pNode->p; - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); uTrace("rsetId:%d p:%p rid:%" PRId64 " is returned", rsetId, newP, rid); } else { uTrace("rsetId:%d the list is over", rsetId); @@ -359,22 +349,21 @@ void *taosIterateRef(int rsetId, int64_t rid) { return newP; } -int taosListRef() { +int32_t taosListRef() { SRefSet *pSet; SRefNode *pNode; - int num = 0; + int32_t num = 0; pthread_mutex_lock(&tsRefMutex); - for (int i = 0; i < TSDB_REF_OBJECTS; ++i) { + for (int32_t i = 0; i < TSDB_REF_OBJECTS; ++i) { pSet = tsRefSetList + i; - if (pSet->state == TSDB_REF_STATE_EMPTY) - continue; + if (pSet->state == TSDB_REF_STATE_EMPTY) continue; uInfo("rsetId:%d state:%d count::%d", i, pSet->state, pSet->count); - for (int j=0; j < pSet->max; ++j) { + for (int32_t j = 0; j < pSet->max; ++j) { pNode = pSet->nodeList[j]; while (pNode) { @@ -390,12 +379,12 @@ int taosListRef() { return num; } -static int taosDecRefCount(int rsetId, int64_t rid, int remove) { - int hash; +static int32_t taosDecRefCount(int32_t rsetId, int64_t rid, int32_t remove) { + int32_t hash; SRefSet *pSet; SRefNode *pNode; - int released = 0; - int code = 0; + int32_t released = 0; + int32_t code = 0; if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) { uTrace("rsetId:%d rid:%" PRId64 " failed to remove, rsetId not valid", rsetId, rid); @@ -417,12 +406,11 @@ static int taosDecRefCount(int rsetId, int64_t rid, int remove) { } hash = rid % pSet->max; - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode = pSet->nodeList[hash]; while (pNode) { - if (pNode->rid == rid) - break; + if (pNode->rid == rid) break; pNode = pNode->next; } @@ -437,13 +425,13 @@ static int taosDecRefCount(int rsetId, int64_t rid, int remove) { } else { pSet->nodeList[hash] = pNode->next; } - + if (pNode->next) { pNode->next->prev = pNode->prev; } released = 1; } else { - uTrace("rsetId:%d p:%p rid:%" PRId64 " is released", rsetId, pNode->p, rid); + uTrace("rsetId:%d p:%p rid:%" PRId64 " is released", rsetId, pNode->p, rid); } } else { uTrace("rsetId:%d rid:%" PRId64 " is not there, failed to release/remove", rsetId, rid); @@ -451,10 +439,11 @@ static int taosDecRefCount(int rsetId, int64_t rid, int remove) { code = -1; } - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); if (released) { - uTrace("rsetId:%d p:%p rid:%" PRId64 " is removed, count:%d, free mem: %p", rsetId, pNode->p, rid, pSet->count, pNode); + uTrace("rsetId:%d p:%p rid:%" PRId64 " is removed, count:%d, free mem: %p", rsetId, pNode->p, rid, pSet->count, + pNode); (*pSet->fp)(pNode->p); free(pNode); @@ -466,7 +455,7 @@ static int taosDecRefCount(int rsetId, int64_t rid, int remove) { static void taosLockList(int64_t *lockedBy) { int64_t tid = taosGetSelfPthreadId(); - int i = 0; + int32_t i = 0; while (atomic_val_compare_exchange_64(lockedBy, 0, tid) != 0) { if (++i % 100 == 0) { sched_yield(); @@ -481,9 +470,7 @@ static void taosUnlockList(int64_t *lockedBy) { } } -static void taosInitRefModule(void) { - pthread_mutex_init(&tsRefMutex, NULL); -} +static void taosInitRefModule(void) { pthread_mutex_init(&tsRefMutex, NULL); } static void taosIncRsetCount(SRefSet *pSet) { atomic_add_fetch_32(&pSet->count, 1); @@ -512,4 +499,3 @@ static void taosDecRsetCount(SRefSet *pSet) { pthread_mutex_unlock(&tsRefMutex); } -