#include #include #include #include #include #include "os.h" #include "tref.h" #include "tlog.h" #include "tglobal.h" #include "taoserror.h" #include "tulog.h" typedef struct { int refNum; int steps; int refId; void **p; } SRefSpace; void iterateRefs(int refId) { int count = 0; void *p = taosIterateRef(refId, NULL); while (p) { // process P count++; p = taosIterateRef(refId, p); } printf(" %d ", count); } void *takeRefActions(void *param) { SRefSpace *pSpace = (SRefSpace *)param; int code, id; for (int i=0; i < pSpace->steps; ++i) { printf("s"); id = random() % pSpace->refNum; code = taosAddRef(pSpace->refId, pSpace->p[id]); usleep(1); id = random() % pSpace->refNum; code = taosAcquireRef(pSpace->refId, pSpace->p[id]); if (code >= 0) { usleep(id % 5 + 1); taosReleaseRef(pSpace->refId, pSpace->p[id]); } id = random() % pSpace->refNum; taosRemoveRef(pSpace->refId, pSpace->p[id]); usleep(id %5 + 1); id = random() % pSpace->refNum; code = taosAcquireRef(pSpace->refId, pSpace->p[id]); if (code >= 0) { usleep(id % 5 + 1); taosReleaseRef(pSpace->refId, pSpace->p[id]); } id = random() % pSpace->refNum; iterateRefs(id); } for (int i=0; i < pSpace->refNum; ++i) { taosRemoveRef(pSpace->refId, pSpace->p[i]); } //uInfo("refId:%d thread exits", pSpace->refId); return NULL; } void myfree(void *p) { return; } void *openRefSpace(void *param) { SRefSpace *pSpace = (SRefSpace *)param; printf("c"); pSpace->refId = taosOpenRef(50, myfree); if (pSpace->refId < 0) { printf("failed to open ref, reson:%s\n", tstrerror(pSpace->refId)); return NULL; } pSpace->p = (void **) calloc(sizeof(void *), pSpace->refNum); for (int i=0; irefNum; ++i) { pSpace->p[i] = (void *) malloc(128); } pthread_attr_t thattr; pthread_attr_init(&thattr); pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); pthread_t thread1, thread2, thread3; pthread_create(&(thread1), &thattr, takeRefActions, (void *)(pSpace)); pthread_create(&(thread2), &thattr, takeRefActions, (void *)(pSpace)); pthread_create(&(thread3), &thattr, takeRefActions, (void *)(pSpace)); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); taosCloseRef(pSpace->refId); for (int i=0; irefNum; ++i) { free(pSpace->p[i]); } uInfo("refId:%d main thread exit", pSpace->refId); free(pSpace->p); pSpace->p = NULL; return NULL; } int main(int argc, char *argv[]) { int refNum = 100; int threads = 10; int steps = 10000; int loops = 1; uDebugFlag = 143; for (int i=1; i