#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 rsetId; int64_t rid; void **p; } SRefSpace; void iterateRefs(int rsetId) { int count = 0; void *p = taosIterateRef(rsetId, NULL); while (p) { // process P count++; p = taosIterateRef(rsetId, p); } printf(" %d ", count); } void *addRef(void *param) { SRefSpace *pSpace = (SRefSpace *)param; int id; int64_t rid; for (int i=0; i < pSpace->steps; ++i) { printf("a"); id = random() % pSpace->refNum; if (pSpace->rid[id] <= 0) { pSpace->p[id] = malloc(128); pSpace->rid[id] = taosAddRef(pSpace->rsetId, pSpace->p[id]); } usleep(100); } return NULL; } void *removeRef(void *param) { SRefSpace *pSpace = (SRefSpace *)param; int id; int64_t rid; for (int i=0; i < pSpace->steps; ++i) { printf("d"); id = random() % pSpace->refNum; if (pSpace->rid[id] > 0) { code = taosRemoveRef(pSpace->rsetId, pSpace->rid[id]); if (code == 0) pSpace->rid[id] = 0; } usleep(100); } return NULL; } void *acquireRelease(void *param) { SRefSpace *pSpace = (SRefSpace *)param; int id; int64_t rid; for (int i=0; i < pSpace->steps; ++i) { printf("a"); id = random() % pSpace->refNum; void *p = taosAcquireRef(pSpace->rsetId, pSpace->p[id]); if (p) { usleep(id % 5 + 1); taosReleaseRef(pSpace->rsetId, pSpace->p[id]); } } return NULL; } void myfree(void *p) { free(p); } void *openRefSpace(void *param) { SRefSpace *pSpace = (SRefSpace *)param; printf("c"); pSpace->rsetId = taosOpenRef(50, myfree); if (pSpace->rsetId < 0) { printf("failed to open ref, reson:%s\n", tstrerror(pSpace->rsetId)); return NULL; } pSpace->p = (void **) calloc(sizeof(void *), pSpace->refNum); pthread_attr_t thattr; pthread_attr_init(&thattr); pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); pthread_t thread1, thread2, thread3; pthread_create(&(thread1), &thattr, addRef, (void *)(pSpace)); pthread_create(&(thread2), &thattr, removeRef, (void *)(pSpace)); pthread_create(&(thread3), &thattr, acquireRelease, (void *)(pSpace)); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); for (int i=0; irefNum; ++i) { taosRemoveRef(pSpace->rsetId, pSpace->rid[i]); } taosCloseRef(pSpace->rsetId); uInfo("rsetId:%d main thread exit", pSpace->rsetId); 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