diff --git a/include/libs/wal/wal.h b/include/libs/wal/wal.h index 32fcd95948..7342af4d18 100644 --- a/include/libs/wal/wal.h +++ b/include/libs/wal/wal.h @@ -127,7 +127,7 @@ typedef struct SWal { int64_t lastRollSeq; // ctl int64_t refId; - pthread_mutex_t mutex; + TdThreadMutex mutex; // path char path[WAL_PATH_LEN]; // reusable write head diff --git a/include/os/os.h b/include/os/os.h index 9118a92e11..b05bfab6d0 100644 --- a/include/os/os.h +++ b/include/os/os.h @@ -22,7 +22,6 @@ extern "C" { #include #include -#include #include #include @@ -83,6 +82,7 @@ extern "C" { #include "osMath.h" #include "osMemory.h" #include "osRand.h" +#include "osThread.h" #include "osSemaphore.h" #include "osSignal.h" #include "osSleep.h" @@ -90,7 +90,6 @@ extern "C" { #include "osString.h" #include "osSysinfo.h" #include "osSystem.h" -#include "osThread.h" #include "osTime.h" #include "osTimer.h" #include "osTimezone.h" diff --git a/include/os/osSemaphore.h b/include/os/osSemaphore.h index 4bac81754d..594daf1bf3 100644 --- a/include/os/osSemaphore.h +++ b/include/os/osSemaphore.h @@ -20,7 +20,6 @@ extern "C" { #endif -#include #include #if defined (_TD_DARWIN_64) @@ -38,25 +37,25 @@ extern "C" { #endif #if defined (_TD_DARWIN_64) -// #define pthread_rwlock_t pthread_mutex_t -// #define pthread_rwlock_init(lock, NULL) pthread_mutex_init(lock, NULL) -// #define pthread_rwlock_destroy(lock) pthread_mutex_destroy(lock) -// #define pthread_rwlock_wrlock(lock) pthread_mutex_lock(lock) -// #define pthread_rwlock_rdlock(lock) pthread_mutex_lock(lock) -// #define pthread_rwlock_unlock(lock) pthread_mutex_unlock(lock) +// #define TdThreadRwlock TdThreadMutex +// #define taosThreadRwlockInit(lock, NULL) taosThreadMutexInit(lock, NULL) +// #define taosThreadRwlockDestroy(lock) taosThreadMutexDestroy(lock) +// #define taosThreadRwlockWrlock(lock) taosThreadMutexLock(lock) +// #define taosThreadRwlockRdlock(lock) taosThreadMutexLock(lock) +// #define taosThreadRwlockUnlock(lock) taosThreadMutexUnlock(lock) - #define pthread_spinlock_t pthread_mutex_t - #define pthread_spin_init(lock, NULL) pthread_mutex_init(lock, NULL) - #define pthread_spin_destroy(lock) pthread_mutex_destroy(lock) - #define pthread_spin_lock(lock) pthread_mutex_lock(lock) - #define pthread_spin_unlock(lock) pthread_mutex_unlock(lock) + #define TdThreadSpinlock TdThreadMutex + #define taosThreadSpinInit(lock, NULL) taosThreadMutexInit(lock, NULL) + #define taosThreadSpinDestroy(lock) taosThreadMutexDestroy(lock) + #define taosThreadSpinLock(lock) taosThreadMutexLock(lock) + #define taosThreadSpinUnlock(lock) taosThreadMutexUnlock(lock) #endif -bool taosCheckPthreadValid(pthread_t thread); +bool taosCheckPthreadValid(TdThread thread); int64_t taosGetSelfPthreadId(); -int64_t taosGetPthreadId(pthread_t thread); -void taosResetPthread(pthread_t* thread); -bool taosComparePthread(pthread_t first, pthread_t second); +int64_t taosGetPthreadId(TdThread thread); +void taosResetPthread(TdThread* thread); +bool taosComparePthread(TdThread first, TdThread second); int32_t taosGetPId(); int32_t taosGetAppName(char* name, int32_t* len); diff --git a/include/os/osString.h b/include/os/osString.h index 9c6d523ab2..5846d6b7ed 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -35,7 +35,7 @@ typedef int32_t TdUcs4; #define wctomb WCTOMB_FUNC_TAOS_FORBID #define wcstombs WCSTOMBS_FUNC_TAOS_FORBID #define wcsncpy WCSNCPY_FUNC_TAOS_FORBID - #define wchar_t WCHAR_T_FUNC_TAOS_FORBID + #define wchar_t WCHAR_T_TYPE_TAOS_FORBID #endif #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) diff --git a/include/os/osThread.h b/include/os/osThread.h index cccc13755d..504814c1c6 100644 --- a/include/os/osThread.h +++ b/include/os/osThread.h @@ -22,6 +22,94 @@ extern "C" { #endif +typedef pthread_t TdThread; +typedef pthread_spinlock_t TdThreadSpinlock; +typedef pthread_mutex_t TdThreadMutex; +typedef pthread_mutexattr_t TdThreadMutexAttr; +typedef pthread_rwlock_t TdThreadRwlock; +typedef pthread_attr_t TdThreadAttr; +typedef pthread_once_t TdThreadOnce; +typedef pthread_rwlockattr_t TdThreadRwlockAttr; +typedef pthread_cond_t TdThreadCond; +typedef pthread_condattr_t TdThreadCondAttr; + +#define taosThreadCleanupPush pthread_cleanup_push +#define taosThreadCleanupPop pthread_cleanup_pop + +// If the error is in a third-party library, place this header file under the third-party library header file. +#ifndef ALLOW_FORBID_FUNC + #define pthread_t PTHREAD_T_TYPE_TAOS_FORBID + #define pthread_spinlock_t PTHREAD_SPINLOCK_T_TYPE_TAOS_FORBID + #define pthread_mutex_t PTHREAD_MUTEX_T_TYPE_TAOS_FORBID + #define pthread_mutexattr_t PTHREAD_MUTEXATTR_T_TYPE_TAOS_FORBID + #define pthread_rwlock_t PTHREAD_RWLOCK_T_TYPE_TAOS_FORBID + #define pthread_attr_t PTHREAD_ATTR_T_TYPE_TAOS_FORBID + #define pthread_once_t PTHREAD_ONCE_T_TYPE_TAOS_FORBID + #define pthread_rwlockattr_t PTHREAD_RWLOCKATTR_T_TYPE_TAOS_FORBID + #define pthread_cond_t PTHREAD_COND_T_TYPE_TAOS_FORBID + #define pthread_condattr_t PTHREAD_CONDATTR_T_TYPE_TAOS_FORBID + #define pthread_spin_init PTHREAD_SPIN_INIT_FUNC_TAOS_FORBID + #define pthread_mutex_init PTHREAD_MUTEX_INIT_FUNC_TAOS_FORBID + #define pthread_spin_destroy PTHREAD_SPIN_DESTROY_FUNC_TAOS_FORBID + #define pthread_mutex_destroy PTHREAD_MUTEX_DESTROY_FUNC_TAOS_FORBID + #define pthread_spin_lock PTHREAD_SPIN_LOCK_FUNC_TAOS_FORBID + #define pthread_mutex_lock PTHREAD_MUTEX_LOCK_FUNC_TAOS_FORBID + #define pthread_spin_unlock PTHREAD_SPIN_UNLOCK_FUNC_TAOS_FORBID + #define pthread_mutex_unlock PTHREAD_MUTEX_UNLOCK_FUNC_TAOS_FORBID + #define pthread_rwlock_rdlock PTHREAD_RWLOCK_RDLOCK_FUNC_TAOS_FORBID + #define pthread_rwlock_wrlock PTHREAD_RWLOCK_WRLOCK_FUNC_TAOS_FORBID + #define pthread_rwlock_unlock PTHREAD_RWLOCK_UNLOCK_FUNC_TAOS_FORBID + #define pthread_testcancel PTHREAD_TESTCANCEL_FUNC_TAOS_FORBID + #define pthread_attr_init PTHREAD_ATTR_INIT_FUNC_TAOS_FORBID + #define pthread_create PTHREAD_CREATE_FUNC_TAOS_FORBID + #define pthread_once PTHREAD_ONCE_FUNC_TAOS_FORBID + #define pthread_attr_setdetachstate PTHREAD_ATTR_SETDETACHSTATE_FUNC_TAOS_FORBID + #define pthread_attr_destroy PTHREAD_ATTR_DESTROY_FUNC_TAOS_FORBID + #define pthread_join PTHREAD_JOIN_FUNC_TAOS_FORBID + #define pthread_rwlock_init PTHREAD_RWLOCK_INIT_FUNC_TAOS_FORBID + #define pthread_rwlock_destroy PTHREAD_RWLOCK_DESTROY_FUNC_TAOS_FORBID + #define pthread_cond_signal PTHREAD_COND_SIGNAL_FUNC_TAOS_FORBID + #define pthread_cond_init PTHREAD_COND_INIT_FUNC_TAOS_FORBID + #define pthread_cond_broadcast PTHREAD_COND_BROADCAST_FUNC_TAOS_FORBID + #define pthread_cond_destroy PTHREAD_COND_DESTROY_FUNC_TAOS_FORBID + #define pthread_cond_wait PTHREAD_COND_WAIT_FUNC_TAOS_FORBID + #define pthread_self PTHREAD_SELF_FUNC_TAOS_FORBID + #define pthread_equal PTHREAD_EQUAL_FUNC_TAOS_FORBID + #define pthread_sigmask PTHREAD_SIGMASK_FUNC_TAOS_FORBID + #define pthread_cancel PTHREAD_CANCEL_FUNC_TAOS_FORBID + #define pthread_kill PTHREAD_KILL_FUNC_TAOS_FORBID +#endif + +int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int pshared); +int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr); +int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock); +int32_t taosThreadMutexDestroy(TdThreadMutex * mutex); +int32_t taosThreadSpinLock(TdThreadSpinlock *lock); +int32_t taosThreadMutexLock(TdThreadMutex *mutex); +int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock); +int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock); +int32_t taosThreadMutexUnlock(TdThreadMutex *mutex); +int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock); +int32_t taosThreadRwlockUnlock(TdThreadRwlock *rwlock); +void taosThreadTestCancel(void); +int32_t taosThreadAttrInit(TdThreadAttr *attr); +int32_t taosThreadCreate(TdThread *tid, const TdThreadAttr *attr, void*(*start)(void*), void *arg); +int32_t taosThreadOnce(TdThreadOnce *onceControl, void(*initRoutine)(void)); +int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachState); +int32_t taosThreadAttrDestroy(TdThreadAttr *attr); +int32_t taosThreadJoin(TdThread thread, void **pValue); +int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr); +int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock); +int32_t taosThreadCondSignal(TdThreadCond *cond); +int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr); +int32_t taosThreadCondBroadcast(TdThreadCond *cond); +int32_t taosThreadCondDestroy(TdThreadCond *cond); +int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex); +TdThread taosThreadSelf(void); +int32_t taosThreadEqual(TdThread t1, TdThread t2); +int32_t taosThreadSigmask(int how, sigset_t const *set, sigset_t *oset); +int32_t taosThreadCancel(TdThread thread); +int32_t taosThreadKill(TdThread thread, int sig); #ifdef __cplusplus } #endif diff --git a/include/os/osTimezone.h b/include/os/osTimezone.h index ff015ef0b1..fa0e70f209 100644 --- a/include/os/osTimezone.h +++ b/include/os/osTimezone.h @@ -20,6 +20,11 @@ extern "C" { #endif +// If the error is in a third-party library, place this header file under the third-party library header file. +#ifndef ALLOW_FORBID_FUNC + #define tzset TZSET_FUNC_TAOS_FORBID +#endif + void taosGetSystemTimezone(char *outTimezone); void taosSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight); diff --git a/include/util/tidpool.h b/include/util/tidpool.h index 8596b439e3..c97f4c5f38 100644 --- a/include/util/tidpool.h +++ b/include/util/tidpool.h @@ -27,7 +27,7 @@ typedef struct { int32_t numOfFree; int32_t freeSlot; bool *freeList; - pthread_mutex_t mutex; + TdThreadMutex mutex; } id_pool_t; void *taosInitIdPool(int32_t maxId); diff --git a/include/util/tskiplist.h b/include/util/tskiplist.h index 64cab08cfe..a2382ad541 100644 --- a/include/util/tskiplist.h +++ b/include/util/tskiplist.h @@ -57,7 +57,7 @@ typedef struct SSkipListNode { * @date 2017/11/12 * the simple version of skip list. * - * for multi-thread safe purpose, we employ pthread_rwlock_t to guarantee to generate + * for multi-thread safe purpose, we employ TdThreadRwlock to guarantee to generate * deterministic result. Later, we will remove the lock in SkipList to further enhance the performance. * In this case, one should use the concurrent skip list (by using michael-scott algorithm) instead of * this simple version in a multi-thread environment, to achieve higher performance of read/write operations. @@ -106,7 +106,7 @@ typedef struct SSkipList { uint32_t seed; __compar_fn_t comparFn; __sl_key_fn_t keyFn; - pthread_rwlock_t *lock; + TdThreadRwlock *lock; uint16_t len; uint8_t maxLevel; uint8_t flags; diff --git a/include/util/tthread.h b/include/util/tthread.h index 4941206944..6fbd2c5f7c 100644 --- a/include/util/tthread.h +++ b/include/util/tthread.h @@ -22,9 +22,9 @@ extern "C" { #endif -pthread_t* taosCreateThread(void* (*__start_routine)(void*), void* param); -bool taosDestoryThread(pthread_t* pthread); -bool taosThreadRunning(pthread_t* pthread); +TdThread* taosCreateThread(void* (*__start_routine)(void*), void* param); +bool taosDestoryThread(TdThread* pthread); +bool taosThreadRunning(TdThread* pthread); #ifdef __cplusplus } diff --git a/include/util/tworker.h b/include/util/tworker.h index e6f6bc077c..dabd1ac9b3 100644 --- a/include/util/tworker.h +++ b/include/util/tworker.h @@ -27,7 +27,7 @@ typedef struct SWWorkerPool SWWorkerPool; typedef struct SQWorker { int32_t id; // worker ID - pthread_t thread; // thread + TdThread thread; // thread SQWorkerPool *pool; } SQWorker, SFWorker; @@ -38,12 +38,12 @@ typedef struct SQWorkerPool { STaosQset *qset; const char *name; SQWorker *workers; - pthread_mutex_t mutex; + TdThreadMutex mutex; } SQWorkerPool, SFWorkerPool; typedef struct SWWorker { int32_t id; // worker id - pthread_t thread; // thread + TdThread thread; // thread STaosQall *qall; STaosQset *qset; // queue set SWWorkerPool *pool; @@ -54,7 +54,7 @@ typedef struct SWWorkerPool { int32_t nextId; // from 0 to max-1, cyclic const char *name; SWWorker *workers; - pthread_mutex_t mutex; + TdThreadMutex mutex; } SWWorkerPool; int32_t tQWorkerInit(SQWorkerPool *pool); diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 4067807eaf..d713b11a92 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -77,8 +77,8 @@ typedef struct { int8_t inited; // ctl int8_t threadStop; - pthread_t thread; - pthread_mutex_t lock; // used when app init and cleanup + TdThread thread; + TdThreadMutex lock; // used when app init and cleanup SArray* appHbMgrs; // SArray one for each cluster FHbReqHandle reqHandle[HEARTBEAT_TYPE_MAX]; FHbRspHandle rspHandle[HEARTBEAT_TYPE_MAX]; @@ -125,7 +125,7 @@ typedef struct SAppInfo { int32_t pid; int32_t numOfThreads; SHashObj* pInstMap; - pthread_mutex_t mutex; + TdThreadMutex mutex; } SAppInfo; typedef struct STscObj { @@ -137,7 +137,7 @@ typedef struct STscObj { uint32_t connId; int32_t connType; uint64_t id; // ref ID returned by taosAddRef - pthread_mutex_t mutex; // used to protect the operation on db + TdThreadMutex mutex; // used to protect the operation on db int32_t numOfReqs; // number of sqlObj bound to this connection SAppInstInfo* pAppInfo; } STscObj; diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 24abbd263c..07f925bce8 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -33,7 +33,7 @@ SAppInfo appInfo; int32_t clientReqRefPool = -1; int32_t clientConnRefPool = -1; -static pthread_once_t tscinit = PTHREAD_ONCE_INIT; +static TdThreadOnce tscinit = PTHREAD_ONCE_INIT; volatile int32_t tscInitRes = 0; static void registerRequest(SRequestObj *pRequest) { @@ -114,7 +114,7 @@ void destroyTscObj(void *pObj) { hbDeregisterConn(pTscObj->pAppInfo->pAppHbMgr, connKey); atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1); tscDebug("connObj 0x%" PRIx64 " destroyed, totalConn:%" PRId64, pTscObj->id, pTscObj->pAppInfo->numOfConns); - pthread_mutex_destroy(&pTscObj->mutex); + taosThreadMutexDestroy(&pTscObj->mutex); tfree(pTscObj); } @@ -133,7 +133,7 @@ void *createTscObj(const char *user, const char *auth, const char *db, SAppInstI tstrncpy(pObj->db, db, tListLen(pObj->db)); } - pthread_mutex_init(&pObj->mutex, NULL); + taosThreadMutexInit(&pObj->mutex, NULL); pObj->id = taosAddRef(clientConnRefPool, pObj); tscDebug("connObj created, 0x%" PRIx64, pObj->id); @@ -242,7 +242,7 @@ void taos_init_imp(void) { // transDestroyBuffer(&conn->readBuf); taosGetAppName(appInfo.appName, NULL); - pthread_mutex_init(&appInfo.mutex, NULL); + taosThreadMutexInit(&appInfo.mutex, NULL); appInfo.pid = taosGetPId(); appInfo.startTime = taosGetTimestampMs(); @@ -251,7 +251,7 @@ void taos_init_imp(void) { } int taos_init() { - pthread_once(&tscinit, taos_init_imp); + taosThreadOnce(&tscinit, taos_init_imp); return tscInitRes; } @@ -507,9 +507,9 @@ static setConfRet taos_set_config_imp(const char *config){ } setConfRet taos_set_config(const char *config){ - pthread_mutex_lock(&setConfMutex); + taosThreadMutexLock(&setConfMutex); setConfRet ret = taos_set_config_imp(config); - pthread_mutex_unlock(&setConfMutex); + taosThreadMutexUnlock(&setConfMutex); return ret; } #endif diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 7163444719..479281eec6 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -372,7 +372,7 @@ static void *hbThreadFunc(void *param) { break; } - pthread_mutex_lock(&clientHbMgr.lock); + taosThreadMutexLock(&clientHbMgr.lock); int sz = taosArrayGetSize(clientHbMgr.appHbMgrs); for (int i = 0; i < sz; i++) { @@ -423,7 +423,7 @@ static void *hbThreadFunc(void *param) { atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1); } - pthread_mutex_unlock(&clientHbMgr.lock); + taosThreadMutexUnlock(&clientHbMgr.lock); taosMsleep(HEARTBEAT_INTERVAL); } @@ -431,15 +431,15 @@ static void *hbThreadFunc(void *param) { } static int32_t hbCreateThread() { - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); -// if (pthread_create(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) { +// if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) { // terrno = TAOS_SYSTEM_ERROR(errno); // return -1; // } -// pthread_attr_destroy(&thAttr); +// taosThreadAttrDestroy(&thAttr); return 0; } @@ -492,15 +492,15 @@ SAppHbMgr *appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key) { return NULL; } - pthread_mutex_lock(&clientHbMgr.lock); + taosThreadMutexLock(&clientHbMgr.lock); taosArrayPush(clientHbMgr.appHbMgrs, &pAppHbMgr); - pthread_mutex_unlock(&clientHbMgr.lock); + taosThreadMutexUnlock(&clientHbMgr.lock); return pAppHbMgr; } void appHbMgrCleanup(void) { - pthread_mutex_lock(&clientHbMgr.lock); + taosThreadMutexLock(&clientHbMgr.lock); int sz = taosArrayGetSize(clientHbMgr.appHbMgrs); for (int i = 0; i < sz; i++) { @@ -511,7 +511,7 @@ void appHbMgrCleanup(void) { pTarget->connInfo = NULL; } - pthread_mutex_unlock(&clientHbMgr.lock); + taosThreadMutexUnlock(&clientHbMgr.lock); } int hbMgrInit() { @@ -520,7 +520,7 @@ int hbMgrInit() { if (old == 1) return 0; clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *)); - pthread_mutex_init(&clientHbMgr.lock, NULL); + taosThreadMutexInit(&clientHbMgr.lock, NULL); // init handle funcs hbMgrInitHandle(); @@ -539,10 +539,10 @@ void hbMgrCleanUp() { int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0); if (old == 0) return; - pthread_mutex_lock(&clientHbMgr.lock); + taosThreadMutexLock(&clientHbMgr.lock); appHbMgrCleanup(); taosArrayDestroy(clientHbMgr.appHbMgrs); - pthread_mutex_unlock(&clientHbMgr.lock); + taosThreadMutexUnlock(&clientHbMgr.lock); clientHbMgr.appHbMgrs = NULL; #endif diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index a5752a6f2e..e73854dd6d 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -95,7 +95,7 @@ TAOS* taos_connect_internal(const char* ip, const char* user, const char* pass, char* key = getClusterKey(user, secretEncrypt, ip, port); SAppInstInfo** pInst = NULL; - pthread_mutex_lock(&appInfo.mutex); + taosThreadMutexLock(&appInfo.mutex); pInst = taosHashGet(appInfo.pInstMap, key, strlen(key)); SAppInstInfo* p = NULL; @@ -109,7 +109,7 @@ TAOS* taos_connect_internal(const char* ip, const char* user, const char* pass, pInst = &p; } - pthread_mutex_unlock(&appInfo.mutex); + taosThreadMutexUnlock(&appInfo.mutex); tfree(key); return taosConnectImpl(user, &secretEncrypt[0], localDb, NULL, NULL, *pInst); @@ -591,21 +591,21 @@ void setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32_t char* getDbOfConnection(STscObj* pObj) { char* p = NULL; - pthread_mutex_lock(&pObj->mutex); + taosThreadMutexLock(&pObj->mutex); size_t len = strlen(pObj->db); if (len > 0) { p = strndup(pObj->db, tListLen(pObj->db)); } - pthread_mutex_unlock(&pObj->mutex); + taosThreadMutexUnlock(&pObj->mutex); return p; } void setConnectionDB(STscObj* pTscObj, const char* db) { assert(db != NULL && pTscObj != NULL); - pthread_mutex_lock(&pTscObj->mutex); + taosThreadMutexLock(&pTscObj->mutex); tstrncpy(pTscObj->db, db, tListLen(pTscObj->db)); - pthread_mutex_unlock(&pTscObj->mutex); + taosThreadMutexUnlock(&pTscObj->mutex); } void setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp) { diff --git a/source/dnode/mgmt/impl/inc/dndEnv.h b/source/dnode/mgmt/impl/inc/dndEnv.h index aeea5386b4..ab45e30bf6 100644 --- a/source/dnode/mgmt/impl/inc/dndEnv.h +++ b/source/dnode/mgmt/impl/inc/dndEnv.h @@ -56,7 +56,7 @@ typedef struct { char *file; SHashObj *dnodeHash; SArray *pDnodeEps; - pthread_t *threadId; + TdThread *threadId; SRWLatch latch; SDnodeWorker mgmtWorker; SDnodeWorker statusWorker; diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 866076a3d0..04a16824bd 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -14,6 +14,7 @@ */ #define _DEFAULT_SOURCE +#include "os.h" #include "dndMgmt.h" #include "dndBnode.h" #include "dndMnode.h" @@ -553,7 +554,7 @@ static void *dnodeThreadRoutine(void *param) { setThreadName("dnode-hb"); while (true) { - pthread_testcancel(); + taosThreadTestCancel(); taosMsleep(200); if (dndGetStat(pDnode) != DND_STAT_RUNNING || pMgmt->dropped) { continue; diff --git a/source/dnode/mgmt/impl/src/dndVnodes.c b/source/dnode/mgmt/impl/src/dndVnodes.c index d311e1e417..d066f025d7 100644 --- a/source/dnode/mgmt/impl/src/dndVnodes.c +++ b/source/dnode/mgmt/impl/src/dndVnodes.c @@ -50,7 +50,7 @@ typedef struct { int32_t opened; int32_t failed; int32_t threadIndex; - pthread_t thread; + TdThread thread; SDnode *pDnode; SWrapperCfg *pCfgs; } SVnodeThread; @@ -450,20 +450,20 @@ static int32_t dndOpenVnodes(SDnode *pDnode) { SVnodeThread *pThread = &threads[t]; if (pThread->vnodeNum == 0) continue; - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&pThread->thread, &thAttr, dnodeOpenVnodeFunc, pThread) != 0) { + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); + if (taosThreadCreate(&pThread->thread, &thAttr, dnodeOpenVnodeFunc, pThread) != 0) { dError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(errno)); } - pthread_attr_destroy(&thAttr); + taosThreadAttrDestroy(&thAttr); } for (int32_t t = 0; t < threadNum; ++t) { SVnodeThread *pThread = &threads[t]; if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) { - pthread_join(pThread->thread, NULL); + taosThreadJoin(pThread->thread, NULL); } free(pThread->pCfgs); } diff --git a/source/dnode/mgmt/impl/test/sut/inc/server.h b/source/dnode/mgmt/impl/test/sut/inc/server.h index 99554a7aa7..96b44453d4 100644 --- a/source/dnode/mgmt/impl/test/sut/inc/server.h +++ b/source/dnode/mgmt/impl/test/sut/inc/server.h @@ -28,7 +28,7 @@ class TestServer { private: SDnode* pDnode; - pthread_t* threadId; + TdThread* threadId; char path[PATH_MAX]; char fqdn[TSDB_FQDN_LEN]; char firstEp[TSDB_EP_LEN]; diff --git a/source/dnode/mgmt/impl/test/sut/src/server.cpp b/source/dnode/mgmt/impl/test/sut/src/server.cpp index 985625b41c..bbbd2af4ab 100644 --- a/source/dnode/mgmt/impl/test/sut/src/server.cpp +++ b/source/dnode/mgmt/impl/test/sut/src/server.cpp @@ -18,7 +18,7 @@ void* serverLoop(void* param) { while (1) { taosMsleep(100); - pthread_testcancel(); + taosThreadTestCancel(); } } diff --git a/source/dnode/vnode/src/inc/tsdbDef.h b/source/dnode/vnode/src/inc/tsdbDef.h index 69bd1ccea5..f7fdc818d0 100644 --- a/source/dnode/vnode/src/inc/tsdbDef.h +++ b/source/dnode/vnode/src/inc/tsdbDef.h @@ -16,6 +16,7 @@ #ifndef _TD_TSDB_DEF_H_ #define _TD_TSDB_DEF_H_ +#include "tsdbDBDef.h" #include "tmallocator.h" #include "meta.h" #include "tcompression.h" @@ -27,7 +28,6 @@ #include "ttime.h" #include "tsdb.h" -#include "tsdbDBDef.h" #include "tsdbCommit.h" #include "tsdbFS.h" #include "tsdbFile.h" @@ -46,7 +46,7 @@ extern "C" { struct STsdb { int32_t vgId; bool repoLocked; - pthread_mutex_t mutex; + TdThreadMutex mutex; char * path; STsdbCfg config; STsdbMemTable * mem; diff --git a/source/dnode/vnode/src/inc/tsdbFS.h b/source/dnode/vnode/src/inc/tsdbFS.h index 21b445755f..8156cbae00 100644 --- a/source/dnode/vnode/src/inc/tsdbFS.h +++ b/source/dnode/vnode/src/inc/tsdbFS.h @@ -64,7 +64,7 @@ typedef struct { */ typedef struct { - pthread_rwlock_t lock; + TdThreadRwlock lock; SFSStatus *cstatus; // current status SHashObj * metaCache; // meta cache @@ -108,7 +108,7 @@ SDFileSet *tsdbFSIterNext(SFSIter *pIter); int tsdbLoadMetaCache(STsdb *pRepo, bool recoverMeta); static FORCE_INLINE int tsdbRLockFS(STsdbFS *pFs) { - int code = pthread_rwlock_rdlock(&(pFs->lock)); + int code = taosThreadRwlockRdlock(&(pFs->lock)); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); return -1; @@ -117,7 +117,7 @@ static FORCE_INLINE int tsdbRLockFS(STsdbFS *pFs) { } static FORCE_INLINE int tsdbWLockFS(STsdbFS *pFs) { - int code = pthread_rwlock_wrlock(&(pFs->lock)); + int code = taosThreadRwlockWrlock(&(pFs->lock)); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); return -1; @@ -126,7 +126,7 @@ static FORCE_INLINE int tsdbWLockFS(STsdbFS *pFs) { } static FORCE_INLINE int tsdbUnLockFS(STsdbFS *pFs) { - int code = pthread_rwlock_unlock(&(pFs->lock)); + int code = taosThreadRwlockUnlock(&(pFs->lock)); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); return -1; diff --git a/source/dnode/vnode/src/inc/tsdbSma.h b/source/dnode/vnode/src/inc/tsdbSma.h index b94f0b1b83..f934b0263d 100644 --- a/source/dnode/vnode/src/inc/tsdbSma.h +++ b/source/dnode/vnode/src/inc/tsdbSma.h @@ -20,7 +20,7 @@ typedef struct SSmaStat SSmaStat; typedef struct SSmaEnv SSmaEnv; struct SSmaEnv { - pthread_rwlock_t lock; + TdThreadRwlock lock; SDiskID did; TDBEnv dbEnv; // TODO: If it's better to put it in smaIndex level? char * path; // relative path @@ -51,7 +51,7 @@ static FORCE_INLINE int32_t tsdbEncodeTSmaKey(tb_uid_t tableUid, col_id_t colId, } static FORCE_INLINE int tsdbRLockSma(SSmaEnv *pEnv) { - int code = pthread_rwlock_rdlock(&(pEnv->lock)); + int code = taosThreadRwlockRdlock(&(pEnv->lock)); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); return -1; @@ -60,7 +60,7 @@ static FORCE_INLINE int tsdbRLockSma(SSmaEnv *pEnv) { } static FORCE_INLINE int tsdbWLockSma(SSmaEnv *pEnv) { - int code = pthread_rwlock_wrlock(&(pEnv->lock)); + int code = taosThreadRwlockWrlock(&(pEnv->lock)); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); return -1; @@ -69,7 +69,7 @@ static FORCE_INLINE int tsdbWLockSma(SSmaEnv *pEnv) { } static FORCE_INLINE int tsdbUnLockSma(SSmaEnv *pEnv) { - int code = pthread_rwlock_unlock(&(pEnv->lock)); + int code = taosThreadRwlockUnlock(&(pEnv->lock)); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); return -1; diff --git a/source/dnode/vnode/src/inc/vnd.h b/source/dnode/vnode/src/inc/vnd.h index 6f4f0049e3..534b01e40c 100644 --- a/source/dnode/vnode/src/inc/vnd.h +++ b/source/dnode/vnode/src/inc/vnd.h @@ -48,9 +48,9 @@ typedef struct SVnodeMgr { // For commit bool stop; uint16_t nthreads; - pthread_t* threads; - pthread_mutex_t mutex; - pthread_cond_t hasTask; + TdThread* threads; + TdThreadMutex mutex; + TdThreadCond hasTask; TD_DLIST(SVnodeTask) queue; // For vnode Mgmt SDnode* pDnode; diff --git a/source/dnode/vnode/src/meta/metaBDBImpl.c b/source/dnode/vnode/src/meta/metaBDBImpl.c index 99a2b272ed..d4cadc75e4 100644 --- a/source/dnode/vnode/src/meta/metaBDBImpl.c +++ b/source/dnode/vnode/src/meta/metaBDBImpl.c @@ -33,7 +33,7 @@ typedef struct { struct SMetaDB { #if IMPL_WITH_LOCK - pthread_rwlock_t rwlock; + TdThreadRwlock rwlock; #endif // DB DB *pTbDB; @@ -317,7 +317,7 @@ static SMetaDB *metaNewDB() { } #if IMPL_WITH_LOCK - pthread_rwlock_init(&pDB->rwlock, NULL); + taosThreadRwlockInit(&pDB->rwlock, NULL); #endif return pDB; @@ -326,7 +326,7 @@ static SMetaDB *metaNewDB() { static void metaFreeDB(SMetaDB *pDB) { if (pDB) { #if IMPL_WITH_LOCK - pthread_rwlock_destroy(&pDB->rwlock); + taosThreadRwlockDestroy(&pDB->rwlock); #endif free(pDB); } @@ -965,18 +965,18 @@ SArray *metaGetSmaTbUids(SMeta *pMeta, bool isDup) { static void metaDBWLock(SMetaDB *pDB) { #if IMPL_WITH_LOCK - pthread_rwlock_wrlock(&(pDB->rwlock)); + taosThreadRwlockWrlock(&(pDB->rwlock)); #endif } static void metaDBRLock(SMetaDB *pDB) { #if IMPL_WITH_LOCK - pthread_rwlock_rdlock(&(pDB->rwlock)); + taosThreadRwlockRdlock(&(pDB->rwlock)); #endif } static void metaDBULock(SMetaDB *pDB) { #if IMPL_WITH_LOCK - pthread_rwlock_unlock(&(pDB->rwlock)); + taosThreadRwlockUnlock(&(pDB->rwlock)); #endif } diff --git a/source/dnode/vnode/src/tsdb/tsdbFS.c b/source/dnode/vnode/src/tsdb/tsdbFS.c index a03739c90f..0f0d50ad08 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFS.c +++ b/source/dnode/vnode/src/tsdb/tsdbFS.c @@ -14,8 +14,8 @@ */ #include -#include "os.h" #include "tsdbDef.h" +#include "os.h" typedef enum { TSDB_TXN_TEMP_FILE = 0, TSDB_TXN_CURR_FILE } TSDB_TXN_FILE_T; static const char *tsdbTxnFname[] = {"current.t", "current"}; @@ -203,7 +203,7 @@ STsdbFS *tsdbNewFS(const STsdbCfg *pCfg) { return NULL; } - int code = pthread_rwlock_init(&(pfs->lock), NULL); + int code = taosThreadRwlockInit(&(pfs->lock), NULL); if (code) { terrno = TAOS_SYSTEM_ERROR(code); free(pfs); @@ -241,7 +241,7 @@ void *tsdbFreeFS(STsdbFS *pfs) { taosHashCleanup(pfs->metaCache); pfs->metaCache = NULL; pfs->cstatus = tsdbFreeFSStatus(pfs->cstatus); - pthread_rwlock_destroy(&(pfs->lock)); + taosThreadRwlockDestroy(&(pfs->lock)); free(pfs); } diff --git a/source/dnode/vnode/src/tsdb/tsdbMain.c b/source/dnode/vnode/src/tsdb/tsdbMain.c index afa8921c00..131e92cab6 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMain.c +++ b/source/dnode/vnode/src/tsdb/tsdbMain.c @@ -110,7 +110,7 @@ static void tsdbCloseImpl(STsdb *pTsdb) { } int tsdbLockRepo(STsdb *pTsdb) { - int code = pthread_mutex_lock(&pTsdb->mutex); + int code = taosThreadMutexLock(&pTsdb->mutex); if (code != 0) { tsdbError("vgId:%d failed to lock tsdb since %s", REPO_ID(pTsdb), strerror(errno)); terrno = TAOS_SYSTEM_ERROR(code); @@ -123,7 +123,7 @@ int tsdbLockRepo(STsdb *pTsdb) { int tsdbUnlockRepo(STsdb *pTsdb) { ASSERT(IS_REPO_LOCKED(pTsdb)); pTsdb->repoLocked = false; - int code = pthread_mutex_unlock(&pTsdb->mutex); + int code = taosThreadMutexUnlock(&pTsdb->mutex); if (code != 0) { tsdbError("vgId:%d failed to unlock tsdb since %s", REPO_ID(pTsdb), strerror(errno)); terrno = TAOS_SYSTEM_ERROR(code); @@ -298,7 +298,7 @@ STsdbCfg *tsdbGetCfg(const STsdbRepo *repo) { } int tsdbLockRepo(STsdbRepo *pRepo) { - int code = pthread_mutex_lock(&pRepo->mutex); + int code = taosThreadMutexLock(&pRepo->mutex); if (code != 0) { tsdbError("vgId:%d failed to lock tsdb since %s", REPO_ID(pRepo), strerror(errno)); terrno = TAOS_SYSTEM_ERROR(code); @@ -311,7 +311,7 @@ int tsdbLockRepo(STsdbRepo *pRepo) { int tsdbUnlockRepo(STsdbRepo *pRepo) { ASSERT(IS_REPO_LOCKED(pRepo)); pRepo->repoLocked = false; - int code = pthread_mutex_unlock(&pRepo->mutex); + int code = taosThreadMutexUnlock(&pRepo->mutex); if (code != 0) { tsdbError("vgId:%d failed to unlock tsdb since %s", REPO_ID(pRepo), strerror(errno)); terrno = TAOS_SYSTEM_ERROR(code); @@ -388,7 +388,7 @@ int32_t tsdbConfigRepo(STsdbRepo *repo, STsdbCfg *pCfg) { tsdbError("vgId:%d no config changed", REPO_ID(repo)); } - int code = pthread_mutex_lock(&repo->save_mutex); + int code = taosThreadMutexLock(&repo->save_mutex); if (code != 0) { tsdbError("vgId:%d failed to lock tsdb save config mutex since %s", REPO_ID(repo), strerror(errno)); terrno = TAOS_SYSTEM_ERROR(code); @@ -416,7 +416,7 @@ int32_t tsdbConfigRepo(STsdbRepo *repo, STsdbCfg *pCfg) { repo->config_changed = true; - pthread_mutex_unlock(&repo->save_mutex); + taosThreadMutexUnlock(&repo->save_mutex); // schedule a commit msg and wait for the new config applied tsdbSyncCommitConfig(repo); @@ -690,14 +690,14 @@ static STsdbRepo *tsdbNewRepo(STsdbCfg *pCfg, STsdbAppH *pAppH) { pRepo->repoLocked = false; pRepo->pthread = NULL; - int code = pthread_mutex_init(&(pRepo->mutex), NULL); + int code = taosThreadMutexInit(&(pRepo->mutex), NULL); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); tsdbFreeRepo(pRepo); return NULL; } - code = pthread_mutex_init(&(pRepo->save_mutex), NULL); + code = taosThreadMutexInit(&(pRepo->save_mutex), NULL); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); tsdbFreeRepo(pRepo); @@ -747,7 +747,7 @@ static void tsdbFreeRepo(STsdbRepo *pRepo) { // tsdbFreeMemTable(pRepo->mem); // tsdbFreeMemTable(pRepo->imem); tsem_destroy(&(pRepo->readyToCommit)); - pthread_mutex_destroy(&pRepo->mutex); + taosThreadMutexDestroy(&pRepo->mutex); free(pRepo); } } diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable.c b/source/dnode/vnode/src/tsdb/tsdbMemTable.c index 01813af556..e46d1a0ed4 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable.c @@ -541,7 +541,7 @@ int tsdbUnRefMemTable(STsdbRepo *pRepo, SMemTable *pMemTable) { } } if (addNew) { - int code = pthread_cond_signal(&pBufPool->poolNotEmpty); + int code = taosThreadCondSignal(&pBufPool->poolNotEmpty); if (code != 0) { if (tsdbUnlockRepo(pRepo) < 0) return -1; tsdbError("vgId:%d failed to signal pool not empty since %s", REPO_ID(pRepo), strerror(code)); diff --git a/source/dnode/vnode/src/tsdb/tsdbRead.c b/source/dnode/vnode/src/tsdb/tsdbRead.c index 6b2857c411..b7719240f9 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead.c @@ -13,6 +13,7 @@ * along with this program. If not, see . */ +#include "tsdbDef.h" #include #include "os.h" #include "talgo.h" @@ -20,7 +21,6 @@ #include "tdataformat.h" #include "texception.h" #include "tsdb.h" -#include "tsdbDef.h" #include "tsdbFS.h" #include "tsdbLog.h" #include "tsdbReadImpl.h" diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index 512ea9fa25..0eb2d525b3 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -124,7 +124,7 @@ static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, const char *path, SDiskID did) return NULL; } - int code = pthread_rwlock_init(&(pEnv->lock), NULL); + int code = taosThreadRwlockInit(&(pEnv->lock), NULL); if (code) { terrno = TAOS_SYSTEM_ERROR(code); free(pEnv); @@ -181,7 +181,7 @@ void tsdbDestroySmaEnv(SSmaEnv *pSmaEnv) { tsdbDestroySmaState(pSmaEnv->pStat); tfree(pSmaEnv->pStat); tfree(pSmaEnv->path); - pthread_rwlock_destroy(&(pSmaEnv->lock)); + taosThreadRwlockDestroy(&(pSmaEnv->lock)); tsdbCloseBDBEnv(pSmaEnv->dbEnv); } } diff --git a/source/dnode/vnode/src/vnd/vnodeBufferPool.c b/source/dnode/vnode/src/vnd/vnodeBufferPool.c index 9d1877bdb7..3b43fd82f6 100644 --- a/source/dnode/vnode/src/vnd/vnodeBufferPool.c +++ b/source/dnode/vnode/src/vnd/vnodeBufferPool.c @@ -19,8 +19,8 @@ #define VNODE_BUF_POOL_SHARDS 3 struct SVBufPool { - pthread_mutex_t mutex; - pthread_cond_t hasFree; + TdThreadMutex mutex; + TdThreadCond hasFree; TD_DLIST(SVMemAllocator) free; TD_DLIST(SVMemAllocator) incycle; SVMemAllocator *inuse; diff --git a/source/dnode/vnode/src/vnd/vnodeMgr.c b/source/dnode/vnode/src/vnd/vnodeMgr.c index 477deed8c8..06423c793b 100644 --- a/source/dnode/vnode/src/vnd/vnodeMgr.c +++ b/source/dnode/vnode/src/vnd/vnodeMgr.c @@ -31,17 +31,17 @@ int vnodeInit(const SVnodeOpt *pOption) { // Start commit handers if (pOption->nthreads > 0) { vnodeMgr.nthreads = pOption->nthreads; - vnodeMgr.threads = (pthread_t*)calloc(pOption->nthreads, sizeof(pthread_t)); + vnodeMgr.threads = (TdThread*)calloc(pOption->nthreads, sizeof(TdThread)); if (vnodeMgr.threads == NULL) { return -1; } - pthread_mutex_init(&(vnodeMgr.mutex), NULL); - pthread_cond_init(&(vnodeMgr.hasTask), NULL); + taosThreadMutexInit(&(vnodeMgr.mutex), NULL); + taosThreadCondInit(&(vnodeMgr.hasTask), NULL); TD_DLIST_INIT(&(vnodeMgr.queue)); for (uint16_t i = 0; i < pOption->nthreads; i++) { - pthread_create(&(vnodeMgr.threads[i]), NULL, loop, NULL); + taosThreadCreate(&(vnodeMgr.threads[i]), NULL, loop, NULL); // pthread_setname_np(vnodeMgr.threads[i], "VND Commit Thread"); } } else { @@ -63,28 +63,28 @@ void vnodeCleanup() { } // Stop commit handler - pthread_mutex_lock(&(vnodeMgr.mutex)); + taosThreadMutexLock(&(vnodeMgr.mutex)); vnodeMgr.stop = true; - pthread_cond_broadcast(&(vnodeMgr.hasTask)); - pthread_mutex_unlock(&(vnodeMgr.mutex)); + taosThreadCondBroadcast(&(vnodeMgr.hasTask)); + taosThreadMutexUnlock(&(vnodeMgr.mutex)); for (uint16_t i = 0; i < vnodeMgr.nthreads; i++) { - pthread_join(vnodeMgr.threads[i], NULL); + taosThreadJoin(vnodeMgr.threads[i], NULL); } tfree(vnodeMgr.threads); - pthread_cond_destroy(&(vnodeMgr.hasTask)); - pthread_mutex_destroy(&(vnodeMgr.mutex)); + taosThreadCondDestroy(&(vnodeMgr.hasTask)); + taosThreadMutexDestroy(&(vnodeMgr.mutex)); } int vnodeScheduleTask(SVnodeTask* pTask) { - pthread_mutex_lock(&(vnodeMgr.mutex)); + taosThreadMutexLock(&(vnodeMgr.mutex)); TD_DLIST_APPEND(&(vnodeMgr.queue), pTask); - pthread_cond_signal(&(vnodeMgr.hasTask)); + taosThreadCondSignal(&(vnodeMgr.hasTask)); - pthread_mutex_unlock(&(vnodeMgr.mutex)); + taosThreadMutexUnlock(&(vnodeMgr.mutex)); return 0; } @@ -107,15 +107,15 @@ static void* loop(void* arg) { SVnodeTask* pTask; for (;;) { - pthread_mutex_lock(&(vnodeMgr.mutex)); + taosThreadMutexLock(&(vnodeMgr.mutex)); for (;;) { pTask = TD_DLIST_HEAD(&(vnodeMgr.queue)); if (pTask == NULL) { if (vnodeMgr.stop) { - pthread_mutex_unlock(&(vnodeMgr.mutex)); + taosThreadMutexUnlock(&(vnodeMgr.mutex)); return NULL; } else { - pthread_cond_wait(&(vnodeMgr.hasTask), &(vnodeMgr.mutex)); + taosThreadCondWait(&(vnodeMgr.hasTask), &(vnodeMgr.mutex)); } } else { TD_DLIST_POP(&(vnodeMgr.queue), pTask); @@ -123,7 +123,7 @@ static void* loop(void* arg) { } } - pthread_mutex_unlock(&(vnodeMgr.mutex)); + taosThreadMutexUnlock(&(vnodeMgr.mutex)); (*(pTask->execute))(pTask->arg); free(pTask); diff --git a/source/dnode/vnode/test/tsdbSmaTest.cpp b/source/dnode/vnode/test/tsdbSmaTest.cpp index bdeb51c018..8140113e67 100644 --- a/source/dnode/vnode/test/tsdbSmaTest.cpp +++ b/source/dnode/vnode/test/tsdbSmaTest.cpp @@ -14,13 +14,13 @@ */ #include +#include #include #include #include #include #include -#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wwrite-strings" diff --git a/source/libs/catalog/inc/catalogInt.h b/source/libs/catalog/inc/catalogInt.h index c4f1a117fe..60c5886250 100644 --- a/source/libs/catalog/inc/catalogInt.h +++ b/source/libs/catalog/inc/catalogInt.h @@ -176,7 +176,7 @@ typedef struct SCatalogMgmt { SCtgQNode *tail; tsem_t sem; uint64_t qRemainNum; - pthread_t updateThread; + TdThread updateThread; SHashObj *pCluster; //key: clusterId, value: SCatalog* SCatalogStat stat; SCatalogCfg cfg; diff --git a/source/libs/catalog/src/catalog.c b/source/libs/catalog/src/catalog.c index 08e0ac55d7..fbb2903a8e 100644 --- a/source/libs/catalog/src/catalog.c +++ b/source/libs/catalog/src/catalog.c @@ -2070,16 +2070,16 @@ void* ctgUpdateThreadFunc(void* param) { int32_t ctgStartUpdateThread() { - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&gCtgMgmt.updateThread, &thAttr, ctgUpdateThreadFunc, NULL) != 0) { + if (taosThreadCreate(&gCtgMgmt.updateThread, &thAttr, ctgUpdateThreadFunc, NULL) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); CTG_ERR_RET(terrno); } - pthread_attr_destroy(&thAttr); + taosThreadAttrDestroy(&thAttr); return TSDB_CODE_SUCCESS; } diff --git a/source/libs/catalog/test/catalogTests.cpp b/source/libs/catalog/test/catalogTests.cpp index cc0e5bb1a9..4aa18c3bab 100644 --- a/source/libs/catalog/test/catalogTests.cpp +++ b/source/libs/catalog/test/catalogTests.cpp @@ -2094,14 +2094,14 @@ TEST(multiThread, getSetRmSameDbVgroup) { strcpy(n.dbname, "db1"); strcpy(n.tname, ctgTestTablename); - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t thread1, thread2; - pthread_create(&(thread1), &thattr, ctgTestSetSameDbVgroupThread, pCtg); + TdThread thread1, thread2; + taosThreadCreate(&(thread1), &thattr, ctgTestSetSameDbVgroupThread, pCtg); taosSsleep(1); - pthread_create(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg); + taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg); while (true) { if (ctgTestDeadLoop) { @@ -2146,14 +2146,14 @@ TEST(multiThread, getSetRmDiffDbVgroup) { strcpy(n.dbname, "db1"); strcpy(n.tname, ctgTestTablename); - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t thread1, thread2; - pthread_create(&(thread1), &thattr, ctgTestSetDiffDbVgroupThread, pCtg); + TdThread thread1, thread2; + taosThreadCreate(&(thread1), &thattr, ctgTestSetDiffDbVgroupThread, pCtg); taosSsleep(1); - pthread_create(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg); + taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg); while (true) { if (ctgTestDeadLoop) { @@ -2198,13 +2198,13 @@ TEST(multiThread, ctableMeta) { strcpy(n.dbname, "db1"); strcpy(n.tname, ctgTestTablename); - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t thread1, thread2; - pthread_create(&(thread1), &thattr, ctgTestSetCtableMetaThread, pCtg); + TdThread thread1, thread2; + taosThreadCreate(&(thread1), &thattr, ctgTestSetCtableMetaThread, pCtg); taosSsleep(1); - pthread_create(&(thread1), &thattr, ctgTestGetCtableMetaThread, pCtg); + taosThreadCreate(&(thread1), &thattr, ctgTestGetCtableMetaThread, pCtg); while (true) { if (ctgTestDeadLoop) { diff --git a/source/libs/executor/inc/dataSinkInt.h b/source/libs/executor/inc/dataSinkInt.h index 8acb6f7e8d..85356a862c 100644 --- a/source/libs/executor/inc/dataSinkInt.h +++ b/source/libs/executor/inc/dataSinkInt.h @@ -29,7 +29,7 @@ struct SDataSinkHandle; typedef struct SDataSinkManager { SDataSinkMgtCfg cfg; - pthread_mutex_t mutex; + TdThreadMutex mutex; } SDataSinkManager; typedef int32_t (*FPutDataBlock)(struct SDataSinkHandle* pHandle, const SInputData* pInput, bool* pContinue); diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 833ac13226..0399a78b09 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -356,7 +356,7 @@ typedef struct SQInfo { STaskAttr query; void* pBuf; // allocated buffer for STableQueryInfo, sizeof(STableQueryInfo)*numOfTables; - pthread_mutex_t lock; // used to synchronize the rsp/query threads + TdThreadMutex lock; // used to synchronize the rsp/query threads tsem_t ready; int32_t dataReady; // denote if query result is ready or not void* rspContext; // response context diff --git a/source/libs/executor/src/dataDispatcher.c b/source/libs/executor/src/dataDispatcher.c index a0ee048d82..6ad457a91b 100644 --- a/source/libs/executor/src/dataDispatcher.c +++ b/source/libs/executor/src/dataDispatcher.c @@ -43,7 +43,7 @@ typedef struct SDataDispatchHandle { int32_t status; bool queryEnd; uint64_t useconds; - pthread_mutex_t mutex; + TdThreadMutex mutex; } SDataDispatchHandle; static bool needCompress(const SSDataBlock* pData, const SDataBlockDescNode* pSchema) { @@ -126,19 +126,19 @@ static bool allocBuf(SDataDispatchHandle* pDispatcher, const SInputData* pInput, } static int32_t updateStatus(SDataDispatchHandle* pDispatcher) { - pthread_mutex_lock(&pDispatcher->mutex); + taosThreadMutexLock(&pDispatcher->mutex); int32_t blockNums = taosQueueSize(pDispatcher->pDataBlocks); int32_t status = (0 == blockNums ? DS_BUF_EMPTY : (blockNums < pDispatcher->pManager->cfg.maxDataBlockNumPerQuery ? DS_BUF_LOW : DS_BUF_FULL)); pDispatcher->status = status; - pthread_mutex_unlock(&pDispatcher->mutex); + taosThreadMutexUnlock(&pDispatcher->mutex); return status; } static int32_t getStatus(SDataDispatchHandle* pDispatcher) { - pthread_mutex_lock(&pDispatcher->mutex); + taosThreadMutexLock(&pDispatcher->mutex); int32_t status = pDispatcher->status; - pthread_mutex_unlock(&pDispatcher->mutex); + taosThreadMutexUnlock(&pDispatcher->mutex); return status; } @@ -156,10 +156,10 @@ static int32_t putDataBlock(SDataSinkHandle* pHandle, const SInputData* pInput, static void endPut(struct SDataSinkHandle* pHandle, uint64_t useconds) { SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle; - pthread_mutex_lock(&pDispatcher->mutex); + taosThreadMutexLock(&pDispatcher->mutex); pDispatcher->queryEnd = true; pDispatcher->useconds = useconds; - pthread_mutex_unlock(&pDispatcher->mutex); + taosThreadMutexUnlock(&pDispatcher->mutex); } static void getDataLength(SDataSinkHandle* pHandle, int32_t* pLen, bool* pQueryEnd) { @@ -191,11 +191,11 @@ static int32_t getDataBlock(SDataSinkHandle* pHandle, SOutputData* pOutput) { pOutput->compressed = pEntry->compressed; tfree(pDispatcher->nextOutput.pData); // todo persistent pOutput->bufStatus = updateStatus(pDispatcher); - pthread_mutex_lock(&pDispatcher->mutex); + taosThreadMutexLock(&pDispatcher->mutex); pOutput->queryEnd = pDispatcher->queryEnd; pOutput->useconds = pDispatcher->useconds; pOutput->precision = pDispatcher->pSchema->precision; - pthread_mutex_unlock(&pDispatcher->mutex); + taosThreadMutexUnlock(&pDispatcher->mutex); return TSDB_CODE_SUCCESS; } @@ -209,7 +209,7 @@ static int32_t destroyDataSinker(SDataSinkHandle* pHandle) { taosFreeQitem(pBuf); } taosCloseQueue(pDispatcher->pDataBlocks); - pthread_mutex_destroy(&pDispatcher->mutex); + taosThreadMutexDestroy(&pDispatcher->mutex); } int32_t createDataDispatcher(SDataSinkManager* pManager, const SDataSinkNode* pDataSink, DataSinkHandle* pHandle) { @@ -228,7 +228,7 @@ int32_t createDataDispatcher(SDataSinkManager* pManager, const SDataSinkNode* pD dispatcher->status = DS_BUF_EMPTY; dispatcher->queryEnd = false; dispatcher->pDataBlocks = taosOpenQueue(); - pthread_mutex_init(&dispatcher->mutex, NULL); + taosThreadMutexInit(&dispatcher->mutex, NULL); if (NULL == dispatcher->pDataBlocks) { terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; return TSDB_CODE_QRY_OUT_OF_MEMORY; diff --git a/source/libs/executor/src/dataSinkMgt.c b/source/libs/executor/src/dataSinkMgt.c index 4e8583eb2a..997397314f 100644 --- a/source/libs/executor/src/dataSinkMgt.c +++ b/source/libs/executor/src/dataSinkMgt.c @@ -22,7 +22,7 @@ static SDataSinkManager gDataSinkManager = {0}; int32_t dsDataSinkMgtInit(SDataSinkMgtCfg *cfg) { gDataSinkManager.cfg = *cfg; - pthread_mutex_init(&gDataSinkManager.mutex, NULL); + taosThreadMutexInit(&gDataSinkManager.mutex, NULL); return 0; // to avoid compiler eror } diff --git a/source/libs/executor/src/executorMain.c b/source/libs/executor/src/executorMain.c index 7e55a4b3e1..cc9921ce73 100644 --- a/source/libs/executor/src/executorMain.c +++ b/source/libs/executor/src/executorMain.c @@ -30,7 +30,7 @@ #include "query.h" typedef struct STaskMgmt { - pthread_mutex_t lock; + TdThreadMutex lock; SCacheObj *qinfoPool; // query handle pool int32_t vgId; bool closed; diff --git a/source/libs/function/inc/tscript.h b/source/libs/function/inc/tscript.h index 281fe6f679..823e2d61f2 100644 --- a/source/libs/function/inc/tscript.h +++ b/source/libs/function/inc/tscript.h @@ -70,7 +70,7 @@ typedef struct { SList *scriptEnvs; // int32_t mSize; // pool limit int32_t cSize; // current available size - pthread_mutex_t mutex; + TdThreadMutex mutex; } ScriptEnvPool; ScriptCtx* createScriptCtx(char *str, int8_t resType, int16_t resBytes); diff --git a/source/libs/function/src/functionMgt.c b/source/libs/function/src/functionMgt.c index 41b0126a07..f9f2e90770 100644 --- a/source/libs/function/src/functionMgt.c +++ b/source/libs/function/src/functionMgt.c @@ -26,7 +26,7 @@ typedef struct SFuncMgtService { } SFuncMgtService; static SFuncMgtService gFunMgtService; -static pthread_once_t functionHashTableInit = PTHREAD_ONCE_INIT; +static TdThreadOnce functionHashTableInit = PTHREAD_ONCE_INIT; static int32_t initFunctionCode = 0; static void doInitFunctionHashTable() { @@ -45,7 +45,7 @@ static void doInitFunctionHashTable() { } int32_t fmFuncMgtInit() { - pthread_once(&functionHashTableInit, doInitFunctionHashTable); + taosThreadOnce(&functionHashTableInit, doInitFunctionHashTable); return initFunctionCode; } diff --git a/source/libs/function/src/tscript.c b/source/libs/function/src/tscript.c index 7c07b0b783..25cf3cfbb1 100644 --- a/source/libs/function/src/tscript.c +++ b/source/libs/function/src/tscript.c @@ -333,7 +333,7 @@ void destroyLuaEnv(lua_State *lua) { int32_t scriptEnvPoolInit() { const int size = 10; // configure or not pool = malloc(sizeof(ScriptEnvPool)); - pthread_mutex_init(&pool->mutex, NULL); + taosThreadMutexInit(&pool->mutex, NULL); pool->scriptEnvs = tdListNew(sizeof(ScriptEnv *)); for (int i = 0; i < size; i++) { @@ -359,7 +359,7 @@ void scriptEnvPoolCleanup() { listNodeFree(pNode); } tdListFree(pool->scriptEnvs); - pthread_mutex_destroy(&pool->mutex); + taosThreadMutexDestroy(&pool->mutex); free(pool); } @@ -372,9 +372,9 @@ void destroyScriptEnv(ScriptEnv *pEnv) { ScriptEnv* getScriptEnvFromPool() { ScriptEnv *pEnv = NULL; - pthread_mutex_lock(&pool->mutex); + taosThreadMutexLock(&pool->mutex); if (pool->cSize <= 0) { - pthread_mutex_unlock(&pool->mutex); + taosThreadMutexUnlock(&pool->mutex); return NULL; } SListNode *pNode = tdListPopHead(pool->scriptEnvs); @@ -384,7 +384,7 @@ ScriptEnv* getScriptEnvFromPool() { } pool->cSize--; - pthread_mutex_unlock(&pool->mutex); + taosThreadMutexUnlock(&pool->mutex); return pEnv; } @@ -392,11 +392,11 @@ void addScriptEnvToPool(ScriptEnv *pEnv) { if (pEnv == NULL) { return; } - pthread_mutex_lock(&pool->mutex); + taosThreadMutexLock(&pool->mutex); lua_settop(pEnv->lua_state, 0); tdListAppend(pool->scriptEnvs, (void *)(&pEnv)); pool->cSize++; - pthread_mutex_unlock(&pool->mutex); + taosThreadMutexUnlock(&pool->mutex); } bool hasBaseFuncDefinedInScript(lua_State *lua, const char *funcPrefix, int32_t len) { diff --git a/source/libs/index/inc/indexInt.h b/source/libs/index/inc/indexInt.h index 3d1d5356c2..37318767c7 100644 --- a/source/libs/index/inc/indexInt.h +++ b/source/libs/index/inc/indexInt.h @@ -58,7 +58,7 @@ struct SIndex { char* path; SIndexStat stat; - pthread_mutex_t mtx; + TdThreadMutex mtx; }; struct SIndexOpts { diff --git a/source/libs/index/inc/index_cache.h b/source/libs/index/inc/index_cache.h index a6ebcd6d6f..086e75d99f 100644 --- a/source/libs/index/inc/index_cache.h +++ b/source/libs/index/inc/index_cache.h @@ -43,8 +43,8 @@ typedef struct IndexCache { int8_t type; uint64_t suid; - pthread_mutex_t mtx; - pthread_cond_t finished; + TdThreadMutex mtx; + TdThreadCond finished; } IndexCache; #define CACHE_VERSION(cache) atomic_load_32(&cache->version) diff --git a/source/libs/index/inc/index_fst.h b/source/libs/index/inc/index_fst.h index 5da0dc537b..cf5c3f306b 100644 --- a/source/libs/index/inc/index_fst.h +++ b/source/libs/index/inc/index_fst.h @@ -260,7 +260,7 @@ typedef struct Fst { FstMeta* meta; FstSlice* data; // FstNode* root; // - pthread_mutex_t mtx; + TdThreadMutex mtx; } Fst; // refactor simple function diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index d25fed6816..83410306eb 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -45,7 +45,7 @@ typedef struct SIdxColInfo { int cVersion; } SIdxColInfo; -static pthread_once_t isInit = PTHREAD_ONCE_INIT; +static TdThreadOnce isInit = PTHREAD_ONCE_INIT; // static void indexInit(); static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* term, SArray** result); @@ -61,7 +61,7 @@ static void indexMergeCacheAndTFile(SArray* result, IterateValue* icache, Iterat // int32_t indexSerialKey(ICacheKey* key, char* buf); int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) { - pthread_once(&isInit, indexInit); + taosThreadOnce(&isInit, indexInit); SIndex* sIdx = calloc(1, sizeof(SIndex)); if (sIdx == NULL) { return -1; @@ -82,7 +82,7 @@ int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) { sIdx->colObj = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); sIdx->cVersion = 1; sIdx->path = tstrdup(path); - pthread_mutex_init(&sIdx->mtx, NULL); + taosThreadMutexInit(&sIdx->mtx, NULL); *index = sIdx; return 0; #endif @@ -112,7 +112,7 @@ void indexClose(SIndex* sIdx) { iter = taosHashIterate(sIdx->colObj, iter); } taosHashCleanup(sIdx->colObj); - pthread_mutex_destroy(&sIdx->mtx); + taosThreadMutexDestroy(&sIdx->mtx); indexTFileDestroy(sIdx->tindex); #endif free(sIdx->path); @@ -140,7 +140,7 @@ int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { #ifdef USE_INVERTED_INDEX // TODO(yihao): reduce the lock range - pthread_mutex_lock(&index->mtx); + taosThreadMutexLock(&index->mtx); for (int i = 0; i < taosArrayGetSize(fVals); i++) { SIndexTerm* p = taosArrayGetP(fVals, i); @@ -154,7 +154,7 @@ int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { taosHashPut(index->colObj, buf, sz, &pCache, sizeof(void*)); } } - pthread_mutex_unlock(&index->mtx); + taosThreadMutexUnlock(&index->mtx); for (int i = 0; i < taosArrayGetSize(fVals); i++) { SIndexTerm* p = taosArrayGetP(fVals, i); @@ -333,10 +333,10 @@ static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* query, SArray** result .suid = term->suid, .colName = term->colName, .nColName = strlen(term->colName), .colType = term->colType}; int32_t sz = indexSerialCacheKey(&key, buf); - pthread_mutex_lock(&sIdx->mtx); + taosThreadMutexLock(&sIdx->mtx); IndexCache** pCache = taosHashGet(sIdx->colObj, buf, sz); cache = (pCache == NULL) ? NULL : *pCache; - pthread_mutex_unlock(&sIdx->mtx); + taosThreadMutexUnlock(&sIdx->mtx); *result = taosArrayInit(4, sizeof(uint64_t)); // TODO: iterator mem and tidex @@ -564,10 +564,10 @@ static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) { TFileHeader* header = &reader->header; ICacheKey key = {.suid = cache->suid, .colName = header->colName, .nColName = strlen(header->colName)}; - pthread_mutex_lock(&sIdx->mtx); + taosThreadMutexLock(&sIdx->mtx); IndexTFile* ifile = (IndexTFile*)sIdx->tindex; tfileCachePut(ifile->cache, &key, reader); - pthread_mutex_unlock(&sIdx->mtx); + taosThreadMutexUnlock(&sIdx->mtx); return ret; END: if (tw != NULL) { diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index 34f009dd7e..2b4327b091 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -54,8 +54,8 @@ IndexCache* indexCacheCreate(SIndex* idx, uint64_t suid, const char* colName, in cache->suid = suid; cache->occupiedMem = 0; - pthread_mutex_init(&cache->mtx, NULL); - pthread_cond_init(&cache->finished, NULL); + taosThreadMutexInit(&cache->mtx, NULL); + taosThreadCondInit(&cache->finished, NULL); indexCacheRef(cache); return cache; @@ -63,10 +63,10 @@ IndexCache* indexCacheCreate(SIndex* idx, uint64_t suid, const char* colName, in void indexCacheDebug(IndexCache* cache) { MemTable* tbl = NULL; - pthread_mutex_lock(&cache->mtx); + taosThreadMutexLock(&cache->mtx); tbl = cache->mem; indexMemRef(tbl); - pthread_mutex_unlock(&cache->mtx); + taosThreadMutexUnlock(&cache->mtx); { SSkipList* slt = tbl->mem; @@ -85,10 +85,10 @@ void indexCacheDebug(IndexCache* cache) { } { - pthread_mutex_lock(&cache->mtx); + taosThreadMutexLock(&cache->mtx); tbl = cache->imm; indexMemRef(tbl); - pthread_mutex_unlock(&cache->mtx); + taosThreadMutexUnlock(&cache->mtx); if (tbl != NULL) { SSkipList* slt = tbl->mem; SSkipListIterator* iter = tSkipListCreateIter(slt); @@ -126,13 +126,13 @@ void indexCacheDestroyImm(IndexCache* cache) { } MemTable* tbl = NULL; - pthread_mutex_lock(&cache->mtx); + taosThreadMutexLock(&cache->mtx); tbl = cache->imm; cache->imm = NULL; // or throw int bg thread - pthread_cond_broadcast(&cache->finished); + taosThreadCondBroadcast(&cache->finished); - pthread_mutex_unlock(&cache->mtx); + taosThreadMutexUnlock(&cache->mtx); indexMemUnRef(tbl); indexMemUnRef(tbl); @@ -146,8 +146,8 @@ void indexCacheDestroy(void* cache) { indexMemUnRef(pCache->imm); free(pCache->colName); - pthread_mutex_destroy(&pCache->mtx); - pthread_cond_destroy(&pCache->finished); + taosThreadMutexDestroy(&pCache->mtx); + taosThreadCondDestroy(&pCache->finished); free(pCache); } @@ -158,7 +158,7 @@ Iterate* indexCacheIteratorCreate(IndexCache* cache) { return NULL; } - pthread_mutex_lock(&cache->mtx); + taosThreadMutexLock(&cache->mtx); indexMemRef(cache->imm); @@ -169,7 +169,7 @@ Iterate* indexCacheIteratorCreate(IndexCache* cache) { iiter->next = indexCacheIteratorNext; iiter->getValue = indexCacheIteratorGetValue; - pthread_mutex_unlock(&cache->mtx); + taosThreadMutexUnlock(&cache->mtx); return iiter; } @@ -200,7 +200,7 @@ static void indexCacheMakeRoomForWrite(IndexCache* cache) { break; } else if (cache->imm != NULL) { // TODO: wake up by condition variable - pthread_cond_wait(&cache->finished, &cache->mtx); + taosThreadCondWait(&cache->finished, &cache->mtx); } else { indexCacheRef(cache); cache->imm = cache->mem; @@ -240,7 +240,7 @@ int indexCachePut(void* cache, SIndexTerm* term, uint64_t uid) { // ugly code, refactor later int64_t estimate = sizeof(ct) + strlen(ct->colVal); - pthread_mutex_lock(&pCache->mtx); + taosThreadMutexLock(&pCache->mtx); pCache->occupiedMem += estimate; indexCacheMakeRoomForWrite(pCache); MemTable* tbl = pCache->mem; @@ -248,7 +248,7 @@ int indexCachePut(void* cache, SIndexTerm* term, uint64_t uid) { tSkipListPut(tbl->mem, (char*)ct); indexMemUnRef(tbl); - pthread_mutex_unlock(&pCache->mtx); + taosThreadMutexUnlock(&pCache->mtx); indexCacheUnRef(pCache); return 0; @@ -299,12 +299,12 @@ int indexCacheSearch(void* cache, SIndexTermQuery* query, SIdxTempResult* result IndexCache* pCache = cache; MemTable *mem = NULL, *imm = NULL; - pthread_mutex_lock(&pCache->mtx); + taosThreadMutexLock(&pCache->mtx); mem = pCache->mem; imm = pCache->imm; indexMemRef(mem); indexMemRef(imm); - pthread_mutex_unlock(&pCache->mtx); + taosThreadMutexUnlock(&pCache->mtx); SIndexTerm* term = query->term; EIndexQueryType qtype = query->qType; diff --git a/source/libs/index/src/index_fst.c b/source/libs/index/src/index_fst.c index 18cde151d2..58d5a871cd 100644 --- a/source/libs/index/src/index_fst.c +++ b/source/libs/index/src/index_fst.c @@ -1023,7 +1023,7 @@ Fst* fstCreate(FstSlice* slice) { *s = fstSliceCopy(slice, 0, FST_SLICE_LEN(slice) - 1); fst->data = s; - pthread_mutex_init(&fst->mtx, NULL); + taosThreadMutexInit(&fst->mtx, NULL); return fst; FST_CREAT_FAILED: @@ -1037,14 +1037,14 @@ void fstDestroy(Fst* fst) { free(fst->meta); fstSliceDestroy(fst->data); free(fst->data); - pthread_mutex_destroy(&fst->mtx); + taosThreadMutexDestroy(&fst->mtx); } free(fst); } bool fstGet(Fst* fst, FstSlice* b, Output* out) { // dec lock range - // pthread_mutex_lock(&fst->mtx); + // taosThreadMutexLock(&fst->mtx); FstNode* root = fstGetRoot(fst); Output tOut = 0; int32_t len; @@ -1057,7 +1057,7 @@ bool fstGet(Fst* fst, FstSlice* b, Output* out) { uint8_t inp = data[i]; Output res = 0; if (false == fstNodeFindInput(root, inp, &res)) { - // pthread_mutex_unlock(&fst->mtx); + // taosThreadMutexUnlock(&fst->mtx); return false; } @@ -1068,7 +1068,7 @@ bool fstGet(Fst* fst, FstSlice* b, Output* out) { taosArrayPush(nodes, &root); } if (!FST_NODE_IS_FINAL(root)) { - // pthread_mutex_unlock(&fst->mtx); + // taosThreadMutexUnlock(&fst->mtx); return false; } else { tOut = tOut + FST_NODE_FINAL_OUTPUT(root); @@ -1080,7 +1080,7 @@ bool fstGet(Fst* fst, FstSlice* b, Output* out) { } taosArrayDestroy(nodes); // fst->root = NULL; - // pthread_mutex_unlock(&fst->mtx); + // taosThreadMutexUnlock(&fst->mtx); *out = tOut; return true; } diff --git a/source/libs/monitor/inc/monInt.h b/source/libs/monitor/inc/monInt.h index 6ef901410b..452c38f66b 100644 --- a/source/libs/monitor/inc/monInt.h +++ b/source/libs/monitor/inc/monInt.h @@ -48,7 +48,7 @@ typedef struct SMonInfo { } SMonInfo; typedef struct { - pthread_mutex_t lock; + TdThreadMutex lock; SArray *logs; // array of SMonLogItem int32_t maxLogs; const char *server; diff --git a/source/libs/monitor/src/monitor.c b/source/libs/monitor/src/monitor.c index 352b59e931..aa5eafd8b2 100644 --- a/source/libs/monitor/src/monitor.c +++ b/source/libs/monitor/src/monitor.c @@ -23,7 +23,7 @@ static SMonitor tsMonitor = {0}; void monRecordLog(int64_t ts, ELogLevel level, const char *content) { - pthread_mutex_lock(&tsMonitor.lock); + taosThreadMutexLock(&tsMonitor.lock); int32_t size = taosArrayGetSize(tsMonitor.logs); if (size < tsMonitor.maxLogs) { SMonLogItem item = {.ts = ts, .level = level}; @@ -32,7 +32,7 @@ void monRecordLog(int64_t ts, ELogLevel level, const char *content) { tstrncpy(pItem->content, content, MON_LOG_LEN); } } - pthread_mutex_unlock(&tsMonitor.lock); + taosThreadMutexUnlock(&tsMonitor.lock); } int32_t monInit(const SMonCfg *pCfg) { @@ -48,7 +48,7 @@ int32_t monInit(const SMonCfg *pCfg) { tsMonitor.comp = pCfg->comp; tsLogFp = monRecordLog; tsMonitor.state.time = taosGetTimestampMs(); - pthread_mutex_init(&tsMonitor.lock, NULL); + taosThreadMutexInit(&tsMonitor.lock, NULL); return 0; } @@ -56,7 +56,7 @@ void monCleanup() { tsLogFp = NULL; taosArrayDestroy(tsMonitor.logs); tsMonitor.logs = NULL; - pthread_mutex_destroy(&tsMonitor.lock); + taosThreadMutexDestroy(&tsMonitor.lock); } SMonInfo *monCreateMonitorInfo() { @@ -66,10 +66,10 @@ SMonInfo *monCreateMonitorInfo() { return NULL; } - pthread_mutex_lock(&tsMonitor.lock); + taosThreadMutexLock(&tsMonitor.lock); pMonitor->logs = taosArrayDup(tsMonitor.logs); taosArrayClear(tsMonitor.logs); - pthread_mutex_unlock(&tsMonitor.lock); + taosThreadMutexUnlock(&tsMonitor.lock); pMonitor->pJson = tjsonCreateObject(); if (pMonitor->pJson == NULL || pMonitor->logs == NULL) { diff --git a/source/libs/parser/src/parTokenizer.c b/source/libs/parser/src/parTokenizer.c index 4549660c76..3cd4549ad0 100644 --- a/source/libs/parser/src/parTokenizer.c +++ b/source/libs/parser/src/parTokenizer.c @@ -266,10 +266,10 @@ static void doInitKeywordsTable(void) { } } -static pthread_once_t keywordsHashTableInit = PTHREAD_ONCE_INIT; +static TdThreadOnce keywordsHashTableInit = PTHREAD_ONCE_INIT; static int32_t tKeywordCode(const char* z, int n) { - pthread_once(&keywordsHashTableInit, doInitKeywordsTable); + taosThreadOnce(&keywordsHashTableInit, doInitKeywordsTable); char key[512] = {0}; if (n > tListLen(key)) { // too long token, can not be any other token type diff --git a/source/libs/qworker/test/qworkerTests.cpp b/source/libs/qworker/test/qworkerTests.cpp index d7814a5dfb..a45ec41e6c 100644 --- a/source/libs/qworker/test/qworkerTests.cpp +++ b/source/libs/qworker/test/qworkerTests.cpp @@ -1030,15 +1030,15 @@ TEST(seqTest, multithreadRand) { code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue); ASSERT_EQ(code, 0); - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t t1,t2,t3,t4,t5; - pthread_create(&(t1), &thattr, queryThread, mgmt); - pthread_create(&(t2), &thattr, readyThread, NULL); - pthread_create(&(t3), &thattr, fetchThread, NULL); - pthread_create(&(t4), &thattr, dropThread, NULL); - pthread_create(&(t5), &thattr, statusThread, NULL); + TdThread t1,t2,t3,t4,t5; + taosThreadCreate(&(t1), &thattr, queryThread, mgmt); + taosThreadCreate(&(t2), &thattr, readyThread, NULL); + taosThreadCreate(&(t3), &thattr, fetchThread, NULL); + taosThreadCreate(&(t4), &thattr, dropThread, NULL); + taosThreadCreate(&(t5), &thattr, statusThread, NULL); while (true) { if (qwtTestDeadLoop) { @@ -1089,13 +1089,13 @@ TEST(rcTest, shortExecshortDelay) { tsem_init(&qwtTestQuerySem, 0, 0); tsem_init(&qwtTestFetchSem, 0, 0); - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t t1,t2,t3,t4,t5; - pthread_create(&(t1), &thattr, qwtclientThread, mgmt); - pthread_create(&(t2), &thattr, queryQueueThread, mgmt); - pthread_create(&(t3), &thattr, fetchQueueThread, mgmt); + TdThread t1,t2,t3,t4,t5; + taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt); + taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt); + taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt); while (true) { if (qwtTestDeadLoop) { @@ -1170,13 +1170,13 @@ TEST(rcTest, longExecshortDelay) { tsem_init(&qwtTestQuerySem, 0, 0); tsem_init(&qwtTestFetchSem, 0, 0); - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t t1,t2,t3,t4,t5; - pthread_create(&(t1), &thattr, qwtclientThread, mgmt); - pthread_create(&(t2), &thattr, queryQueueThread, mgmt); - pthread_create(&(t3), &thattr, fetchQueueThread, mgmt); + TdThread t1,t2,t3,t4,t5; + taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt); + taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt); + taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt); while (true) { if (qwtTestDeadLoop) { @@ -1253,13 +1253,13 @@ TEST(rcTest, shortExeclongDelay) { tsem_init(&qwtTestQuerySem, 0, 0); tsem_init(&qwtTestFetchSem, 0, 0); - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t t1,t2,t3,t4,t5; - pthread_create(&(t1), &thattr, qwtclientThread, mgmt); - pthread_create(&(t2), &thattr, queryQueueThread, mgmt); - pthread_create(&(t3), &thattr, fetchQueueThread, mgmt); + TdThread t1,t2,t3,t4,t5; + taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt); + taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt); + taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt); while (true) { if (qwtTestDeadLoop) { @@ -1332,13 +1332,13 @@ TEST(rcTest, dropTest) { tsem_init(&qwtTestQuerySem, 0, 0); tsem_init(&qwtTestFetchSem, 0, 0); - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t t1,t2,t3,t4,t5; - pthread_create(&(t1), &thattr, clientThread, mgmt); - pthread_create(&(t2), &thattr, queryQueueThread, mgmt); - pthread_create(&(t3), &thattr, fetchQueueThread, mgmt); + TdThread t1,t2,t3,t4,t5; + taosThreadCreate(&(t1), &thattr, clientThread, mgmt); + taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt); + taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt); while (true) { if (qwtTestDeadLoop) { diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index 376ed1e2bc..b5bd68ba86 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -684,11 +684,11 @@ TEST(queryTest, normalCase) { pIter = taosHashIterate(pJob->execTasks, pIter); } - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t thread1; - pthread_create(&(thread1), &thattr, schtCreateFetchRspThread, &job); + TdThread thread1; + taosThreadCreate(&(thread1), &thattr, schtCreateFetchRspThread, &job); void *data = NULL; code = schedulerFetchRows(job, &data); @@ -780,11 +780,11 @@ TEST(queryTest, flowCtrlCase) { } - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t thread1; - pthread_create(&(thread1), &thattr, schtCreateFetchRspThread, &job); + TdThread thread1; + taosThreadCreate(&(thread1), &thattr, schtCreateFetchRspThread, &job); void *data = NULL; code = schedulerFetchRows(job, &data); @@ -834,11 +834,11 @@ TEST(insertTest, normalCase) { schtSetPlanToString(); schtSetAsyncSendMsgToServer(); - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t thread1; - pthread_create(&(thread1), &thattr, schtSendRsp, &insertJobRefId); + TdThread thread1; + taosThreadCreate(&(thread1), &thattr, schtSendRsp, &insertJobRefId); SQueryResult res = {0}; code = schedulerExecJob(mockPointer, qnodeList, &dag, &insertJobRefId, "insert into tb values(now,1)", &res); @@ -851,13 +851,13 @@ TEST(insertTest, normalCase) { } TEST(multiThread, forceFree) { - pthread_attr_t thattr; - pthread_attr_init(&thattr); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); - pthread_t thread1, thread2, thread3; - pthread_create(&(thread1), &thattr, schtRunJobThread, NULL); - pthread_create(&(thread2), &thattr, schtFreeJobThread, NULL); - pthread_create(&(thread3), &thattr, schtFetchRspThread, NULL); + TdThread thread1, thread2, thread3; + taosThreadCreate(&(thread1), &thattr, schtRunJobThread, NULL); + taosThreadCreate(&(thread2), &thattr, schtFreeJobThread, NULL); + taosThreadCreate(&(thread3), &thattr, schtFetchRspThread, NULL); while (true) { if (schtTestDeadLoop) { diff --git a/source/libs/sync/inc/syncIO.h b/source/libs/sync/inc/syncIO.h index 5a4b7555bf..0185d5f211 100644 --- a/source/libs/sync/inc/syncIO.h +++ b/source/libs/sync/inc/syncIO.h @@ -35,7 +35,7 @@ extern "C" { typedef struct SSyncIO { STaosQueue *pMsgQ; STaosQset * pQset; - pthread_t consumerTid; + TdThread consumerTid; void * serverRpc; void * clientRpc; diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index c841c2ac06..19121632e2 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -211,7 +211,7 @@ static int32_t syncIOStartInternal(SSyncIO *io) { // start consumer thread { - if (pthread_create(&io->consumerTid, NULL, syncIOConsumerFunc, io) != 0) { + if (taosThreadCreate(&io->consumerTid, NULL, syncIOConsumerFunc, io) != 0) { sError("failed to create sync consumer thread since %s", strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; @@ -228,7 +228,7 @@ static int32_t syncIOStartInternal(SSyncIO *io) { static int32_t syncIOStopInternal(SSyncIO *io) { int32_t ret = 0; atomic_store_8(&io->isStart, 0); - pthread_join(io->consumerTid, NULL); + taosThreadJoin(io->consumerTid, NULL); taosTmrCleanUp(io->timerMgr); return ret; } diff --git a/source/libs/tdb/src/db/tdbPCache.c b/source/libs/tdb/src/db/tdbPCache.c index 29f7395cbc..9d7181c1da 100644 --- a/source/libs/tdb/src/db/tdbPCache.c +++ b/source/libs/tdb/src/db/tdbPCache.c @@ -17,7 +17,7 @@ struct SPCache { int pageSize; int cacheSize; - pthread_mutex_t mutex; + TdThreadMutex mutex; int nFree; SPage *pFree; int nPage; @@ -118,13 +118,13 @@ void tdbPCacheRelease(SPage *pPage) { } } -static void tdbPCacheInitLock(SPCache *pCache) { pthread_mutex_init(&(pCache->mutex), NULL); } +static void tdbPCacheInitLock(SPCache *pCache) { taosThreadMutexInit(&(pCache->mutex), NULL); } -static void tdbPCacheClearLock(SPCache *pCache) { pthread_mutex_destroy(&(pCache->mutex)); } +static void tdbPCacheClearLock(SPCache *pCache) { taosThreadMutexDestroy(&(pCache->mutex)); } -static void tdbPCacheLock(SPCache *pCache) { pthread_mutex_lock(&(pCache->mutex)); } +static void tdbPCacheLock(SPCache *pCache) { taosThreadMutexLock(&(pCache->mutex)); } -static void tdbPCacheUnlock(SPCache *pCache) { pthread_mutex_unlock(&(pCache->mutex)); } +static void tdbPCacheUnlock(SPCache *pCache) { taosThreadMutexUnlock(&(pCache->mutex)); } static bool tdbPCacheLocked(SPCache *pCache) { assert(0); diff --git a/source/libs/tdb/src/inc/tdbPage.h b/source/libs/tdb/src/inc/tdbPage.h index 6479574abe..8a51c331b6 100644 --- a/source/libs/tdb/src/inc/tdbPage.h +++ b/source/libs/tdb/src/inc/tdbPage.h @@ -53,7 +53,7 @@ typedef struct __attribute__((__packed__)) { } SPageFtr; struct SPage { - pthread_spinlock_t lock; + TdThreadSpinlock lock; u8 *pData; int pageSize; SPageMethods *pPageMethods; @@ -101,10 +101,10 @@ struct SPage { #define P_LOCK_BUSY 1 #define P_LOCK_FAIL -1 -#define TDB_INIT_PAGE_LOCK(pPage) pthread_spin_init(&((pPage)->lock), 0) -#define TDB_DESTROY_PAGE_LOCK(pPage) pthread_spin_destroy(&((pPage)->lock)) -#define TDB_LOCK_PAGE(pPage) pthread_spin_lock(&((pPage)->lock)) -#define TDB_UNLOCK_PAGE(pPage) pthread_spin_unlock(&((pPage)->lock)) +#define TDB_INIT_PAGE_LOCK(pPage) taosThreadSpinInit(&((pPage)->lock), 0) +#define TDB_DESTROY_PAGE_LOCK(pPage) taosThreadSpinDestroy(&((pPage)->lock)) +#define TDB_LOCK_PAGE(pPage) taosThreadSpinLock(&((pPage)->lock)) +#define TDB_UNLOCK_PAGE(pPage) taosThreadSpinUnlock(&((pPage)->lock)) #define TDB_TRY_LOCK_PAGE(pPage) \ ({ \ int ret; \ diff --git a/source/libs/tfs/inc/tfsInt.h b/source/libs/tfs/inc/tfsInt.h index f16d0445c6..2a508cf676 100644 --- a/source/libs/tfs/inc/tfsInt.h +++ b/source/libs/tfs/inc/tfsInt.h @@ -41,7 +41,7 @@ typedef struct { } STfsDisk; typedef struct { - pthread_spinlock_t lock; + TdThreadSpinlock lock; int32_t level; int32_t nextid; // next disk id to allocate int32_t ndisk; // # of disks mounted to this tier @@ -64,7 +64,7 @@ typedef struct STfsDir { } STfsDir; typedef struct STfs { - pthread_spinlock_t lock; + TdThreadSpinlock lock; SDiskSize size; int32_t nlevel; STfsTier tiers[TFS_MAX_TIERS]; @@ -82,11 +82,11 @@ void tfsUpdateTierSize(STfsTier *pTier); int32_t tfsAllocDiskOnTier(STfsTier *pTier); void tfsPosNextId(STfsTier *pTier); -#define tfsLockTier(pTier) pthread_spin_lock(&(pTier)->lock) -#define tfsUnLockTier(pTier) pthread_spin_unlock(&(pTier)->lock) +#define tfsLockTier(pTier) taosThreadSpinLock(&(pTier)->lock) +#define tfsUnLockTier(pTier) taosThreadSpinUnlock(&(pTier)->lock) -#define tfsLock(pTfs) pthread_spin_lock(&(pTfs)->lock) -#define tfsUnLock(pTfs) pthread_spin_unlock(&(pTfs)->lock) +#define tfsLock(pTfs) taosThreadSpinLock(&(pTfs)->lock) +#define tfsUnLock(pTfs) taosThreadSpinUnlock(&(pTfs)->lock) #define TFS_TIER_AT(pTfs, level) (&(pTfs)->tiers[level]) #define TFS_DISK_AT(pTfs, did) ((pTfs)->tiers[(did).level].disks[(did).id]) diff --git a/source/libs/tfs/src/tfs.c b/source/libs/tfs/src/tfs.c index aee7376491..d30b41edfe 100644 --- a/source/libs/tfs/src/tfs.c +++ b/source/libs/tfs/src/tfs.c @@ -36,7 +36,7 @@ STfs *tfsOpen(SDiskCfg *pCfg, int32_t ndisk) { return NULL; } - if (pthread_spin_init(&pTfs->lock, 0) != 0) { + if (taosThreadSpinInit(&pTfs->lock, 0) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); tfsClose(pTfs); return NULL; @@ -85,7 +85,7 @@ void tfsClose(STfs *pTfs) { } taosHashCleanup(pTfs->hash); - pthread_spin_destroy(&pTfs->lock); + taosThreadSpinDestroy(&pTfs->lock); free(pTfs); } diff --git a/source/libs/tfs/src/tfsTier.c b/source/libs/tfs/src/tfsTier.c index e4390d13d1..2ad021cbb5 100644 --- a/source/libs/tfs/src/tfsTier.c +++ b/source/libs/tfs/src/tfsTier.c @@ -19,7 +19,7 @@ int32_t tfsInitTier(STfsTier *pTier, int32_t level) { memset(pTier, 0, sizeof(STfsTier)); - if (pthread_spin_init(&pTier->lock, 0) != 0) { + if (taosThreadSpinInit(&pTier->lock, 0) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); return -1; } @@ -34,7 +34,7 @@ void tfsDestroyTier(STfsTier *pTier) { } pTier->ndisk = 0; - pthread_spin_destroy(&pTier->lock); + taosThreadSpinDestroy(&pTier->lock); } STfsDisk *tfsMountDiskToTier(STfsTier *pTier, SDiskCfg *pCfg) { diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index c861ed350e..d4e75dcd84 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -230,7 +230,7 @@ typedef void (*AsyncCB)(uv_async_t* handle); typedef struct { void* pThrd; queue qmsg; - pthread_mutex_t mtx; // protect qmsg; + TdThreadMutex mtx; // protect qmsg; } SAsyncItem; typedef struct { diff --git a/source/libs/transport/inc/transportInt.h b/source/libs/transport/inc/transportInt.h index 1395408960..ad50948a02 100644 --- a/source/libs/transport/inc/transportInt.h +++ b/source/libs/transport/inc/transportInt.h @@ -70,7 +70,7 @@ typedef struct { void* tmrCtrl; // handle to timer SHashObj* hash; // handle returned by hash utility void* tcphandle; // returned handle from TCP initialization - pthread_mutex_t mutex; + TdThreadMutex mutex; } SRpcInfo; #endif // USE_LIBUV diff --git a/source/libs/transport/src/rpcCache.c b/source/libs/transport/src/rpcCache.c index 1db2808126..d629f6dba4 100644 --- a/source/libs/transport/src/rpcCache.c +++ b/source/libs/transport/src/rpcCache.c @@ -39,7 +39,7 @@ typedef struct { int total; int * count; int64_t keepTimer; - pthread_mutex_t mutex; + TdThreadMutex mutex; void (*cleanFp)(void *); void * tmrCtrl; void * pTimer; @@ -85,7 +85,7 @@ void *rpcOpenConnCache(int maxSessions, void (*cleanFp)(void *), void *tmrCtrl, pCache->lockedBy = calloc(sizeof(int64_t), maxSessions); taosTmrReset(rpcCleanConnCache, (int32_t)(pCache->keepTimer * 2), pCache, pCache->tmrCtrl, &pCache->pTimer); - pthread_mutex_init(&pCache->mutex, NULL); + taosThreadMutexInit(&pCache->mutex, NULL); return pCache; } @@ -96,7 +96,7 @@ void rpcCloseConnCache(void *handle) { pCache = (SConnCache *)handle; if (pCache == NULL || pCache->maxSessions == 0) return; - pthread_mutex_lock(&pCache->mutex); + taosThreadMutexLock(&pCache->mutex); taosTmrStopA(&(pCache->pTimer)); @@ -106,9 +106,9 @@ void rpcCloseConnCache(void *handle) { tfree(pCache->count); tfree(pCache->lockedBy); - pthread_mutex_unlock(&pCache->mutex); + taosThreadMutexUnlock(&pCache->mutex); - pthread_mutex_destroy(&pCache->mutex); + taosThreadMutexDestroy(&pCache->mutex); memset(pCache, 0, sizeof(SConnCache)); free(pCache); @@ -220,7 +220,7 @@ static void rpcCleanConnCache(void *handle, void *tmrId) { if (pCache == NULL || pCache->maxSessions == 0) return; if (pCache->pTimer != tmrId) return; - pthread_mutex_lock(&pCache->mutex); + taosThreadMutexLock(&pCache->mutex); uint64_t time = taosGetTimestampMs(); for (hash = 0; hash < pCache->maxSessions; ++hash) { @@ -232,7 +232,7 @@ static void rpcCleanConnCache(void *handle, void *tmrId) { // tTrace("timer, total connections in cache:%d", pCache->total); taosTmrReset(rpcCleanConnCache, (int32_t)(pCache->keepTimer * 2), pCache, pCache->tmrCtrl, &pCache->pTimer); - pthread_mutex_unlock(&pCache->mutex); + taosThreadMutexUnlock(&pCache->mutex); } static void rpcRemoveExpiredNodes(SConnCache *pCache, SConnHash *pNode, int hash, uint64_t time) { diff --git a/source/libs/transport/src/rpcMain.c b/source/libs/transport/src/rpcMain.c index 86dd17bb0c..98b24ac3c7 100644 --- a/source/libs/transport/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -14,6 +14,7 @@ */ #include "lz4.h" +#include "transportInt.h" #include "os.h" #include "rpcCache.h" #include "rpcHead.h" @@ -27,13 +28,12 @@ #include "tmd5.h" #include "tmempool.h" #include "tmsg.h" -#include "transportInt.h" #include "tref.h" #include "trpc.h" #include "ttimer.h" #include "tutil.h" -static pthread_once_t tsRpcInitOnce = PTHREAD_ONCE_INIT; +static TdThreadOnce tsRpcInitOnce = PTHREAD_ONCE_INIT; int tsRpcMaxUdpSize = 15000; // bytes int tsProgressTimer = 100; @@ -72,7 +72,7 @@ typedef struct { void * tcphandle; // returned handle from TCP initialization void * udphandle; // returned handle from UDP initialization void * pCache; // connection cache - pthread_mutex_t mutex; + TdThreadMutex mutex; struct SRpcConn *connList; // connection list } SRpcInfo; @@ -143,7 +143,7 @@ typedef struct SRpcConn { static int tsRpcRefId = -1; static int32_t tsRpcNum = 0; -// static pthread_once_t tsRpcInit = PTHREAD_ONCE_INIT; +// static TdThreadOnce tsRpcInit = PTHREAD_ONCE_INIT; // server:0 client:1 tcp:2 udp:0 #define RPC_CONN_UDPS 0 @@ -223,7 +223,7 @@ static void rpcInitImp(void) { } int32_t rpcInit() { - pthread_once(&tsRpcInitOnce, rpcInitImp); + taosThreadOnce(&tsRpcInitOnce, rpcInitImp); return 0; } @@ -238,7 +238,7 @@ void rpcCleanup(void) { void *rpcOpen(const SRpcInit *pInit) { SRpcInfo *pRpc; - // pthread_once(&tsRpcInit, rpcInit); + // taosThreadOnce(&tsRpcInit, rpcInit); pRpc = (SRpcInfo *)calloc(1, sizeof(SRpcInfo)); if (pRpc == NULL) return NULL; @@ -307,7 +307,7 @@ void *rpcOpen(const SRpcInit *pInit) { } } - pthread_mutex_init(&pRpc->mutex, NULL); + taosThreadMutexInit(&pRpc->mutex, NULL); pRpc->tcphandle = (*taosInitConn[pRpc->connType | RPC_CONN_TCP])(0, pRpc->localPort, pRpc->label, pRpc->numOfThreads, rpcProcessMsgFromPeer, pRpc); @@ -1672,7 +1672,7 @@ static void rpcDecRef(SRpcInfo *pRpc) { taosIdPoolCleanUp(pRpc->idPool); tfree(pRpc->connList); - pthread_mutex_destroy(&pRpc->mutex); + taosThreadMutexDestroy(&pRpc->mutex); tDebug("%s rpc resources are released", pRpc->label); tfree(pRpc); diff --git a/source/libs/transport/src/rpcTcp.c b/source/libs/transport/src/rpcTcp.c index aac38b21e8..4b1700a10f 100644 --- a/source/libs/transport/src/rpcTcp.c +++ b/source/libs/transport/src/rpcTcp.c @@ -35,9 +35,9 @@ typedef struct SFdObj { } SFdObj; typedef struct SThreadObj { - pthread_t thread; + TdThread thread; SFdObj * pHead; - pthread_mutex_t mutex; + TdThreadMutex mutex; uint32_t ip; bool stop; TdEpollPtr pEpoll; @@ -65,7 +65,7 @@ typedef struct { int numOfThreads; void * shandle; SThreadObj **pThreadObj; - pthread_t thread; + TdThread thread; } SServerObj; static void * taosProcessTcpData(void *param); @@ -101,9 +101,9 @@ void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThread } int code = 0; - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); // initialize parameters in case it may encounter error later for (int i = 0; i < numOfThreads; ++i) { @@ -129,7 +129,7 @@ void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThread // initialize mutex, thread, fd which may fail for (int i = 0; i < numOfThreads; ++i) { pThreadObj = pServerObj->pThreadObj[i]; - code = pthread_mutex_init(&(pThreadObj->mutex), NULL); + code = taosThreadMutexInit(&(pThreadObj->mutex), NULL); if (code < 0) { tError("%s failed to init TCP process data mutex(%s)", label, strerror(errno)); break; @@ -142,7 +142,7 @@ void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThread break; } - code = pthread_create(&(pThreadObj->thread), &thattr, taosProcessTcpData, (void *)(pThreadObj)); + code = taosThreadCreate(&(pThreadObj->thread), &thattr, taosProcessTcpData, (void *)(pThreadObj)); if (code != 0) { tError("%s failed to create TCP process data thread(%s)", label, strerror(errno)); break; @@ -155,7 +155,7 @@ void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThread if (pServerObj->pSocketServer == NULL) code = -1; if (code == 0) { - code = pthread_create(&pServerObj->thread, &thattr, taosAcceptTcpConnection, (void *)pServerObj); + code = taosThreadCreate(&pServerObj->thread, &thattr, taosAcceptTcpConnection, (void *)pServerObj); if (code != 0) { tError("%s failed to create TCP accept thread(%s)", label, strerror(code)); } @@ -169,7 +169,7 @@ void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThread tDebug("%s TCP server is initialized, ip:0x%x port:%hu numOfThreads:%d", label, ip, port, numOfThreads); } - pthread_attr_destroy(&thattr); + taosThreadAttrDestroy(&thattr); return (void *)pServerObj; } @@ -178,16 +178,16 @@ static void taosStopTcpThread(SThreadObj *pThreadObj) { return; } // save thread into local variable and signal thread to stop - pthread_t thread = pThreadObj->thread; + TdThread thread = pThreadObj->thread; if (!taosCheckPthreadValid(thread)) { return; } pThreadObj->stop = true; - if (taosComparePthread(thread, pthread_self())) { - pthread_detach(pthread_self()); + if (taosComparePthread(thread, taosThreadSelf())) { + pthread_detach(taosThreadSelf()); return; } - pthread_join(thread, NULL); + taosThreadJoin(thread, NULL); } void taosStopTcpServer(void *handle) { @@ -200,10 +200,10 @@ void taosStopTcpServer(void *handle) { taosShutDownSocketServerRD(pServerObj->pSocketServer); } if (taosCheckPthreadValid(pServerObj->thread)) { - if (taosComparePthread(pServerObj->thread, pthread_self())) { - pthread_detach(pthread_self()); + if (taosComparePthread(pServerObj->thread, taosThreadSelf())) { + pthread_detach(taosThreadSelf()); } else { - pthread_join(pServerObj->thread, NULL); + taosThreadJoin(pServerObj->thread, NULL); } } @@ -307,9 +307,9 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int numOfThread } int code = 0; - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); for (int i = 0; i < numOfThreads; ++i) { SThreadObj *pThreadObj = (SThreadObj *)calloc(1, sizeof(SThreadObj)); @@ -318,7 +318,7 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int numOfThread terrno = TAOS_SYSTEM_ERROR(errno); for (int j = 0; j < i; ++j) free(pClientObj->pThreadObj[j]); free(pClientObj); - pthread_attr_destroy(&thattr); + taosThreadAttrDestroy(&thattr); return NULL; } pClientObj->pThreadObj[i] = pThreadObj; @@ -333,7 +333,7 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int numOfThread // initialize mutex, thread, fd which may fail for (int i = 0; i < numOfThreads; ++i) { SThreadObj *pThreadObj = pClientObj->pThreadObj[i]; - code = pthread_mutex_init(&(pThreadObj->mutex), NULL); + code = taosThreadMutexInit(&(pThreadObj->mutex), NULL); if (code < 0) { tError("%s failed to init TCP process data mutex(%s)", label, strerror(errno)); break; @@ -346,7 +346,7 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int numOfThread break; } - code = pthread_create(&(pThreadObj->thread), &thattr, taosProcessTcpData, (void *)(pThreadObj)); + code = taosThreadCreate(&(pThreadObj->thread), &thattr, taosProcessTcpData, (void *)(pThreadObj)); if (code != 0) { tError("%s failed to create TCP process data thread(%s)", label, strerror(errno)); break; @@ -578,7 +578,7 @@ static void *taosProcessTcpData(void *param) { taosReportBrokenLink(pFdObj); } - pthread_mutex_destroy(&(pThreadObj->mutex)); + taosThreadMutexDestroy(&(pThreadObj->mutex)); tDebug("%s TCP thread exits ...", pThreadObj->label); tfree(pThreadObj); @@ -607,12 +607,12 @@ static SFdObj *taosMallocFdObj(SThreadObj *pThreadObj, TdSocketPtr pSocket) { } // notify the data process, add into the FdObj list - pthread_mutex_lock(&(pThreadObj->mutex)); + taosThreadMutexLock(&(pThreadObj->mutex)); pFdObj->next = pThreadObj->pHead; if (pThreadObj->pHead) (pThreadObj->pHead)->prev = pFdObj; pThreadObj->pHead = pFdObj; pThreadObj->numOfFds++; - pthread_mutex_unlock(&(pThreadObj->mutex)); + taosThreadMutexUnlock(&(pThreadObj->mutex)); return pFdObj; } @@ -622,10 +622,10 @@ static void taosFreeFdObj(SFdObj *pFdObj) { if (pFdObj->signature != pFdObj) return; SThreadObj *pThreadObj = pFdObj->pThreadObj; - pthread_mutex_lock(&pThreadObj->mutex); + taosThreadMutexLock(&pThreadObj->mutex); if (pFdObj->signature == NULL) { - pthread_mutex_unlock(&pThreadObj->mutex); + taosThreadMutexUnlock(&pThreadObj->mutex); return; } @@ -648,7 +648,7 @@ static void taosFreeFdObj(SFdObj *pFdObj) { (pFdObj->next)->prev = pFdObj->prev; } - pthread_mutex_unlock(&pThreadObj->mutex); + taosThreadMutexUnlock(&pThreadObj->mutex); tDebug("%s %p TCP connection is closed, FD:%p numOfFds:%d", pThreadObj->label, pFdObj->thandle, pFdObj, pThreadObj->numOfFds); diff --git a/source/libs/transport/src/rpcUdp.c b/source/libs/transport/src/rpcUdp.c index 81c8d0af76..af26761603 100644 --- a/source/libs/transport/src/rpcUdp.c +++ b/source/libs/transport/src/rpcUdp.c @@ -35,7 +35,7 @@ typedef struct { uint16_t port; // peer port uint16_t localPort; // local port char label[TSDB_LABEL_LEN]; // copy from udpConnSet; - pthread_t thread; + TdThread thread; void *hash; void *shandle; // handle passed by upper layer during server initialization void *pSet; @@ -77,9 +77,9 @@ void *taosInitUdpConnection(uint32_t ip, uint16_t port, char *label, int threads pSet->threads = threads; tstrncpy(pSet->label, label, sizeof(pSet->label)); - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); int i; uint16_t ownPort; @@ -111,14 +111,14 @@ void *taosInitUdpConnection(uint32_t ip, uint16_t port, char *label, int threads pConn->index = i; pConn->pSet = pSet; - int code = pthread_create(&pConn->thread, &thAttr, taosRecvUdpData, pConn); + int code = taosThreadCreate(&pConn->thread, &thAttr, taosRecvUdpData, pConn); if (code != 0) { tError("%s failed to create thread to process UDP data(%s)", label, strerror(errno)); break; } } - pthread_attr_destroy(&thAttr); + taosThreadAttrDestroy(&thAttr); if (i != threads) { terrno = TAOS_SYSTEM_ERROR(errno); @@ -146,7 +146,7 @@ void taosStopUdpConnection(void *handle) { for (int i = 0; i < pSet->threads; ++i) { pConn = pSet->udpConn + i; if (taosCheckPthreadValid(pConn->thread)) { - pthread_join(pConn->thread, NULL); + taosThreadJoin(pConn->thread, NULL); } tfree(pConn->buffer); // tTrace("%s UDP thread is closed, index:%d", pConn->label, i); diff --git a/source/libs/transport/src/thttp.c b/source/libs/transport/src/thttp.c index 6d1c691b9b..4e2cb83527 100644 --- a/source/libs/transport/src/thttp.c +++ b/source/libs/transport/src/thttp.c @@ -14,10 +14,13 @@ */ #define _DEFAULT_SOURCE +#ifdef USE_UV +#include +#endif +#include "zlib.h" #include "thttp.h" #include "taoserror.h" #include "tlog.h" -#include "zlib.h" static int32_t taosBuildHttpHeader(const char* server, int32_t contLen, char* pHead, int32_t headLen, EHttpCompFlag flag) { @@ -111,7 +114,6 @@ _OVER: } #ifdef USE_UV -#include static void clientConnCb(uv_connect_t* req, int32_t status) { if (status < 0) { terrno = TAOS_SYSTEM_ERROR(status); diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 46658e9352..8415b4d83d 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -56,7 +56,7 @@ typedef struct SCliMsg { } SCliMsg; typedef struct SCliThrdObj { - pthread_t thread; + TdThread thread; uv_loop_t* loop; SAsyncPool* asyncPool; uv_timer_t timer; @@ -64,7 +64,7 @@ typedef struct SCliThrdObj { // msg queue queue msg; - pthread_mutex_t msgMtx; + TdThreadMutex msgMtx; uint64_t nextTimeout; // next timeout void* pTransInst; // @@ -668,9 +668,9 @@ static void cliAsyncCb(uv_async_t* handle) { // batch process to avoid to lock/unlock frequently queue wq; - pthread_mutex_lock(&item->mtx); + taosThreadMutexLock(&item->mtx); QUEUE_MOVE(&item->qmsg, &wq); - pthread_mutex_unlock(&item->mtx); + taosThreadMutexUnlock(&item->mtx); int count = 0; while (!QUEUE_IS_EMPTY(&wq)) { @@ -710,7 +710,7 @@ void* transInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads, pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pTransInst->idleTime); pThrd->pTransInst = shandle; - int err = pthread_create(&pThrd->thread, NULL, cliWorkThread, (void*)(pThrd)); + int err = taosThreadCreate(&pThrd->thread, NULL, cliWorkThread, (void*)(pThrd)); if (err == 0) { tDebug("success to create tranport-cli thread %d", i); } @@ -739,7 +739,7 @@ static SCliThrdObj* createThrdObj() { SCliThrdObj* pThrd = (SCliThrdObj*)calloc(1, sizeof(SCliThrdObj)); QUEUE_INIT(&pThrd->msg); - pthread_mutex_init(&pThrd->msgMtx, NULL); + taosThreadMutexInit(&pThrd->msgMtx, NULL); pThrd->loop = (uv_loop_t*)malloc(sizeof(uv_loop_t)); uv_loop_init(pThrd->loop); @@ -759,8 +759,8 @@ static void destroyThrdObj(SCliThrdObj* pThrd) { return; } uv_stop(pThrd->loop); - pthread_join(pThrd->thread, NULL); - pthread_mutex_destroy(&pThrd->msgMtx); + taosThreadJoin(pThrd->thread, NULL); + taosThreadMutexDestroy(&pThrd->msgMtx); transDestroyAsyncPool(pThrd->asyncPool); uv_timer_stop(&pThrd->timer); diff --git a/source/libs/transport/src/transComm.c b/source/libs/transport/src/transComm.c index 2c90efc3aa..6c16113d46 100644 --- a/source/libs/transport/src/transComm.c +++ b/source/libs/transport/src/transComm.c @@ -186,7 +186,7 @@ SAsyncPool* transCreateAsyncPool(uv_loop_t* loop, int sz, void* arg, AsyncCB cb) SAsyncItem* item = calloc(1, sizeof(SAsyncItem)); item->pThrd = arg; QUEUE_INIT(&item->qmsg); - pthread_mutex_init(&item->mtx, NULL); + taosThreadMutexInit(&item->mtx, NULL); async->data = item; } @@ -197,7 +197,7 @@ void transDestroyAsyncPool(SAsyncPool* pool) { uv_async_t* async = &(pool->asyncs[i]); SAsyncItem* item = async->data; - pthread_mutex_destroy(&item->mtx); + taosThreadMutexDestroy(&item->mtx); free(item); } free(pool->asyncs); @@ -214,9 +214,9 @@ int transSendAsync(SAsyncPool* pool, queue* q) { SAsyncItem* item = async->data; int64_t st = taosGetTimestampUs(); - pthread_mutex_lock(&item->mtx); + taosThreadMutexLock(&item->mtx); QUEUE_PUSH(&item->qmsg, q); - pthread_mutex_unlock(&item->mtx); + taosThreadMutexUnlock(&item->mtx); int64_t el = taosGetTimestampUs() - st; if (el > 50) { // tInfo("lock and unlock cost: %d", (int)el); diff --git a/source/libs/transport/src/transSrv.c b/source/libs/transport/src/transSrv.c index 063088aeb4..de96883b77 100644 --- a/source/libs/transport/src/transSrv.c +++ b/source/libs/transport/src/transSrv.c @@ -62,13 +62,13 @@ typedef struct SSrvMsg { } SSrvMsg; typedef struct SWorkThrdObj { - pthread_t thread; + TdThread thread; uv_pipe_t* pipe; uv_os_fd_t fd; uv_loop_t* loop; SAsyncPool* asyncPool; queue msg; - pthread_mutex_t msgMtx; + TdThreadMutex msgMtx; queue conn; void* pTransInst; @@ -76,7 +76,7 @@ typedef struct SWorkThrdObj { } SWorkThrdObj; typedef struct SServerObj { - pthread_t thread; + TdThread thread; uv_tcp_t server; uv_loop_t* loop; @@ -476,9 +476,9 @@ void uvWorkerAsyncCb(uv_async_t* handle) { queue wq; // batch process to avoid to lock/unlock frequently - pthread_mutex_lock(&item->mtx); + taosThreadMutexLock(&item->mtx); QUEUE_MOVE(&item->qmsg, &wq); - pthread_mutex_unlock(&item->mtx); + taosThreadMutexUnlock(&item->mtx); while (!QUEUE_IS_EMPTY(&wq)) { queue* head = QUEUE_HEAD(&wq); @@ -622,7 +622,7 @@ static bool addHandleToWorkloop(void* arg) { pThrd->pipe->data = pThrd; QUEUE_INIT(&pThrd->msg); - pthread_mutex_init(&pThrd->msgMtx, NULL); + taosThreadMutexInit(&pThrd->msgMtx, NULL); // conn set QUEUE_INIT(&pThrd->conn); @@ -771,7 +771,7 @@ void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, if (false == addHandleToWorkloop(thrd)) { goto End; } - int err = pthread_create(&(thrd->thread), NULL, workerThread, (void*)(thrd)); + int err = taosThreadCreate(&(thrd->thread), NULL, workerThread, (void*)(thrd)); if (err == 0) { tDebug("sucess to create worker-thread %d", i); // printf("thread %d create\n", i); @@ -783,7 +783,7 @@ void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, if (false == addHandleToAcceptloop(srv)) { goto End; } - int err = pthread_create(&srv->thread, NULL, acceptThread, (void*)srv); + int err = taosThreadCreate(&srv->thread, NULL, acceptThread, (void*)srv); if (err == 0) { tDebug("success to create accept-thread"); } else { @@ -850,7 +850,7 @@ void destroyWorkThrd(SWorkThrdObj* pThrd) { if (pThrd == NULL) { return; } - pthread_join(pThrd->thread, NULL); + taosThreadJoin(pThrd->thread, NULL); free(pThrd->loop); transDestroyAsyncPool(pThrd->asyncPool); free(pThrd); @@ -872,7 +872,7 @@ void transCloseServer(void* arg) { tDebug("send quit msg to accept thread"); uv_async_send(srv->pAcceptAsync); - pthread_join(srv->thread, NULL); + taosThreadJoin(srv->thread, NULL); free(srv->pThreadObj); free(srv->pAcceptAsync); diff --git a/source/libs/transport/test/rclient.c b/source/libs/transport/test/rclient.c index c6ff2480ef..4af316acae 100644 --- a/source/libs/transport/test/rclient.c +++ b/source/libs/transport/test/rclient.c @@ -30,7 +30,7 @@ typedef struct { int msgSize; tsem_t rspSem; tsem_t * pOverSem; - pthread_t thread; + TdThread thread; void * pRpc; } SInfo; static void processResponse(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { @@ -104,7 +104,7 @@ int main(int argc, char *argv[]) { char secret[20] = "mypassword"; struct timeval systemTime; int64_t startTime, endTime; - pthread_attr_t thattr; + TdThreadAttr thattr; // server info epSet.inUse = 0; @@ -186,8 +186,8 @@ int main(int argc, char *argv[]) { SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo) * appThreads); - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); for (int i = 0; i < appThreads; ++i) { pInfo->index = i; @@ -196,7 +196,7 @@ int main(int argc, char *argv[]) { pInfo->msgSize = msgSize; tsem_init(&pInfo->rspSem, 0, 0); pInfo->pRpc = pRpc; - pthread_create(&pInfo->thread, &thattr, sendRequest, pInfo); + taosThreadCreate(&pInfo->thread, &thattr, sendRequest, pInfo); pInfo++; } diff --git a/source/libs/transport/test/rsclient.c b/source/libs/transport/test/rsclient.c index 1fe13a35b1..f3be4e3452 100644 --- a/source/libs/transport/test/rsclient.c +++ b/source/libs/transport/test/rsclient.c @@ -29,7 +29,7 @@ typedef struct { int msgSize; tsem_t rspSem; tsem_t *pOverSem; - pthread_t thread; + TdThread thread; void *pRpc; } SInfo; @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) { char secret[TSDB_KEY_LEN] = "mypassword"; struct timeval systemTime; int64_t startTime, endTime; - pthread_attr_t thattr; + TdThreadAttr thattr; // server info epSet.numOfEps = 1; @@ -163,8 +163,8 @@ int main(int argc, char *argv[]) { SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo)*appThreads); - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); for (int i=0; iindex = i; @@ -173,7 +173,7 @@ int main(int argc, char *argv[]) { pInfo->msgSize = msgSize; tsem_init(&pInfo->rspSem, 0, 0); pInfo->pRpc = pRpc; - pthread_create(&pInfo->thread, &thattr, sendRequest, pInfo); + taosThreadCreate(&pInfo->thread, &thattr, sendRequest, pInfo); pInfo++; } diff --git a/source/libs/transport/test/syncClient.c b/source/libs/transport/test/syncClient.c index b3c2f6ebdc..14f32df0b8 100644 --- a/source/libs/transport/test/syncClient.c +++ b/source/libs/transport/test/syncClient.c @@ -30,7 +30,7 @@ typedef struct { int msgSize; tsem_t rspSem; tsem_t * pOverSem; - pthread_t thread; + TdThread thread; void * pRpc; } SInfo; static void processResponse(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { @@ -105,7 +105,7 @@ int main(int argc, char *argv[]) { char secret[20] = "mypassword"; struct timeval systemTime; int64_t startTime, endTime; - pthread_attr_t thattr; + TdThreadAttr thattr; // server info epSet.inUse = 0; @@ -186,8 +186,8 @@ int main(int argc, char *argv[]) { SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo) * appThreads); - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); for (int i = 0; i < appThreads; ++i) { pInfo->index = i; @@ -196,7 +196,7 @@ int main(int argc, char *argv[]) { pInfo->msgSize = msgSize; tsem_init(&pInfo->rspSem, 0, 0); pInfo->pRpc = pRpc; - pthread_create(&pInfo->thread, &thattr, sendRequest, pInfo); + taosThreadCreate(&pInfo->thread, &thattr, sendRequest, pInfo); pInfo++; } diff --git a/source/libs/transport/test/uv.c b/source/libs/transport/test/uv.c index 4c7d30900b..5a19b7998c 100644 --- a/source/libs/transport/test/uv.c +++ b/source/libs/transport/test/uv.c @@ -1,17 +1,16 @@ #include -#include +#include #include #include #include #include "task.h" -#include #define NUM_OF_THREAD 1 #define TIMEOUT 10000 typedef struct SThreadObj { - pthread_t thread; + TdThread thread; uv_pipe_t *pipe; uv_loop_t *loop; uv_async_t *workerAsync; // @@ -183,7 +182,7 @@ int main() { server->pThreadObj[i]->fd = fds[0]; server->pThreadObj[i]->pipe = &(server->pipe[i][1]); // init read - int err = pthread_create(&(server->pThreadObj[i]->thread), NULL, + int err = taosThreadCreate(&(server->pThreadObj[i]->thread), NULL, worker_thread, (void *)(server->pThreadObj[i])); if (err == 0) { printf("thread %d create\n", i); diff --git a/source/libs/wal/src/walMgmt.c b/source/libs/wal/src/walMgmt.c index b1caeab3b1..2da8f4f8af 100644 --- a/source/libs/wal/src/walMgmt.c +++ b/source/libs/wal/src/walMgmt.c @@ -25,7 +25,7 @@ typedef struct { int8_t inited; uint32_t seq; int32_t refSetId; - pthread_t thread; + TdThread thread; } SWalMgmt; static SWalMgmt tsWal = {0, .seq = 1}; @@ -101,7 +101,7 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) { pWal->writeHead.head.headVer = WAL_HEAD_VER; pWal->writeHead.magic = WAL_MAGIC; - if (pthread_mutex_init(&pWal->mutex, NULL) < 0) { + if (taosThreadMutexInit(&pWal->mutex, NULL) < 0) { taosArrayDestroy(pWal->fileInfoSet); free(pWal); return NULL; @@ -109,7 +109,7 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) { pWal->refId = taosAddRef(tsWal.refSetId, pWal); if (pWal->refId < 0) { - pthread_mutex_destroy(&pWal->mutex); + taosThreadMutexDestroy(&pWal->mutex); taosArrayDestroy(pWal->fileInfoSet); free(pWal); return NULL; @@ -119,7 +119,7 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) { if (walCheckAndRepairMeta(pWal) < 0) { taosRemoveRef(tsWal.refSetId, pWal->refId); - pthread_mutex_destroy(&pWal->mutex); + taosThreadMutexDestroy(&pWal->mutex); taosArrayDestroy(pWal->fileInfoSet); free(pWal); return NULL; @@ -156,7 +156,7 @@ int32_t walAlter(SWal *pWal, SWalCfg *pCfg) { } void walClose(SWal *pWal) { - pthread_mutex_lock(&pWal->mutex); + taosThreadMutexLock(&pWal->mutex); taosCloseFile(&pWal->pWriteLogTFile); pWal->pWriteLogTFile = NULL; taosCloseFile(&pWal->pWriteIdxTFile); @@ -164,7 +164,7 @@ void walClose(SWal *pWal) { walSaveMeta(pWal); taosArrayDestroy(pWal->fileInfoSet); pWal->fileInfoSet = NULL; - pthread_mutex_unlock(&pWal->mutex); + taosThreadMutexUnlock(&pWal->mutex); taosRemoveRef(tsWal.refSetId, pWal->refId); } @@ -173,7 +173,7 @@ static void walFreeObj(void *wal) { SWal *pWal = wal; wDebug("vgId:%d, wal:%p is freed", pWal->cfg.vgId, pWal); - pthread_mutex_destroy(&pWal->mutex); + taosThreadMutexDestroy(&pWal->mutex); tfree(pWal); } @@ -223,17 +223,17 @@ static void *walThreadFunc(void *param) { } static int32_t walCreateThread() { - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&tsWal.thread, &thAttr, walThreadFunc, NULL) != 0) { + if (taosThreadCreate(&tsWal.thread, &thAttr, walThreadFunc, NULL) != 0) { wError("failed to create wal thread since %s", strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; } - pthread_attr_destroy(&thAttr); + taosThreadAttrDestroy(&thAttr); wDebug("wal thread is launched, thread:0x%08" PRIx64, taosGetPthreadId(tsWal.thread)); return 0; @@ -243,7 +243,7 @@ static void walStopThread() { atomic_store_8(&tsWal.stop, 1); if (taosCheckPthreadValid(tsWal.thread)) { - pthread_join(tsWal.thread, NULL); + taosThreadJoin(tsWal.thread, NULL); } wDebug("wal thread is stopped"); diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index 0f6ac0b214..8392e66e58 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -38,7 +38,7 @@ int32_t walRollback(SWal *pWal, int64_t ver) { terrno = TSDB_CODE_WAL_INVALID_VER; return -1; } - pthread_mutex_lock(&pWal->mutex); + taosThreadMutexLock(&pWal->mutex); // find correct file if (ver < walGetLastFileFirstVer(pWal)) { @@ -65,20 +65,20 @@ int32_t walRollback(SWal *pWal, int64_t ver) { // TODO:change to deserialize function if (pIdxTFile == NULL) { - pthread_mutex_unlock(&pWal->mutex); + taosThreadMutexUnlock(&pWal->mutex); return -1; } int64_t idxOff = walGetVerIdxOffset(pWal, ver); code = taosLSeekFile(pIdxTFile, idxOff, SEEK_SET); if (code < 0) { - pthread_mutex_unlock(&pWal->mutex); + taosThreadMutexUnlock(&pWal->mutex); return -1; } // read idx file and get log file pos // TODO:change to deserialize function SWalIdxEntry entry; if (taosReadFile(pIdxTFile, &entry, sizeof(SWalIdxEntry)) != sizeof(SWalIdxEntry)) { - pthread_mutex_unlock(&pWal->mutex); + taosThreadMutexUnlock(&pWal->mutex); return -1; } ASSERT(entry.ver == ver); @@ -87,13 +87,13 @@ int32_t walRollback(SWal *pWal, int64_t ver) { TdFilePtr pLogTFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ); if (pLogTFile == NULL) { // TODO - pthread_mutex_unlock(&pWal->mutex); + taosThreadMutexUnlock(&pWal->mutex); return -1; } code = taosLSeekFile(pLogTFile, entry.offset, SEEK_SET); if (code < 0) { // TODO - pthread_mutex_unlock(&pWal->mutex); + taosThreadMutexUnlock(&pWal->mutex); return -1; } // validate offset @@ -127,7 +127,7 @@ int32_t walRollback(SWal *pWal, int64_t ver) { ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->fileSize = entry.offset; // unlock - pthread_mutex_unlock(&pWal->mutex); + taosThreadMutexUnlock(&pWal->mutex); return 0; } @@ -283,7 +283,7 @@ int64_t walWrite(SWal *pWal, int64_t index, tmsg_t msgType, const void *body, in ASSERT(pWal->writeCur >= 0); - pthread_mutex_lock(&pWal->mutex); + taosThreadMutexLock(&pWal->mutex); if (pWal->pWriteIdxTFile == NULL || pWal->pWriteLogTFile == NULL) { walSetWrite(pWal); @@ -327,7 +327,7 @@ int64_t walWrite(SWal *pWal, int64_t index, tmsg_t msgType, const void *body, in walGetCurFileInfo(pWal)->lastVer = index; walGetCurFileInfo(pWal)->fileSize += sizeof(SWalHead) + bodyLen; - pthread_mutex_unlock(&pWal->mutex); + taosThreadMutexUnlock(&pWal->mutex); return 0; } diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 5e859de5d6..4dece8abef 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -57,7 +57,7 @@ typedef int32_t SocketFd; typedef int32_t FileFd; typedef struct TdFile { - pthread_rwlock_t rwlock; + TdThreadRwlock rwlock; int refId; FileFd fd; FILE *fp; @@ -275,7 +275,7 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { return NULL; } #if FILE_WITH_LOCK - pthread_rwlock_init(&(pFile->rwlock), NULL); + taosThreadRwlockInit(&(pFile->rwlock), NULL); #endif pFile->fd = fd; pFile->fp = fp; @@ -292,7 +292,7 @@ int64_t taosCloseFile(TdFilePtr *ppFile) { return 0; } #if FILE_WITH_LOCK - pthread_rwlock_wrlock(&((*ppFile)->rwlock)); + taosThreadRwlockWrlock(&((*ppFile)->rwlock)); #endif if (ppFile == NULL || *ppFile == NULL || (*ppFile)->fd == -1) { return 0; @@ -309,8 +309,8 @@ int64_t taosCloseFile(TdFilePtr *ppFile) { } (*ppFile)->refId = 0; #if FILE_WITH_LOCK - pthread_rwlock_unlock(&((*ppFile)->rwlock)); - pthread_rwlock_destroy(&((*ppFile)->rwlock)); + taosThreadRwlockUnlock(&((*ppFile)->rwlock)); + taosThreadRwlockDestroy(&((*ppFile)->rwlock)); #endif free(*ppFile); *ppFile = NULL; @@ -323,7 +323,7 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { return 0; } #if FILE_WITH_LOCK - pthread_rwlock_rdlock(&(pFile->rwlock)); + taosThreadRwlockRdlock(&(pFile->rwlock)); #endif assert(pFile->fd >= 0); int64_t leftbytes = count; @@ -337,13 +337,13 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { continue; } else { #if FILE_WITH_LOCK - pthread_rwlock_unlock(&(pFile->rwlock)); + taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return -1; } } else if (readbytes == 0) { #if FILE_WITH_LOCK - pthread_rwlock_unlock(&(pFile->rwlock)); + taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return (int64_t)(count - leftbytes); } @@ -353,7 +353,7 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { } #if FILE_WITH_LOCK - pthread_rwlock_unlock(&(pFile->rwlock)); + taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return count; } @@ -363,12 +363,12 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) return 0; } #if FILE_WITH_LOCK - pthread_rwlock_rdlock(&(pFile->rwlock)); + taosThreadRwlockRdlock(&(pFile->rwlock)); #endif assert(pFile->fd >= 0); int64_t ret = pread(pFile->fd, buf, count, offset); #if FILE_WITH_LOCK - pthread_rwlock_unlock(&(pFile->rwlock)); + taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return ret; } @@ -378,7 +378,7 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { return 0; } #if FILE_WITH_LOCK - pthread_rwlock_wrlock(&(pFile->rwlock)); + taosThreadRwlockWrlock(&(pFile->rwlock)); #endif assert(pFile->fd >= 0); @@ -393,7 +393,7 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { continue; } #if FILE_WITH_LOCK - pthread_rwlock_unlock(&(pFile->rwlock)); + taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return -1; } @@ -402,7 +402,7 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { } #if FILE_WITH_LOCK - pthread_rwlock_unlock(&(pFile->rwlock)); + taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return count; } @@ -412,12 +412,12 @@ int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) { return 0; } #if FILE_WITH_LOCK - pthread_rwlock_rdlock(&(pFile->rwlock)); + taosThreadRwlockRdlock(&(pFile->rwlock)); #endif assert(pFile->fd >= 0); int64_t ret = lseek(pFile->fd, (long)offset, whence); #if FILE_WITH_LOCK - pthread_rwlock_unlock(&(pFile->rwlock)); + taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return ret; } diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index e5b506e8d8..b41ef898d4 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -24,11 +24,11 @@ #include -bool taosCheckPthreadValid(pthread_t thread) { return thread.p != NULL; } +bool taosCheckPthreadValid(TdThread thread) { return thread.p != NULL; } -void taosResetPthread(pthread_t* thread) { thread->p = 0; } +void taosResetPthread(TdThread* thread) { thread->p = 0; } -int64_t taosGetPthreadId(pthread_t thread) { +int64_t taosGetPthreadId(TdThread thread) { #ifdef PTW32_VERSION return pthread_getw32threadid_np(thread); #else @@ -38,7 +38,7 @@ int64_t taosGetPthreadId(pthread_t thread) { int64_t taosGetSelfPthreadId() { return GetCurrentThreadId(); } -bool taosComparePthread(pthread_t first, pthread_t second) { return first.p == second.p; } +bool taosComparePthread(TdThread first, TdThread second) { return first.p == second.p; } int32_t taosGetPId() { return GetCurrentProcessId(); } @@ -84,10 +84,9 @@ int32_t tsem_wait(tsem_t* sem) { #include #include #include -#include -static pthread_t sem_thread; -static pthread_once_t sem_once; +static TdThread sem_thread; +static TdThreadOnce sem_once; static task_t sem_port; static volatile int sem_inited = 0; static semaphore_t sem_exit; @@ -110,7 +109,7 @@ static void *sem_thread_routine(void *arg) { static void once_init(void) { int r = 0; - r = pthread_create(&sem_thread, NULL, sem_thread_routine, NULL); + r = taosThreadCreate(&sem_thread, NULL, sem_thread_routine, NULL); if (r) { fprintf(stderr, "==%s[%d]%s()==failed to create thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); return; @@ -123,8 +122,8 @@ static void once_init(void) { struct tsem_s { #ifdef SEM_USE_PTHREAD - pthread_mutex_t lock; - pthread_cond_t cond; + TdThreadMutex lock; + TdThreadCond cond; volatile int64_t val; #elif defined(SEM_USE_POSIX) size_t id; @@ -151,12 +150,12 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { } #ifdef SEM_USE_PTHREAD - int r = pthread_mutex_init(&p->lock, NULL); + int r = taosThreadMutexInit(&p->lock, NULL); do { if (r) break; - r = pthread_cond_init(&p->cond, NULL); + r = taosThreadCondInit(&p->cond, NULL); if (r) { - pthread_mutex_destroy(&p->lock); + taosThreadMutexDestroy(&p->lock); break; } p->val = value; @@ -186,7 +185,7 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { abort(); } while (p->sem == SEM_FAILED); #elif defined(SEM_USE_SEM) - pthread_once(&sem_once, once_init); + taosThreadOnce(&sem_once, once_init); if (sem_inited != 1) { fprintf(stderr, "==%s[%d]%s():[%p]==internal resource init failed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); errno = ENOMEM; @@ -224,18 +223,18 @@ int tsem_wait(tsem_t *sem) { abort(); } #ifdef SEM_USE_PTHREAD - if (pthread_mutex_lock(&p->lock)) { + if (taosThreadMutexLock(&p->lock)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } p->val -= 1; if (p->val < 0) { - if (pthread_cond_wait(&p->cond, &p->lock)) { + if (taosThreadCondWait(&p->cond, &p->lock)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } } - if (pthread_mutex_unlock(&p->lock)) { + if (taosThreadMutexUnlock(&p->lock)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } @@ -260,18 +259,18 @@ int tsem_post(tsem_t *sem) { abort(); } #ifdef SEM_USE_PTHREAD - if (pthread_mutex_lock(&p->lock)) { + if (taosThreadMutexLock(&p->lock)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } p->val += 1; if (p->val <= 0) { - if (pthread_cond_signal(&p->cond)) { + if (taosThreadCondSignal(&p->cond)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } } - if (pthread_mutex_unlock(&p->lock)) { + if (taosThreadMutexUnlock(&p->lock)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } @@ -299,20 +298,20 @@ int tsem_destroy(tsem_t *sem) { return 0; } #ifdef SEM_USE_PTHREAD - if (pthread_mutex_lock(&p->lock)) { + if (taosThreadMutexLock(&p->lock)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } p->valid = 0; - if (pthread_cond_destroy(&p->cond)) { + if (taosThreadCondDestroy(&p->cond)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } - if (pthread_mutex_unlock(&p->lock)) { + if (taosThreadMutexUnlock(&p->lock)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } - if (pthread_mutex_destroy(&p->lock)) { + if (taosThreadMutexDestroy(&p->lock)) { fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); abort(); } @@ -338,23 +337,23 @@ int tsem_destroy(tsem_t *sem) { return 0; } -bool taosCheckPthreadValid(pthread_t thread) { +bool taosCheckPthreadValid(TdThread thread) { uint64_t id = 0; - int r = pthread_threadid_np(thread, &id); + int r = TdThreadhreadid_np(thread, &id); return r ? false : true; } int64_t taosGetSelfPthreadId() { uint64_t id; - pthread_threadid_np(0, &id); + TdThreadhreadid_np(0, &id); return (int64_t)id; } -int64_t taosGetPthreadId(pthread_t thread) { return (int64_t)thread; } +int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; } -void taosResetPthread(pthread_t *thread) { *thread = NULL; } +void taosResetPthread(TdThread *thread) { *thread = NULL; } -bool taosComparePthread(pthread_t first, pthread_t second) { return pthread_equal(first, second) ? true : false; } +bool taosComparePthread(TdThread first, TdThread second) { return taosThreadEqual(first, second) ? true : false; } int32_t taosGetPId() { return (int32_t)getpid(); } @@ -378,7 +377,7 @@ int32_t taosGetAppName(char *name, int32_t *len) { #include #include -bool taosCheckPthreadValid(pthread_t thread) { return thread != 0; } +bool taosCheckPthreadValid(TdThread thread) { return thread != 0; } int64_t taosGetSelfPthreadId() { static __thread int id = 0; @@ -387,9 +386,9 @@ int64_t taosGetSelfPthreadId() { return id; } -int64_t taosGetPthreadId(pthread_t thread) { return (int64_t)thread; } -void taosResetPthread(pthread_t* thread) { *thread = 0; } -bool taosComparePthread(pthread_t first, pthread_t second) { return first == second; } +int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; } +void taosResetPthread(TdThread* thread) { *thread = 0; } +bool taosComparePthread(TdThread first, TdThread second) { return first == second; } int32_t taosGetPId() { return getpid(); } int32_t taosGetAppName(char* name, int32_t* len) { diff --git a/source/os/src/osSocket.c b/source/os/src/osSocket.c index e3b1015d88..e5f83fa167 100644 --- a/source/os/src/osSocket.c +++ b/source/os/src/osSocket.c @@ -52,7 +52,7 @@ typedef SocketFd EpollFd; typedef struct TdSocketServer { #if SOCKET_WITH_LOCK - pthread_rwlock_t rwlock; + TdThreadRwlock rwlock; #endif int refId; SocketFd fd; @@ -60,7 +60,7 @@ typedef struct TdSocketServer { typedef struct TdSocket { #if SOCKET_WITH_LOCK - pthread_rwlock_t rwlock; + TdThreadRwlock rwlock; #endif int refId; SocketFd fd; @@ -68,7 +68,7 @@ typedef struct TdSocket { typedef struct TdEpoll { #if SOCKET_WITH_LOCK - pthread_rwlock_t rwlock; + TdThreadRwlock rwlock; #endif int refId; EpollFd fd; @@ -775,7 +775,7 @@ void taosBlockSIGPIPE() { sigset_t signal_mask; sigemptyset(&signal_mask); sigaddset(&signal_mask, SIGPIPE); - int32_t rc = pthread_sigmask(SIG_BLOCK, &signal_mask, NULL); + int32_t rc = taosThreadSigmask(SIG_BLOCK, &signal_mask, NULL); if (rc != 0) { // printf("failed to block SIGPIPE"); } @@ -893,7 +893,7 @@ void taosSetMaskSIGPIPE() { sigset_t signal_mask; sigemptyset(&signal_mask); sigaddset(&signal_mask, SIGPIPE); - int32_t rc = pthread_sigmask(SIG_SETMASK, &signal_mask, NULL); + int32_t rc = taosThreadSigmask(SIG_SETMASK, &signal_mask, NULL); if (rc != 0) { // printf("failed to setmask SIGPIPE"); } diff --git a/source/os/src/osThread.c b/source/os/src/osThread.c new file mode 100644 index 0000000000..716080274b --- /dev/null +++ b/source/os/src/osThread.c @@ -0,0 +1,145 @@ +/* + * 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 . + */ + +#define ALLOW_FORBID_FUNC +#include "os.h" + +// int32_t taosThreadSetnameNp(TdThread thread, const char *name) { +// return pthread_setname_np(thread,name); +// } + +int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int pshared) { + return pthread_spin_init(lock, pshared); +} + +int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) { + return pthread_mutex_init(mutex, attr); +} + +int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) { + return pthread_spin_destroy(lock); +} + +int32_t taosThreadMutexDestroy(TdThreadMutex * mutex) { + return pthread_mutex_destroy(mutex); +} + +int32_t taosThreadSpinLock(TdThreadSpinlock *lock) { + return pthread_spin_lock(lock); +} + +int32_t taosThreadMutexLock(TdThreadMutex *mutex) { + return pthread_mutex_lock(mutex); +} + +int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) { + return pthread_spin_unlock(lock); +} + +int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) { + return pthread_mutex_unlock(mutex); +} + +int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) { + return pthread_rwlock_rdlock(rwlock); +} + +int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) { + return pthread_rwlock_wrlock(rwlock); +} + +int32_t taosThreadRwlockUnlock(TdThreadRwlock *rwlock) { + return pthread_rwlock_unlock(rwlock); +} + +void taosThreadTestCancel(void) { + return pthread_testcancel(); +} + +int32_t taosThreadAttrInit(TdThreadAttr *attr) { + return pthread_attr_init(attr); +} + +int32_t taosThreadCreate(TdThread *tid, const TdThreadAttr *attr, void*(*start)(void*), void *arg) { + return pthread_create(tid, attr, start, arg); +} + +int32_t taosThreadOnce(TdThreadOnce *onceControl, void(*initRoutine)(void)) { + return pthread_once(onceControl, initRoutine); +} + +int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachState) { + return pthread_attr_setdetachstate(attr, detachState); +} + +int32_t taosThreadAttrDestroy(TdThreadAttr *attr) { + return pthread_attr_destroy(attr); +} + +int32_t taosThreadJoin(TdThread thread, void **pValue) { + return pthread_join(thread, pValue); +} + +int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) { + return pthread_rwlock_init(rwlock, attr); +} + +int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) { + return pthread_rwlock_destroy(rwlock); +} + +int32_t taosThreadCondSignal(TdThreadCond *cond) { + return pthread_cond_signal(cond); +} + +int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) { + return pthread_cond_init(cond, attr); +} + +int32_t taosThreadCondBroadcast(TdThreadCond *cond) { + return pthread_cond_broadcast(cond); +} + +int32_t taosThreadCondDestroy(TdThreadCond *cond) { + return pthread_cond_destroy(cond); +} + +int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) { + return pthread_cond_wait(cond, mutex); +} + +TdThread taosThreadSelf(void) { + return pthread_self(); +} + +// int32_t taosThreadGetW32ThreadIdNp(TdThread thread) { +// return pthread_getw32threadid_np(thread); +// } + +int32_t taosThreadEqual(TdThread t1, TdThread t2) { + return pthread_equal(t1, t2); +} + +int32_t taosThreadSigmask(int how, sigset_t const *set, sigset_t *oset) { + return pthread_sigmask(how, set, oset); +} + +int32_t taosThreadCancel(TdThread thread) { + return pthread_cancel(thread); +} + +int32_t taosThreadKill(TdThread thread, int sig) { + return pthread_kill(thread, sig); +} \ No newline at end of file diff --git a/source/os/src/osTimer.c b/source/os/src/osTimer.c index c95ca72bd5..e6e3ec7962 100644 --- a/source/os/src/osTimer.c +++ b/source/os/src/osTimer.c @@ -44,7 +44,7 @@ static MMRESULT timerId; static void (*timer_callback)(int); static int timer_ms = 0; -static pthread_t timer_thread; +static TdThread timer_thread; static int timer_kq = -1; static volatile int timer_stop = 0; @@ -83,7 +83,7 @@ static void taosDeleteTimer(void *tharg) { timer_delete(*pTimer); } -static pthread_t timerThread; +static TdThread timerThread; static timer_t timerId; static volatile bool stopTimer = false; static void * taosProcessAlarmSignal(void *tharg) { @@ -112,7 +112,7 @@ static void * taosProcessAlarmSignal(void *tharg) { // printf("Failed to create timer"); } - pthread_cleanup_push(taosDeleteTimer, &timerId); + taosThreadCleanupPush(taosDeleteTimer, &timerId); struct itimerspec ts; ts.it_value.tv_sec = 0; @@ -136,7 +136,7 @@ static void * taosProcessAlarmSignal(void *tharg) { callback(0); } - pthread_cleanup_pop(1); + taosThreadCleanupPop(1); return NULL; } @@ -165,7 +165,7 @@ int taosInitTimer(void (*callback)(int), int ms) { abort(); } - r = pthread_create(&timer_thread, NULL, timer_routine, NULL); + r = taosThreadCreate(&timer_thread, NULL, timer_routine, NULL); if (r) { fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); // since no caller of this func checks the return value for the moment @@ -174,10 +174,10 @@ int taosInitTimer(void (*callback)(int), int ms) { return 0; #else stopTimer = false; - pthread_attr_t tattr; - pthread_attr_init(&tattr); - int code = pthread_create(&timerThread, &tattr, taosProcessAlarmSignal, callback); - pthread_attr_destroy(&tattr); + TdThreadAttr tattr; + taosThreadAttrInit(&tattr); + int code = taosThreadCreate(&timerThread, &tattr, taosProcessAlarmSignal, callback); + taosThreadAttrDestroy(&tattr); if (code != 0) { // printf("failed to create timer thread"); return -1; @@ -195,7 +195,7 @@ void taosUninitTimer() { #elif defined(_TD_DARWIN_64) int r = 0; timer_stop = 1; - r = pthread_join(timer_thread, NULL); + r = taosThreadJoin(timer_thread, NULL); if (r) { fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); // since no caller of this func checks the return value for the moment @@ -207,7 +207,7 @@ void taosUninitTimer() { stopTimer = true; // printf("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread)); - pthread_join(timerThread, NULL); + taosThreadJoin(timerThread, NULL); #endif } diff --git a/source/os/src/osTimezone.c b/source/os/src/osTimezone.c index dd34d77b23..bdafa63d64 100644 --- a/source/os/src/osTimezone.c +++ b/source/os/src/osTimezone.c @@ -13,6 +13,7 @@ * along with this program. If not, see . */ +#define ALLOW_FORBID_FUNC #define _DEFAULT_SOURCE #include "os.h" diff --git a/source/util/CMakeLists.txt b/source/util/CMakeLists.txt index 08fa0a2409..7a47639e75 100644 --- a/source/util/CMakeLists.txt +++ b/source/util/CMakeLists.txt @@ -5,10 +5,6 @@ target_include_directories( util PUBLIC "${CMAKE_SOURCE_DIR}/include/util" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" -IF(${TD_WINDOWS}) - PRIVATE "${CMAKE_SOURCE_DIR}/contrib/pthread-win32" - PRIVATE "${CMAKE_SOURCE_DIR}/contrib/gnuregex" -ENDIF () ) target_link_libraries( util diff --git a/source/util/src/tcache.c b/source/util/src/tcache.c index 51ea2b1f13..9a1b213534 100644 --- a/source/util/src/tcache.c +++ b/source/util/src/tcache.c @@ -22,9 +22,9 @@ #define CACHE_MAX_CAPACITY 1024*1024*16 #define CACHE_DEFAULT_CAPACITY 1024*4 -static pthread_t cacheRefreshWorker = {0}; -static pthread_once_t cacheThreadInit = PTHREAD_ONCE_INIT; -static pthread_mutex_t guard = PTHREAD_MUTEX_INITIALIZER; +static TdThread cacheRefreshWorker = {0}; +static TdThreadOnce cacheThreadInit = PTHREAD_ONCE_INIT; +static TdThreadMutex guard = PTHREAD_MUTEX_INITIALIZER; static SArray *pCacheArrayList = NULL; static bool stopRefreshWorker = false; static bool refreshWorkerNormalStopped = false; @@ -90,13 +90,13 @@ struct SCacheObj { STrashElem *pTrash; uint8_t deleting; // set the deleting flag to stop refreshing ASAP. - pthread_t refreshWorker; + TdThread refreshWorker; bool extendLifespan; // auto extend life span when one item is accessed. int64_t checkTick; // tick used to record the check times of the refresh threads #if defined(LINUX) - pthread_rwlock_t lock; + TdThreadRwlock lock; #else - pthread_mutex_t lock; + TdThreadMutex lock; #endif }; @@ -109,33 +109,33 @@ typedef struct SCacheObjTravSup { static FORCE_INLINE void __trashcan_wr_lock(SCacheObj *pCacheObj) { #if defined(LINUX) - pthread_rwlock_wrlock(&pCacheObj->lock); + taosThreadRwlockWrlock(&pCacheObj->lock); #else - pthread_mutex_lock(&pCacheObj->lock); + taosThreadMutexLock(&pCacheObj->lock); #endif } static FORCE_INLINE void __trashcan_unlock(SCacheObj *pCacheObj) { #if defined(LINUX) - pthread_rwlock_unlock(&pCacheObj->lock); + taosThreadRwlockUnlock(&pCacheObj->lock); #else - pthread_mutex_unlock(&pCacheObj->lock); + taosThreadMutexUnlock(&pCacheObj->lock); #endif } static FORCE_INLINE int32_t __trashcan_lock_init(SCacheObj *pCacheObj) { #if defined(LINUX) - return pthread_rwlock_init(&pCacheObj->lock, NULL); + return taosThreadRwlockInit(&pCacheObj->lock, NULL); #else - return pthread_mutex_init(&pCacheObj->lock, NULL); + return taosThreadMutexInit(&pCacheObj->lock, NULL); #endif } static FORCE_INLINE void __trashcan_lock_destroy(SCacheObj *pCacheObj) { #if defined(LINUX) - pthread_rwlock_destroy(&pCacheObj->lock); + taosThreadRwlockDestroy(&pCacheObj->lock); #else - pthread_mutex_destroy(&pCacheObj->lock); + taosThreadMutexDestroy(&pCacheObj->lock); #endif } @@ -154,20 +154,20 @@ static void *taosCacheTimedRefresh(void *handle); static void doInitRefreshThread(void) { pCacheArrayList = taosArrayInit(4, POINTER_BYTES); - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); - pthread_create(&cacheRefreshWorker, &thattr, taosCacheTimedRefresh, NULL); - pthread_attr_destroy(&thattr); + taosThreadCreate(&cacheRefreshWorker, &thattr, taosCacheTimedRefresh, NULL); + taosThreadAttrDestroy(&thattr); } -pthread_t doRegisterCacheObj(SCacheObj *pCacheObj) { - pthread_once(&cacheThreadInit, doInitRefreshThread); +TdThread doRegisterCacheObj(SCacheObj *pCacheObj) { + taosThreadOnce(&cacheThreadInit, doInitRefreshThread); - pthread_mutex_lock(&guard); + taosThreadMutexLock(&guard); taosArrayPush(pCacheArrayList, &pCacheObj); - pthread_mutex_unlock(&guard); + taosThreadMutexUnlock(&guard); return cacheRefreshWorker; } @@ -836,19 +836,19 @@ void *taosCacheTimedRefresh(void *handle) { goto _end; } - pthread_mutex_lock(&guard); + taosThreadMutexLock(&guard); size_t size = taosArrayGetSize(pCacheArrayList); - pthread_mutex_unlock(&guard); + taosThreadMutexUnlock(&guard); count += 1; for (int32_t i = 0; i < size; ++i) { - pthread_mutex_lock(&guard); + taosThreadMutexLock(&guard); SCacheObj *pCacheObj = taosArrayGetP(pCacheArrayList, i); if (pCacheObj == NULL) { uError("object is destroyed. ignore and try next"); - pthread_mutex_unlock(&guard); + taosThreadMutexUnlock(&guard); continue; } @@ -860,11 +860,11 @@ void *taosCacheTimedRefresh(void *handle) { uDebug("%s is destroying, remove it from refresh list, remain cache obj:%" PRIzu, pCacheObj->name, size); pCacheObj->deleting = 0; // reset the deleting flag to enable pCacheObj to continue releasing resources. - pthread_mutex_unlock(&guard); + taosThreadMutexUnlock(&guard); continue; } - pthread_mutex_unlock(&guard); + taosThreadMutexUnlock(&guard); if ((count % pCacheObj->checkTick) != 0) { continue; @@ -892,7 +892,7 @@ _end: taosArrayDestroy(pCacheArrayList); pCacheArrayList = NULL; - pthread_mutex_destroy(&guard); + taosThreadMutexDestroy(&guard); refreshWorkerNormalStopped = true; uDebug("cache refresh thread quits"); diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 469b944bf9..2603d1d51a 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -455,14 +455,14 @@ static int32_t taosCompareTaosError(const void* a, const void* b) { return 0; } -static pthread_once_t tsErrorInit = PTHREAD_ONCE_INIT; +static TdThreadOnce tsErrorInit = PTHREAD_ONCE_INIT; static void tsSortError(void) { qsort(errors, sizeof(errors) / sizeof(errors[0]), sizeof(errors[0]), taosCompareTaosError); } const char* tstrerror(int32_t err) { - pthread_once(&tsErrorInit, tsSortError); + taosThreadOnce(&tsErrorInit, tsSortError); // this is a system errno if ((err & 0x00ff0000) == 0x00ff0000) { diff --git a/source/util/src/tidpool.c b/source/util/src/tidpool.c index 3ae537eae8..705cb0d2d3 100644 --- a/source/util/src/tidpool.c +++ b/source/util/src/tidpool.c @@ -31,7 +31,7 @@ void *taosInitIdPool(int32_t maxId) { pIdPool->numOfFree = maxId; pIdPool->freeSlot = 0; - pthread_mutex_init(&pIdPool->mutex, NULL); + taosThreadMutexInit(&pIdPool->mutex, NULL); uDebug("pool:%p is setup, maxId:%d", pIdPool, pIdPool->maxId); @@ -42,7 +42,7 @@ int32_t taosAllocateId(id_pool_t *pIdPool) { if (pIdPool == NULL) return -1; int32_t slot = -1; - pthread_mutex_lock(&pIdPool->mutex); + taosThreadMutexLock(&pIdPool->mutex); if (pIdPool->numOfFree > 0) { for (int32_t i = 0; i < pIdPool->maxId; ++i) { @@ -56,14 +56,14 @@ int32_t taosAllocateId(id_pool_t *pIdPool) { } } - pthread_mutex_unlock(&pIdPool->mutex); + taosThreadMutexUnlock(&pIdPool->mutex); return slot + 1; } void taosFreeId(id_pool_t *pIdPool, int32_t id) { if (pIdPool == NULL) return; - pthread_mutex_lock(&pIdPool->mutex); + taosThreadMutexLock(&pIdPool->mutex); int32_t slot = (id - 1) % pIdPool->maxId; if (pIdPool->freeList[slot]) { @@ -71,7 +71,7 @@ void taosFreeId(id_pool_t *pIdPool, int32_t id) { pIdPool->numOfFree++; } - pthread_mutex_unlock(&pIdPool->mutex); + taosThreadMutexUnlock(&pIdPool->mutex); } void taosIdPoolCleanUp(id_pool_t *pIdPool) { @@ -81,7 +81,7 @@ void taosIdPoolCleanUp(id_pool_t *pIdPool) { if (pIdPool->freeList) free(pIdPool->freeList); - pthread_mutex_destroy(&pIdPool->mutex); + taosThreadMutexDestroy(&pIdPool->mutex); memset(pIdPool, 0, sizeof(id_pool_t)); @@ -89,16 +89,16 @@ void taosIdPoolCleanUp(id_pool_t *pIdPool) { } int32_t taosIdPoolNumOfUsed(id_pool_t *pIdPool) { - pthread_mutex_lock(&pIdPool->mutex); + taosThreadMutexLock(&pIdPool->mutex); int32_t ret = pIdPool->maxId - pIdPool->numOfFree; - pthread_mutex_unlock(&pIdPool->mutex); + taosThreadMutexUnlock(&pIdPool->mutex); return ret; } bool taosIdPoolMarkStatus(id_pool_t *pIdPool, int32_t id) { bool ret = false; - pthread_mutex_lock(&pIdPool->mutex); + taosThreadMutexLock(&pIdPool->mutex); int32_t slot = (id - 1) % pIdPool->maxId; if (!pIdPool->freeList[slot]) { @@ -109,7 +109,7 @@ bool taosIdPoolMarkStatus(id_pool_t *pIdPool, int32_t id) { ret = false; } - pthread_mutex_unlock(&pIdPool->mutex); + taosThreadMutexUnlock(&pIdPool->mutex); return ret; } @@ -123,7 +123,7 @@ int32_t taosUpdateIdPool(id_pool_t *pIdPool, int32_t maxId) { return -1; } - pthread_mutex_lock(&pIdPool->mutex); + taosThreadMutexLock(&pIdPool->mutex); memcpy(idList, pIdPool->freeList, sizeof(bool) * pIdPool->maxId); pIdPool->numOfFree += (maxId - pIdPool->maxId); @@ -133,15 +133,15 @@ int32_t taosUpdateIdPool(id_pool_t *pIdPool, int32_t maxId) { pIdPool->freeList = idList; free(oldIdList); - pthread_mutex_unlock(&pIdPool->mutex); + taosThreadMutexUnlock(&pIdPool->mutex); return 0; } int32_t taosIdPoolMaxSize(id_pool_t *pIdPool) { - pthread_mutex_lock(&pIdPool->mutex); + taosThreadMutexLock(&pIdPool->mutex); int32_t ret = pIdPool->maxId; - pthread_mutex_unlock(&pIdPool->mutex); + taosThreadMutexUnlock(&pIdPool->mutex); return ret; } \ No newline at end of file diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 6dedc3f740..63199353af 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -45,8 +45,8 @@ typedef struct { int32_t minBuffSize; TdFilePtr pFile; int32_t stop; - pthread_t asyncThread; - pthread_mutex_t buffMutex; + TdThread asyncThread; + TdThreadMutex buffMutex; tsem_t buffNotEmpty; } SLogBuff; @@ -59,7 +59,7 @@ typedef struct { pid_t pid; char logName[LOG_FILE_NAME_LEN]; SLogBuff *logHandle; - pthread_mutex_t logMutex; + TdThreadMutex logMutex; } SLogObj; static int8_t tsLogInited = 0; @@ -107,12 +107,12 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) static int32_t taosCompressFile(char *srcFileName, char *destFileName); static int32_t taosStartLog() { - pthread_attr_t threadAttr; - pthread_attr_init(&threadAttr); - if (pthread_create(&(tsLogObj.logHandle->asyncThread), &threadAttr, taosAsyncOutputLog, tsLogObj.logHandle) != 0) { + TdThreadAttr threadAttr; + taosThreadAttrInit(&threadAttr); + if (taosThreadCreate(&(tsLogObj.logHandle->asyncThread), &threadAttr, taosAsyncOutputLog, tsLogObj.logHandle) != 0) { return -1; } - pthread_attr_destroy(&threadAttr); + taosThreadAttrDestroy(&threadAttr); return 0; } @@ -139,7 +139,7 @@ static void taosStopLog() { void taosCloseLog() { taosStopLog(); if (taosCheckPthreadValid(tsLogObj.logHandle->asyncThread)) { - pthread_join(tsLogObj.logHandle->asyncThread, NULL); + taosThreadJoin(tsLogObj.logHandle->asyncThread, NULL); } // In case that other threads still use log resources causing invalid write in valgrind // we comment two lines below. @@ -223,22 +223,22 @@ static void *taosThreadToOpenNewFile(void *param) { } static int32_t taosOpenNewLogFile() { - pthread_mutex_lock(&tsLogObj.logMutex); + taosThreadMutexLock(&tsLogObj.logMutex); if (tsLogObj.lines > tsLogObj.maxLines && tsLogObj.openInProgress == 0) { tsLogObj.openInProgress = 1; uInfo("open new log file ......"); - pthread_t thread; - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + TdThread thread; + TdThreadAttr attr; + taosThreadAttrInit(&attr); + taosThreadAttrSetDetachState(&attr, PTHREAD_CREATE_DETACHED); - pthread_create(&thread, &attr, taosThreadToOpenNewFile, NULL); - pthread_attr_destroy(&attr); + taosThreadCreate(&thread, &attr, taosThreadToOpenNewFile, NULL); + taosThreadAttrDestroy(&attr); } - pthread_mutex_unlock(&tsLogObj.logMutex); + taosThreadMutexUnlock(&tsLogObj.logMutex); return 0; } @@ -343,7 +343,7 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) { char fileName[LOG_FILE_NAME_LEN + 50] = "\0"; sprintf(fileName, "%s.%d", tsLogObj.logName, tsLogObj.flag); - pthread_mutex_init(&tsLogObj.logMutex, NULL); + taosThreadMutexInit(&tsLogObj.logMutex, NULL); taosUmaskFile(0); tsLogObj.logHandle->pFile = taosOpenFile(fileName, TD_FILE_CTEATE | TD_FILE_WRITE); @@ -517,7 +517,7 @@ static SLogBuff *taosLogBuffNew(int32_t bufSize) { tLogBuff->minBuffSize = bufSize / 10; tLogBuff->stop = 0; - if (pthread_mutex_init(&LOG_BUF_MUTEX(tLogBuff), NULL) < 0) goto _err; + if (taosThreadMutexInit(&LOG_BUF_MUTEX(tLogBuff), NULL) < 0) goto _err; // tsem_init(&(tLogBuff->buffNotEmpty), 0, 0); return tLogBuff; @@ -552,7 +552,7 @@ static int32_t taosPushLogBuffer(SLogBuff *tLogBuff, const char *msg, int32_t ms if (tLogBuff == NULL || tLogBuff->stop) return -1; - pthread_mutex_lock(&LOG_BUF_MUTEX(tLogBuff)); + taosThreadMutexLock(&LOG_BUF_MUTEX(tLogBuff)); start = LOG_BUF_START(tLogBuff); end = LOG_BUF_END(tLogBuff); @@ -566,7 +566,7 @@ static int32_t taosPushLogBuffer(SLogBuff *tLogBuff, const char *msg, int32_t ms if (remainSize <= msgLen || ((lostLine > 0) && (remainSize <= (msgLen + tmpBufLen)))) { lostLine++; tsAsyncLogLostLines++; - pthread_mutex_unlock(&LOG_BUF_MUTEX(tLogBuff)); + taosThreadMutexUnlock(&LOG_BUF_MUTEX(tLogBuff)); return -1; } @@ -587,7 +587,7 @@ static int32_t taosPushLogBuffer(SLogBuff *tLogBuff, const char *msg, int32_t ms } */ - pthread_mutex_unlock(&LOG_BUF_MUTEX(tLogBuff)); + taosThreadMutexUnlock(&LOG_BUF_MUTEX(tLogBuff)); return 0; } diff --git a/source/util/src/tmempool.c b/source/util/src/tmempool.c index 1fc9bfc7ab..d62e903977 100644 --- a/source/util/src/tmempool.c +++ b/source/util/src/tmempool.c @@ -25,7 +25,7 @@ typedef struct { int32_t blockSize; /* block size in bytes */ int32_t *freeList; /* the index list */ char *pool; /* the actual mem block */ - pthread_mutex_t mutex; + TdThreadMutex mutex; } pool_t; mpool_h taosMemPoolInit(int32_t numOfBlock, int32_t blockSize) { @@ -58,7 +58,7 @@ mpool_h taosMemPoolInit(int32_t numOfBlock, int32_t blockSize) { return NULL; } - pthread_mutex_init(&(pool_p->mutex), NULL); + taosThreadMutexInit(&(pool_p->mutex), NULL); memset(pool_p->pool, 0, (size_t)(blockSize * numOfBlock)); for (i = 0; i < pool_p->numOfBlock; ++i) pool_p->freeList[i] = i; @@ -73,7 +73,7 @@ char *taosMemPoolMalloc(mpool_h handle) { char *pos = NULL; pool_t *pool_p = (pool_t *)handle; - pthread_mutex_lock(&(pool_p->mutex)); + taosThreadMutexLock(&(pool_p->mutex)); if (pool_p->numOfFree > 0) { pos = pool_p->pool + pool_p->blockSize * (pool_p->freeList[pool_p->first]); @@ -82,7 +82,7 @@ char *taosMemPoolMalloc(mpool_h handle) { pool_p->numOfFree--; } - pthread_mutex_unlock(&(pool_p->mutex)); + taosThreadMutexUnlock(&(pool_p->mutex)); if (pos == NULL) uDebug("mempool: out of memory"); return pos; @@ -108,18 +108,18 @@ void taosMemPoolFree(mpool_h handle, char *pMem) { memset(pMem, 0, (size_t)pool_p->blockSize); - pthread_mutex_lock(&pool_p->mutex); + taosThreadMutexLock(&pool_p->mutex); pool_p->freeList[(pool_p->first + pool_p->numOfFree) % pool_p->numOfBlock] = index; pool_p->numOfFree++; - pthread_mutex_unlock(&pool_p->mutex); + taosThreadMutexUnlock(&pool_p->mutex); } void taosMemPoolCleanUp(mpool_h handle) { pool_t *pool_p = (pool_t *)handle; - pthread_mutex_destroy(&pool_p->mutex); + taosThreadMutexDestroy(&pool_p->mutex); if (pool_p->pool) free(pool_p->pool); if (pool_p->freeList) free(pool_p->freeList); memset(pool_p, 0, sizeof(*pool_p)); diff --git a/source/util/src/tqueue.c b/source/util/src/tqueue.c index 0003522033..99675630f4 100644 --- a/source/util/src/tqueue.c +++ b/source/util/src/tqueue.c @@ -37,13 +37,13 @@ typedef struct STaosQueue { void *ahandle; // for queue set FItem itemFp; FItems itemsFp; - pthread_mutex_t mutex; + TdThreadMutex mutex; } STaosQueue; typedef struct STaosQset { STaosQueue *head; STaosQueue *current; - pthread_mutex_t mutex; + TdThreadMutex mutex; int32_t numOfQueues; int32_t numOfItems; tsem_t sem; @@ -63,7 +63,7 @@ STaosQueue *taosOpenQueue() { return NULL; } - if (pthread_mutex_init(&queue->mutex, NULL) != 0) { + if (taosThreadMutexInit(&queue->mutex, NULL) != 0) { terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -84,11 +84,11 @@ void taosCloseQueue(STaosQueue *queue) { STaosQnode *pTemp; STaosQset *qset; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); STaosQnode *pNode = queue->head; queue->head = NULL; qset = queue->qset; - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); if (queue->qset) { taosRemoveFromQset(qset, queue); @@ -100,7 +100,7 @@ void taosCloseQueue(STaosQueue *queue) { free(pTemp); } - pthread_mutex_destroy(&queue->mutex); + taosThreadMutexDestroy(&queue->mutex); free(queue); uDebug("queue:%p is closed", queue); @@ -110,19 +110,19 @@ bool taosQueueEmpty(STaosQueue *queue) { if (queue == NULL) return true; bool empty = false; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); if (queue->head == NULL && queue->tail == NULL) { empty = true; } - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); return empty; } int32_t taosQueueSize(STaosQueue *queue) { - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); int32_t numOfItems = queue->numOfItems; - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); return numOfItems; } @@ -151,7 +151,7 @@ int32_t taosWriteQitem(STaosQueue *queue, void *pItem) { STaosQnode *pNode = (STaosQnode *)(((char *)pItem) - sizeof(STaosQnode)); pNode->next = NULL; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); if (queue->tail) { queue->tail->next = pNode; @@ -165,7 +165,7 @@ int32_t taosWriteQitem(STaosQueue *queue, void *pItem) { if (queue->qset) atomic_add_fetch_32(&queue->qset->numOfItems, 1); uTrace("item:%p is put into queue:%p, items:%d", pItem, queue, queue->numOfItems); - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); if (queue->qset) tsem_post(&queue->qset->sem); @@ -176,7 +176,7 @@ int32_t taosReadQitem(STaosQueue *queue, void **ppItem) { STaosQnode *pNode = NULL; int32_t code = 0; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); if (queue->head) { pNode = queue->head; @@ -189,7 +189,7 @@ int32_t taosReadQitem(STaosQueue *queue, void **ppItem) { uTrace("item:%p is read out from queue:%p, items:%d", *ppItem, queue, queue->numOfItems); } - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); return code; } @@ -202,7 +202,7 @@ int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) { int32_t code = 0; bool empty; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); empty = queue->head == NULL; if (!empty) { @@ -219,7 +219,7 @@ int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) { if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, qall->numOfItems); } - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); // if source queue is empty, we set destination qall to empty too. if (empty) { @@ -255,7 +255,7 @@ STaosQset *taosOpenQset() { return NULL; } - pthread_mutex_init(&qset->mutex, NULL); + taosThreadMutexInit(&qset->mutex, NULL); tsem_init(&qset->sem, 0, 0); uDebug("qset:%p is opened", qset); @@ -266,7 +266,7 @@ void taosCloseQset(STaosQset *qset) { if (qset == NULL) return; // remove all the queues from qset - pthread_mutex_lock(&qset->mutex); + taosThreadMutexLock(&qset->mutex); while (qset->head) { STaosQueue *queue = qset->head; qset->head = qset->head->next; @@ -274,9 +274,9 @@ void taosCloseQset(STaosQset *qset) { queue->qset = NULL; queue->next = NULL; } - pthread_mutex_unlock(&qset->mutex); + taosThreadMutexUnlock(&qset->mutex); - pthread_mutex_destroy(&qset->mutex); + taosThreadMutexDestroy(&qset->mutex); tsem_destroy(&qset->sem); free(qset); uDebug("qset:%p is closed", qset); @@ -293,19 +293,19 @@ void taosQsetThreadResume(STaosQset *qset) { int32_t taosAddIntoQset(STaosQset *qset, STaosQueue *queue, void *ahandle) { if (queue->qset) return -1; - pthread_mutex_lock(&qset->mutex); + taosThreadMutexLock(&qset->mutex); queue->next = qset->head; queue->ahandle = ahandle; qset->head = queue; qset->numOfQueues++; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems); queue->qset = qset; - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); - pthread_mutex_unlock(&qset->mutex); + taosThreadMutexUnlock(&qset->mutex); uTrace("queue:%p is added into qset:%p", queue, qset); return 0; @@ -314,7 +314,7 @@ int32_t taosAddIntoQset(STaosQset *qset, STaosQueue *queue, void *ahandle) { void taosRemoveFromQset(STaosQset *qset, STaosQueue *queue) { STaosQueue *tqueue = NULL; - pthread_mutex_lock(&qset->mutex); + taosThreadMutexLock(&qset->mutex); if (qset->head) { if (qset->head == queue) { @@ -339,15 +339,15 @@ void taosRemoveFromQset(STaosQset *qset, STaosQueue *queue) { if (qset->current == queue) qset->current = tqueue->next; qset->numOfQueues--; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); atomic_sub_fetch_32(&qset->numOfItems, queue->numOfItems); queue->qset = NULL; queue->next = NULL; - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); } } - pthread_mutex_unlock(&qset->mutex); + taosThreadMutexUnlock(&qset->mutex); uDebug("queue:%p is removed from qset:%p", queue, qset); } @@ -360,7 +360,7 @@ int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, void **ahandle, FI tsem_wait(&qset->sem); - pthread_mutex_lock(&qset->mutex); + taosThreadMutexLock(&qset->mutex); for (int32_t i = 0; i < qset->numOfQueues; ++i) { if (qset->current == NULL) qset->current = qset->head; @@ -369,7 +369,7 @@ int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, void **ahandle, FI if (queue == NULL) break; if (queue->head == NULL) continue; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); if (queue->head) { pNode = queue->head; @@ -385,11 +385,11 @@ int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, void **ahandle, FI uTrace("item:%p is read out from queue:%p, items:%d", *ppItem, queue, queue->numOfItems); } - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); if (pNode) break; } - pthread_mutex_unlock(&qset->mutex); + taosThreadMutexUnlock(&qset->mutex); return code; } @@ -399,7 +399,7 @@ int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, void **ahand int32_t code = 0; tsem_wait(&qset->sem); - pthread_mutex_lock(&qset->mutex); + taosThreadMutexLock(&qset->mutex); for (int32_t i = 0; i < qset->numOfQueues; ++i) { if (qset->current == NULL) qset->current = qset->head; @@ -408,7 +408,7 @@ int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, void **ahand if (queue == NULL) break; if (queue->head == NULL) continue; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); if (queue->head) { qall->current = queue->head; @@ -428,12 +428,12 @@ int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, void **ahand } } - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); if (code != 0) break; } - pthread_mutex_unlock(&qset->mutex); + taosThreadMutexUnlock(&qset->mutex); return code; } @@ -443,7 +443,7 @@ int32_t taosReadQitemFromQsetByThread(STaosQset *qset, void **ppItem, void **aha tsem_wait(&qset->sem); - pthread_mutex_lock(&qset->mutex); + taosThreadMutexLock(&qset->mutex); for (int32_t i = 0; i < qset->numOfQueues; ++i) { if (qset->current == NULL) qset->current = qset->head; @@ -456,7 +456,7 @@ int32_t taosReadQitemFromQsetByThread(STaosQset *qset, void **ppItem, void **aha continue; } - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); if (queue->head) { pNode = queue->head; @@ -475,11 +475,11 @@ int32_t taosReadQitemFromQsetByThread(STaosQset *qset, void **ppItem, void **aha uTrace("item:%p is read out from queue:%p, items:%d", *ppItem, queue, queue->numOfItems); } - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); if (pNode) break; } - pthread_mutex_unlock(&qset->mutex); + taosThreadMutexUnlock(&qset->mutex); return code; } @@ -488,21 +488,21 @@ void taosResetQsetThread(STaosQset *qset, void *pItem) { if (pItem == NULL) return; STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode)); - pthread_mutex_lock(&qset->mutex); + taosThreadMutexLock(&qset->mutex); pNode->queue->threadId = -1; for (int32_t i = 0; i < pNode->queue->numOfItems; ++i) { tsem_post(&qset->sem); } - pthread_mutex_unlock(&qset->mutex); + taosThreadMutexUnlock(&qset->mutex); } int32_t taosGetQueueItemsNumber(STaosQueue *queue) { if (!queue) return 0; int32_t num; - pthread_mutex_lock(&queue->mutex); + taosThreadMutexLock(&queue->mutex); num = queue->numOfItems; - pthread_mutex_unlock(&queue->mutex); + taosThreadMutexUnlock(&queue->mutex); return num; } @@ -510,8 +510,8 @@ int32_t taosGetQsetItemsNumber(STaosQset *qset) { if (!qset) return 0; int32_t num = 0; - pthread_mutex_lock(&qset->mutex); + taosThreadMutexLock(&qset->mutex); num = qset->numOfItems; - pthread_mutex_unlock(&qset->mutex); + taosThreadMutexUnlock(&qset->mutex); return num; } diff --git a/source/util/src/tref.c b/source/util/src/tref.c index a9f6c21bf8..86633fe07a 100644 --- a/source/util/src/tref.c +++ b/source/util/src/tref.c @@ -45,8 +45,8 @@ typedef struct { } SRefSet; static SRefSet tsRefSetList[TSDB_REF_OBJECTS]; -static pthread_once_t tsRefModuleInit = PTHREAD_ONCE_INIT; -static pthread_mutex_t tsRefMutex; +static TdThreadOnce tsRefModuleInit = PTHREAD_ONCE_INIT; +static TdThreadMutex tsRefMutex; static int32_t tsRefSetNum = 0; static int32_t tsNextId = 0; @@ -63,7 +63,7 @@ int32_t taosOpenRef(int32_t max, void (*fp)(void *)) { int64_t *lockedBy; int32_t i, rsetId; - pthread_once(&tsRefModuleInit, taosInitRefModule); + taosThreadOnce(&tsRefModuleInit, taosInitRefModule); nodeList = calloc(sizeof(SRefNode *), (size_t)max); if (nodeList == NULL) { @@ -78,7 +78,7 @@ int32_t taosOpenRef(int32_t max, void (*fp)(void *)) { return -1; } - pthread_mutex_lock(&tsRefMutex); + taosThreadMutexLock(&tsRefMutex); for (i = 0; i < TSDB_REF_OBJECTS; ++i) { tsNextId = (tsNextId + 1) % TSDB_REF_OBJECTS; @@ -107,7 +107,7 @@ int32_t taosOpenRef(int32_t max, void (*fp)(void *)) { uTrace("run out of Ref ID, maximum:%d refSetNum:%d", TSDB_REF_OBJECTS, tsRefSetNum); } - pthread_mutex_unlock(&tsRefMutex); + taosThreadMutexUnlock(&tsRefMutex); return rsetId; } @@ -124,7 +124,7 @@ int32_t taosCloseRef(int32_t rsetId) { pSet = tsRefSetList + rsetId; - pthread_mutex_lock(&tsRefMutex); + taosThreadMutexLock(&tsRefMutex); if (pSet->state == TSDB_REF_STATE_ACTIVE) { pSet->state = TSDB_REF_STATE_DELETED; @@ -134,7 +134,7 @@ int32_t taosCloseRef(int32_t rsetId) { uTrace("rsetId:%d is already closed, count:%d", rsetId, pSet->count); } - pthread_mutex_unlock(&tsRefMutex); + taosThreadMutexUnlock(&tsRefMutex); if (deleted) taosDecRsetCount(pSet); @@ -354,7 +354,7 @@ int32_t taosListRef() { SRefNode *pNode; int32_t num = 0; - pthread_mutex_lock(&tsRefMutex); + taosThreadMutexLock(&tsRefMutex); for (int32_t i = 0; i < TSDB_REF_OBJECTS; ++i) { pSet = tsRefSetList + i; @@ -374,7 +374,7 @@ int32_t taosListRef() { } } - pthread_mutex_unlock(&tsRefMutex); + taosThreadMutexUnlock(&tsRefMutex); return num; } @@ -470,7 +470,7 @@ static void taosUnlockList(int64_t *lockedBy) { } } -static void taosInitRefModule(void) { pthread_mutex_init(&tsRefMutex, NULL); } +static void taosInitRefModule(void) { taosThreadMutexInit(&tsRefMutex, NULL); } static void taosIncRsetCount(SRefSet *pSet) { atomic_add_fetch_32(&pSet->count, 1); @@ -483,7 +483,7 @@ static void taosDecRsetCount(SRefSet *pSet) { if (count > 0) return; - pthread_mutex_lock(&tsRefMutex); + taosThreadMutexLock(&tsRefMutex); if (pSet->state != TSDB_REF_STATE_EMPTY) { pSet->state = TSDB_REF_STATE_EMPTY; @@ -497,5 +497,5 @@ static void taosDecRsetCount(SRefSet *pSet) { uTrace("rsetId:%d is cleaned, refSetNum:%d count:%d", pSet->rsetId, tsRefSetNum, pSet->count); } - pthread_mutex_unlock(&tsRefMutex); + taosThreadMutexUnlock(&tsRefMutex); } diff --git a/source/util/src/tsched.c b/source/util/src/tsched.c index 740e742bad..316c1f7ec1 100644 --- a/source/util/src/tsched.c +++ b/source/util/src/tsched.c @@ -26,12 +26,12 @@ typedef struct { char label[TSDB_LABEL_LEN]; tsem_t emptySem; tsem_t fullSem; - pthread_mutex_t queueMutex; + TdThreadMutex queueMutex; int32_t fullSlot; int32_t emptySlot; int32_t queueSize; int32_t numOfThreads; - pthread_t *qthread; + TdThread *qthread; SSchedMsg *queue; bool stop; void *pTmrCtrl; @@ -55,7 +55,7 @@ void *taosInitScheduler(int32_t queueSize, int32_t numOfThreads, const char *lab return NULL; } - pSched->qthread = calloc(sizeof(pthread_t), numOfThreads); + pSched->qthread = calloc(sizeof(TdThread), numOfThreads); if (pSched->qthread == NULL) { uError("%s: no enough memory for qthread", label); taosCleanUpScheduler(pSched); @@ -68,7 +68,7 @@ void *taosInitScheduler(int32_t queueSize, int32_t numOfThreads, const char *lab pSched->fullSlot = 0; pSched->emptySlot = 0; - if (pthread_mutex_init(&pSched->queueMutex, NULL) < 0) { + if (taosThreadMutexInit(&pSched->queueMutex, NULL) < 0) { uError("init %s:queueMutex failed(%s)", label, strerror(errno)); taosCleanUpScheduler(pSched); return NULL; @@ -88,11 +88,11 @@ void *taosInitScheduler(int32_t queueSize, int32_t numOfThreads, const char *lab pSched->stop = false; for (int32_t i = 0; i < numOfThreads; ++i) { - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - int32_t code = pthread_create(pSched->qthread + i, &attr, taosProcessSchedQueue, (void *)pSched); - pthread_attr_destroy(&attr); + TdThreadAttr attr; + taosThreadAttrInit(&attr); + taosThreadAttrSetDetachState(&attr, PTHREAD_CREATE_JOINABLE); + int32_t code = taosThreadCreate(pSched->qthread + i, &attr, taosProcessSchedQueue, (void *)pSched); + taosThreadAttrDestroy(&attr); if (code != 0) { uError("%s: failed to create rpc thread(%s)", label, strerror(errno)); taosCleanUpScheduler(pSched); @@ -135,7 +135,7 @@ void *taosProcessSchedQueue(void *scheduler) { break; } - if ((ret = pthread_mutex_lock(&pSched->queueMutex)) != 0) { + if ((ret = taosThreadMutexLock(&pSched->queueMutex)) != 0) { uFatal("lock %s queueMutex failed(%s)", pSched->label, strerror(errno)); exit(ret); } @@ -144,7 +144,7 @@ void *taosProcessSchedQueue(void *scheduler) { memset(pSched->queue + pSched->fullSlot, 0, sizeof(SSchedMsg)); pSched->fullSlot = (pSched->fullSlot + 1) % pSched->queueSize; - if ((ret = pthread_mutex_unlock(&pSched->queueMutex)) != 0) { + if ((ret = taosThreadMutexUnlock(&pSched->queueMutex)) != 0) { uFatal("unlock %s queueMutex failed(%s)", pSched->label, strerror(errno)); exit(ret); } @@ -177,7 +177,7 @@ void taosScheduleTask(void *queueScheduler, SSchedMsg *pMsg) { exit(ret); } - if ((ret = pthread_mutex_lock(&pSched->queueMutex)) != 0) { + if ((ret = taosThreadMutexLock(&pSched->queueMutex)) != 0) { uFatal("lock %s queueMutex failed(%s)", pSched->label, strerror(errno)); exit(ret); } @@ -185,7 +185,7 @@ void taosScheduleTask(void *queueScheduler, SSchedMsg *pMsg) { pSched->queue[pSched->emptySlot] = *pMsg; pSched->emptySlot = (pSched->emptySlot + 1) % pSched->queueSize; - if ((ret = pthread_mutex_unlock(&pSched->queueMutex)) != 0) { + if ((ret = taosThreadMutexUnlock(&pSched->queueMutex)) != 0) { uFatal("unlock %s queueMutex failed(%s)", pSched->label, strerror(errno)); exit(ret); } @@ -208,13 +208,13 @@ void taosCleanUpScheduler(void *param) { } for (int32_t i = 0; i < pSched->numOfThreads; ++i) { if (taosCheckPthreadValid(pSched->qthread[i])) { - pthread_join(pSched->qthread[i], NULL); + taosThreadJoin(pSched->qthread[i], NULL); } } tsem_destroy(&pSched->emptySem); tsem_destroy(&pSched->fullSem); - pthread_mutex_destroy(&pSched->queueMutex); + taosThreadMutexDestroy(&pSched->queueMutex); if (pSched->pTimer) { taosTmrStopA(&pSched->pTimer); diff --git a/source/util/src/tskiplist.c b/source/util/src/tskiplist.c index b5e54d12d3..f7e56b6f31 100644 --- a/source/util/src/tskiplist.c +++ b/source/util/src/tskiplist.c @@ -70,13 +70,13 @@ SSkipList *tSkipListCreate(uint8_t maxLevel, uint8_t keyType, uint16_t keyLen, _ } if (SL_IS_THREAD_SAFE(pSkipList)) { - pSkipList->lock = (pthread_rwlock_t *)calloc(1, sizeof(pthread_rwlock_t)); + pSkipList->lock = (TdThreadRwlock *)calloc(1, sizeof(TdThreadRwlock)); if (pSkipList->lock == NULL) { tSkipListDestroy(pSkipList); return NULL; } - if (pthread_rwlock_init(pSkipList->lock, NULL) != 0) { + if (taosThreadRwlockInit(pSkipList->lock, NULL) != 0) { tSkipListDestroy(pSkipList); return NULL; } @@ -109,7 +109,7 @@ void tSkipListDestroy(SSkipList *pSkipList) { tSkipListUnlock(pSkipList); if (pSkipList->lock != NULL) { - pthread_rwlock_destroy(pSkipList->lock); + taosThreadRwlockDestroy(pSkipList->lock); tfree(pSkipList->lock); } @@ -435,21 +435,21 @@ static SSkipListIterator *doCreateSkipListIterator(SSkipList *pSkipList, int32_t static FORCE_INLINE int32_t tSkipListWLock(SSkipList *pSkipList) { if (pSkipList->lock) { - return pthread_rwlock_wrlock(pSkipList->lock); + return taosThreadRwlockWrlock(pSkipList->lock); } return 0; } static FORCE_INLINE int32_t tSkipListRLock(SSkipList *pSkipList) { if (pSkipList->lock) { - return pthread_rwlock_rdlock(pSkipList->lock); + return taosThreadRwlockRdlock(pSkipList->lock); } return 0; } static FORCE_INLINE int32_t tSkipListUnlock(SSkipList *pSkipList) { if (pSkipList->lock) { - return pthread_rwlock_unlock(pSkipList->lock); + return taosThreadRwlockUnlock(pSkipList->lock); } return 0; } diff --git a/source/util/src/tthread.c b/source/util/src/tthread.c index f9e28d7b62..da76135b68 100644 --- a/source/util/src/tthread.c +++ b/source/util/src/tthread.c @@ -16,13 +16,13 @@ #define _DEFAULT_SOURCE #include "tthread.h" -pthread_t* taosCreateThread(void* (*__start_routine)(void*), void* param) { - pthread_t* pthread = (pthread_t*)malloc(sizeof(pthread_t)); - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - int32_t ret = pthread_create(pthread, &thattr, __start_routine, param); - pthread_attr_destroy(&thattr); +TdThread* taosCreateThread(void* (*__start_routine)(void*), void* param) { + TdThread* pthread = (TdThread*)malloc(sizeof(TdThread)); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); + int32_t ret = taosThreadCreate(pthread, &thattr, __start_routine, param); + taosThreadAttrDestroy(&thattr); if (ret != 0) { free(pthread); @@ -31,20 +31,20 @@ pthread_t* taosCreateThread(void* (*__start_routine)(void*), void* param) { return pthread; } -bool taosDestoryThread(pthread_t* pthread) { +bool taosDestoryThread(TdThread* pthread) { if (pthread == NULL) return false; if (taosThreadRunning(pthread)) { - pthread_cancel(*pthread); - pthread_join(*pthread, NULL); + taosThreadCancel(*pthread); + taosThreadJoin(*pthread, NULL); } free(pthread); return true; } -bool taosThreadRunning(pthread_t* pthread) { +bool taosThreadRunning(TdThread* pthread) { if (pthread == NULL) return false; - int32_t ret = pthread_kill(*pthread, 0); + int32_t ret = taosThreadKill(*pthread, 0); if (ret == ESRCH) return false; if (ret == EINVAL) return false; // alive diff --git a/source/util/src/ttimer.c b/source/util/src/ttimer.c index 46bca6e6cb..cc31d9c94a 100644 --- a/source/util/src/ttimer.c +++ b/source/util/src/ttimer.c @@ -102,7 +102,7 @@ typedef struct timer_map_t { } timer_map_t; typedef struct time_wheel_t { - pthread_mutex_t mutex; + TdThreadMutex mutex; int64_t nextScanAt; uint32_t resolution; uint16_t size; @@ -112,8 +112,8 @@ typedef struct time_wheel_t { int32_t tsMaxTmrCtrl = 512; -static pthread_once_t tmrModuleInit = PTHREAD_ONCE_INIT; -static pthread_mutex_t tmrCtrlMutex; +static TdThreadOnce tmrModuleInit = PTHREAD_ONCE_INIT; +static TdThreadMutex tmrCtrlMutex; static tmr_ctrl_t* tmrCtrls; static tmr_ctrl_t* unusedTmrCtrl = NULL; static void* tmrQhandle; @@ -230,7 +230,7 @@ static void addToWheel(tmr_obj_t* timer, uint32_t delay) { timer->prev = NULL; timer->expireAt = taosGetMonotonicMs() + delay; - pthread_mutex_lock(&wheel->mutex); + taosThreadMutexLock(&wheel->mutex); uint32_t idx = 0; if (timer->expireAt > wheel->nextScanAt) { @@ -248,7 +248,7 @@ static void addToWheel(tmr_obj_t* timer, uint32_t delay) { p->prev = timer; } - pthread_mutex_unlock(&wheel->mutex); + taosThreadMutexUnlock(&wheel->mutex); } static bool removeFromWheel(tmr_obj_t* timer) { @@ -259,7 +259,7 @@ static bool removeFromWheel(tmr_obj_t* timer) { time_wheel_t* wheel = wheels + wheelIdx; bool removed = false; - pthread_mutex_lock(&wheel->mutex); + taosThreadMutexLock(&wheel->mutex); // other thread may modify timer->wheel, check again. if (timer->wheel < tListLen(wheels)) { if (timer->prev != NULL) { @@ -277,7 +277,7 @@ static bool removeFromWheel(tmr_obj_t* timer) { timerDecRef(timer); removed = true; } - pthread_mutex_unlock(&wheel->mutex); + taosThreadMutexUnlock(&wheel->mutex); return removed; } @@ -372,7 +372,7 @@ static void taosTimerLoopFunc(int32_t signo) { time_wheel_t* wheel = wheels + i; while (now >= wheel->nextScanAt) { - pthread_mutex_lock(&wheel->mutex); + taosThreadMutexLock(&wheel->mutex); wheel->index = (wheel->index + 1) % wheel->size; tmr_obj_t* timer = wheel->slots[wheel->index]; while (timer != NULL) { @@ -407,7 +407,7 @@ static void taosTimerLoopFunc(int32_t signo) { timer = next; } wheel->nextScanAt += wheel->resolution; - pthread_mutex_unlock(&wheel->mutex); + taosThreadMutexUnlock(&wheel->mutex); } addToExpired(expired); @@ -528,12 +528,12 @@ static void taosTmrModuleInit(void) { (tmrCtrls + tsMaxTmrCtrl - 1)->next = NULL; unusedTmrCtrl = tmrCtrls; - pthread_mutex_init(&tmrCtrlMutex, NULL); + taosThreadMutexInit(&tmrCtrlMutex, NULL); int64_t now = taosGetMonotonicMs(); for (int32_t i = 0; i < tListLen(wheels); i++) { time_wheel_t* wheel = wheels + i; - if (pthread_mutex_init(&wheel->mutex, NULL) != 0) { + if (taosThreadMutexInit(&wheel->mutex, NULL) != 0) { tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno)); return; } @@ -564,15 +564,15 @@ void* taosTmrInit(int32_t maxNumOfTmrs, int32_t resolution, int32_t longest, con const char* ret = taosMonotonicInit(); tmrDebug("ttimer monotonic clock source:%s", ret); - pthread_once(&tmrModuleInit, taosTmrModuleInit); + taosThreadOnce(&tmrModuleInit, taosTmrModuleInit); - pthread_mutex_lock(&tmrCtrlMutex); + taosThreadMutexLock(&tmrCtrlMutex); tmr_ctrl_t* ctrl = unusedTmrCtrl; if (ctrl != NULL) { unusedTmrCtrl = ctrl->next; numOfTmrCtrl++; } - pthread_mutex_unlock(&tmrCtrlMutex); + taosThreadMutexUnlock(&tmrCtrlMutex); if (ctrl == NULL) { tmrError("%s too many timer controllers, failed to create timer controller.", label); @@ -594,11 +594,11 @@ void taosTmrCleanUp(void* handle) { tmrDebug("%s timer controller is cleaned up.", ctrl->label); ctrl->label[0] = 0; - pthread_mutex_lock(&tmrCtrlMutex); + taosThreadMutexLock(&tmrCtrlMutex); ctrl->next = unusedTmrCtrl; numOfTmrCtrl--; unusedTmrCtrl = ctrl; - pthread_mutex_unlock(&tmrCtrlMutex); + taosThreadMutexUnlock(&tmrCtrlMutex); tmrDebug("time controller's tmr ctrl size: %d", numOfTmrCtrl); if (numOfTmrCtrl <= 0) { @@ -608,11 +608,11 @@ void taosTmrCleanUp(void* handle) { for (int32_t i = 0; i < tListLen(wheels); i++) { time_wheel_t* wheel = wheels + i; - pthread_mutex_destroy(&wheel->mutex); + taosThreadMutexDestroy(&wheel->mutex); free(wheel->slots); } - pthread_mutex_destroy(&tmrCtrlMutex); + taosThreadMutexDestroy(&tmrCtrlMutex); for (size_t i = 0; i < timerMap.size; i++) { timer_list_t* list = timerMap.slots + i; diff --git a/source/util/src/tworker.c b/source/util/src/tworker.c index 1fa70da870..78098af3bd 100644 --- a/source/util/src/tworker.c +++ b/source/util/src/tworker.c @@ -28,7 +28,7 @@ int32_t tQWorkerInit(SQWorkerPool *pool) { return -1; } - if (pthread_mutex_init(&pool->mutex, NULL)) { + if (taosThreadMutexInit(&pool->mutex, NULL)) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } @@ -56,13 +56,13 @@ void tQWorkerCleanup(SQWorkerPool *pool) { SQWorker *worker = pool->workers + i; if (worker == NULL) continue; if (taosCheckPthreadValid(worker->thread)) { - pthread_join(worker->thread, NULL); + taosThreadJoin(worker->thread, NULL); } } tfree(pool->workers); taosCloseQset(pool->qset); - pthread_mutex_destroy(&pool->mutex); + taosThreadMutexDestroy(&pool->mutex); uDebug("worker:%s is closed", pool->name); } @@ -94,10 +94,10 @@ static void *tQWorkerThreadFp(SQWorker *worker) { } STaosQueue *tWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp, ThreadFp threadFp) { - pthread_mutex_lock(&pool->mutex); + taosThreadMutexLock(&pool->mutex); STaosQueue *queue = taosOpenQueue(); if (queue == NULL) { - pthread_mutex_unlock(&pool->mutex); + taosThreadMutexUnlock(&pool->mutex); terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -110,11 +110,11 @@ STaosQueue *tWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp, Threa do { SQWorker *worker = pool->workers + pool->num; - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&worker->thread, &thAttr, threadFp, worker) != 0) { + if (taosThreadCreate(&worker->thread, &thAttr, threadFp, worker) != 0) { uError("worker:%s:%d failed to create thread to process since %s", pool->name, worker->id, strerror(errno)); taosCloseQueue(queue); terrno = TSDB_CODE_OUT_OF_MEMORY; @@ -122,13 +122,13 @@ STaosQueue *tWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp, Threa break; } - pthread_attr_destroy(&thAttr); + taosThreadAttrDestroy(&thAttr); pool->num++; uDebug("worker:%s:%d is launched, total:%d", pool->name, worker->id, pool->num); } while (pool->num < pool->min); } - pthread_mutex_unlock(&pool->mutex); + taosThreadMutexUnlock(&pool->mutex); uDebug("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle); return queue; @@ -194,7 +194,7 @@ int32_t tWWorkerInit(SWWorkerPool *pool) { return -1; } - if (pthread_mutex_init(&pool->mutex, NULL) != 0) { + if (taosThreadMutexInit(&pool->mutex, NULL) != 0) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } @@ -224,14 +224,14 @@ void tWWorkerCleanup(SWWorkerPool *pool) { for (int32_t i = 0; i < pool->max; ++i) { SWWorker *worker = pool->workers + i; if (taosCheckPthreadValid(worker->thread)) { - pthread_join(worker->thread, NULL); + taosThreadJoin(worker->thread, NULL); taosFreeQall(worker->qall); taosCloseQset(worker->qset); } } tfree(pool->workers); - pthread_mutex_destroy(&pool->mutex); + taosThreadMutexDestroy(&pool->mutex); uInfo("worker:%s is closed", pool->name); } @@ -265,12 +265,12 @@ static void *tWWorkerThreadFp(SWWorker *worker) { } STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) { - pthread_mutex_lock(&pool->mutex); + taosThreadMutexLock(&pool->mutex); SWWorker *worker = pool->workers + pool->nextId; STaosQueue *queue = taosOpenQueue(); if (queue == NULL) { - pthread_mutex_unlock(&pool->mutex); + taosThreadMutexUnlock(&pool->mutex); terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -281,7 +281,7 @@ STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) { worker->qset = taosOpenQset(); if (worker->qset == NULL) { taosCloseQueue(queue); - pthread_mutex_unlock(&pool->mutex); + taosThreadMutexUnlock(&pool->mutex); return NULL; } @@ -290,15 +290,15 @@ STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) { if (worker->qall == NULL) { taosCloseQset(worker->qset); taosCloseQueue(queue); - pthread_mutex_unlock(&pool->mutex); + taosThreadMutexUnlock(&pool->mutex); terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker) != 0) { + if (taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker) != 0) { uError("worker:%s:%d failed to create thread to process since %s", pool->name, worker->id, strerror(errno)); taosFreeQall(worker->qall); taosCloseQset(worker->qset); @@ -310,13 +310,13 @@ STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) { pool->nextId = (pool->nextId + 1) % pool->max; } - pthread_attr_destroy(&thAttr); + taosThreadAttrDestroy(&thAttr); } else { taosAddIntoQset(worker->qset, queue, ahandle); pool->nextId = (pool->nextId + 1) % pool->max; } - pthread_mutex_unlock(&pool->mutex); + taosThreadMutexUnlock(&pool->mutex); uDebug("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle); return queue; diff --git a/source/util/test/trefTest.c b/source/util/test/trefTest.c index 58d9d2202e..a439a84562 100644 --- a/source/util/test/trefTest.c +++ b/source/util/test/trefTest.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include "os.h" @@ -102,18 +101,18 @@ void *openRefSpace(void *param) { pSpace->p = (void **) calloc(sizeof(void *), pSpace->refNum); pSpace->rid = calloc(pSpace->refNum, sizeof(int64_t)); - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&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)); + TdThread thread1, thread2, thread3; + taosThreadCreate(&(thread1), &thattr, addRef, (void *)(pSpace)); + taosThreadCreate(&(thread2), &thattr, removeRef, (void *)(pSpace)); + taosThreadCreate(&(thread3), &thattr, acquireRelease, (void *)(pSpace)); - pthread_join(thread1, NULL); - pthread_join(thread2, NULL); - pthread_join(thread3, NULL); + taosThreadJoin(thread1, NULL); + taosThreadJoin(thread2, NULL); + taosThreadJoin(thread3, NULL); for (int i=0; irefNum; ++i) { taosRemoveRef(pSpace->rsetId, pSpace->rid[i]); @@ -161,22 +160,22 @@ int main(int argc, char *argv[]) { taosInitLog("tref.log", 10); SRefSpace *pSpaceList = (SRefSpace *) calloc(sizeof(SRefSpace), threads); - pthread_t *pThreadList = (pthread_t *) calloc(sizeof(pthread_t), threads); + TdThread *pThreadList = (TdThread *) calloc(sizeof(TdThread), threads); - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); for (int i=0; i #include #include #include @@ -5067,11 +5066,11 @@ int main(int argc, char *argv[]) exit(1); } - pthread_t *pThreadList = (pthread_t *) calloc(sizeof(pthread_t), 4); + TdThread *pThreadList = (TdThread *) calloc(sizeof(TdThread), 4); - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); T_par par[4]; par[0].taos = taos[0]; @@ -5083,10 +5082,10 @@ int main(int argc, char *argv[]) par[3].taos = taos[3]; par[3].idx = 3; - pthread_create(&(pThreadList[0]), &thattr, runcase, (void *)&par[0]); - //pthread_create(&(pThreadList[1]), &thattr, runcase, (void *)&par[1]); - //pthread_create(&(pThreadList[2]), &thattr, runcase, (void *)&par[2]); - //pthread_create(&(pThreadList[3]), &thattr, runcase, (void *)&par[3]); + taosThreadCreate(&(pThreadList[0]), &thattr, runcase, (void *)&par[0]); + //taosThreadCreate(&(pThreadList[1]), &thattr, runcase, (void *)&par[1]); + //taosThreadCreate(&(pThreadList[2]), &thattr, runcase, (void *)&par[2]); + //taosThreadCreate(&(pThreadList[3]), &thattr, runcase, (void *)&par[3]); while(1) { taosSsleep(1); diff --git a/tests/script/api/stmtBatchTest.c b/tests/script/api/stmtBatchTest.c index 57cbdf1090..a6c20d282e 100644 --- a/tests/script/api/stmtBatchTest.c +++ b/tests/script/api/stmtBatchTest.c @@ -1,7 +1,6 @@ // TAOS standard API example. The same syntax as MySQL, but only a subet // to compile: gcc -o prepare prepare.c -ltaos -#include #include #include #include @@ -5080,7 +5079,7 @@ int main(int argc, char *argv[]) #if 0 printf("server:%s, threadNum:%d, rows:%d\n\n", serverIp, threadNum, g_rows); - pthread_t *pThreadList = (pthread_t *) calloc(sizeof(pthread_t), (size_t)threadNum); + TdThread *pThreadList = (TdThread *) calloc(sizeof(TdThread), (size_t)threadNum); ThreadInfo* threadInfo = (ThreadInfo *) calloc(sizeof(ThreadInfo), (size_t)threadNum); ThreadInfo* tInfo = threadInfo; @@ -5094,16 +5093,16 @@ int main(int argc, char *argv[]) tInfo->taos = taos; tInfo->idx = i; if (0 == i) { - //pthread_create(&(pThreadList[0]), NULL, runCase, (void *)tInfo); - pthread_create(&(pThreadList[0]), NULL, SpecifyColumnBatchCase, (void *)tInfo); + //taosThreadCreate(&(pThreadList[0]), NULL, runCase, (void *)tInfo); + taosThreadCreate(&(pThreadList[0]), NULL, SpecifyColumnBatchCase, (void *)tInfo); } else if (1 == i){ - pthread_create(&(pThreadList[0]), NULL, runCase_long, (void *)tInfo); + taosThreadCreate(&(pThreadList[0]), NULL, runCase_long, (void *)tInfo); } tInfo++; } for (int i = 0; i < threadNum; i++) { - pthread_join(pThreadList[i], NULL); + taosThreadJoin(pThreadList[i], NULL); } free(pThreadList); diff --git a/tests/script/api/stmtTest.c b/tests/script/api/stmtTest.c index c69de46f84..46f1c7c8f8 100644 --- a/tests/script/api/stmtTest.c +++ b/tests/script/api/stmtTest.c @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/tests/script/api/stmt_function.c b/tests/script/api/stmt_function.c index 4521ad4fa3..a5427844d7 100644 --- a/tests/script/api/stmt_function.c +++ b/tests/script/api/stmt_function.c @@ -1,5 +1,4 @@ #include -#include #include #include #include diff --git a/tests/script/http/httpTest.c b/tests/script/http/httpTest.c index 36ce6b95ba..0fe4265a9d 100644 --- a/tests/script/http/httpTest.c +++ b/tests/script/http/httpTest.c @@ -5,12 +5,11 @@ #include #include #include -#include #define MAXLINE 1024 typedef struct { - pthread_t pid; + TdThread pid; int threadId; int rows; int tables; @@ -107,16 +106,16 @@ void multiThread() { ThreadObj *threads = calloc((size_t)numOfThreads, sizeof(ThreadObj)); for (int i = 0; i < numOfThreads; i++) { ThreadObj *pthread = threads + i; - pthread_attr_t thattr; + TdThreadAttr thattr; pthread->threadId = i + 1; pthread->rows = numOfRows; pthread->tables = numOfTables; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - pthread_create(&pthread->pid, &thattr, (void *(*)(void *))execute, pthread); + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); + taosThreadCreate(&pthread->pid, &thattr, (void *(*)(void *))execute, pthread); } for (int i = 0; i < numOfThreads; i++) { - pthread_join(threads[i].pid, NULL); + taosThreadJoin(threads[i].pid, NULL); } free(threads); } diff --git a/tests/script/http/httpTestSqlUtc.c b/tests/script/http/httpTestSqlUtc.c index 643c884a1a..c6c5829b95 100644 --- a/tests/script/http/httpTestSqlUtc.c +++ b/tests/script/http/httpTestSqlUtc.c @@ -5,12 +5,11 @@ #include #include #include -#include #define MAXLINE 1024 typedef struct { - pthread_t pid; + TdThread pid; int threadId; int rows; int tables; @@ -107,16 +106,16 @@ void multiThread() { ThreadObj *threads = calloc((size_t)numOfThreads, sizeof(ThreadObj)); for (int i = 0; i < numOfThreads; i++) { ThreadObj *pthread = threads + i; - pthread_attr_t thattr; + TdThreadAttr thattr; pthread->threadId = i + 1; pthread->rows = numOfRows; pthread->tables = numOfTables; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - pthread_create(&pthread->pid, &thattr, (void *(*)(void *))execute, pthread); + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); + taosThreadCreate(&pthread->pid, &thattr, (void *(*)(void *))execute, pthread); } for (int i = 0; i < numOfThreads; i++) { - pthread_join(threads[i].pid, NULL); + taosThreadJoin(threads[i].pid, NULL); } free(threads); } diff --git a/tests/script/http/httpTestSqlt.c b/tests/script/http/httpTestSqlt.c index 2eaaee0f99..400428f471 100644 --- a/tests/script/http/httpTestSqlt.c +++ b/tests/script/http/httpTestSqlt.c @@ -5,12 +5,11 @@ #include #include #include -#include #define MAXLINE 1024 typedef struct { - pthread_t pid; + TdThread pid; int threadId; int rows; int tables; @@ -107,16 +106,16 @@ void multiThread() { ThreadObj *threads = calloc((size_t)numOfThreads, sizeof(ThreadObj)); for (int i = 0; i < numOfThreads; i++) { ThreadObj *pthread = threads + i; - pthread_attr_t thattr; + TdThreadAttr thattr; pthread->threadId = i + 1; pthread->rows = numOfRows; pthread->tables = numOfTables; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - pthread_create(&pthread->pid, &thattr, (void *(*)(void *))execute, pthread); + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); + taosThreadCreate(&pthread->pid, &thattr, (void *(*)(void *))execute, pthread); } for (int i = 0; i < numOfThreads; i++) { - pthread_join(threads[i].pid, NULL); + taosThreadJoin(threads[i].pid, NULL); } free(threads); } diff --git a/tests/test/c/create_table.c b/tests/test/c/create_table.c index 57fbd4c9c1..5e539780f4 100644 --- a/tests/test/c/create_table.c +++ b/tests/test/c/create_table.c @@ -49,7 +49,7 @@ typedef struct { int64_t startMs; int64_t maxDelay; int64_t minDelay; - pthread_t thread; + TdThread thread; } SThreadInfo; // void parseArgument(int32_t argc, char *argv[]); @@ -400,9 +400,9 @@ int32_t main(int32_t argc, char *argv[]) { pPrint("%d threads are spawned to create %" PRId64 " tables, offset is %" PRId64 " ", numOfThreads, numOfTables, startOffset); - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + TdThreadAttr thattr; + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); SThreadInfo *pInfo = (SThreadInfo *)calloc(numOfThreads, sizeof(SThreadInfo)); // int64_t numOfTablesPerThread = numOfTables / numOfThreads; @@ -430,12 +430,12 @@ int32_t main(int32_t argc, char *argv[]) { pInfo[i].minDelay = INT64_MAX; strcpy(pInfo[i].dbName, dbName); strcpy(pInfo[i].stbName, stbName); - pthread_create(&(pInfo[i].thread), &thattr, threadFunc, (void *)(pInfo + i)); + taosThreadCreate(&(pInfo[i].thread), &thattr, threadFunc, (void *)(pInfo + i)); } taosMsleep(300); for (int32_t i = 0; i < numOfThreads; i++) { - pthread_join(pInfo[i].thread, NULL); + taosThreadJoin(pInfo[i].thread, NULL); } int64_t maxDelay = 0; @@ -465,6 +465,6 @@ int32_t main(int32_t argc, char *argv[]) { numOfThreads, NC); } - pthread_attr_destroy(&thattr); + taosThreadAttrDestroy(&thattr); free(pInfo); } diff --git a/tests/tsim/inc/simInt.h b/tests/tsim/inc/simInt.h index 24619a4ad5..1e2190e308 100644 --- a/tests/tsim/inc/simInt.h +++ b/tests/tsim/inc/simInt.h @@ -143,7 +143,7 @@ typedef struct _script_t { char *optionBuffer; SCmdLine *lines; // command list SVariable variables[MAX_VAR_LEN]; - pthread_t bgPid; + TdThread bgPid; char auth[128]; struct _script_t *bgScripts[MAX_BACKGROUND_SCRIPT_NUM]; } SScript; diff --git a/tests/tsim/src/simExe.c b/tests/tsim/src/simExe.c index 1857e21753..855705e904 100644 --- a/tests/tsim/src/simExe.c +++ b/tests/tsim/src/simExe.c @@ -293,7 +293,7 @@ bool simExecuteRunBackCmd(SScript *script, char *option) { script->bgScripts[script->bgScriptLen++] = newScript; simInfo("script:%s, start to execute in background,", newScript->fileName); - if (pthread_create(&newScript->bgPid, NULL, simExecuteScript, (void *)newScript) != 0) { + if (taosThreadCreate(&newScript->bgPid, NULL, simExecuteScript, (void *)newScript) != 0) { sprintf(script->error, "lineNum:%d. create background thread failed", script->lines[script->linePos].lineNum); return false; } else { diff --git a/tests/tsim/src/simSystem.c b/tests/tsim/src/simSystem.c index bfbfe2b0df..eddcd3afc2 100644 --- a/tests/tsim/src/simSystem.c +++ b/tests/tsim/src/simSystem.c @@ -55,7 +55,7 @@ void simFreeScript(SScript *script) { simDebug("script:%s, is background script, set stop flag", bgScript->fileName); bgScript->killed = true; if (taosCheckPthreadValid(bgScript->bgPid)) { - pthread_join(bgScript->bgPid, NULL); + taosThreadJoin(bgScript->bgPid, NULL); } simDebug("script:%s, background thread joined", bgScript->fileName); diff --git a/tools/shell/src/backup/shellCheck.c b/tools/shell/src/backup/shellCheck.c index 33d25b6746..919cb0d846 100644 --- a/tools/shell/src/backup/shellCheck.c +++ b/tools/shell/src/backup/shellCheck.c @@ -31,7 +31,7 @@ static int32_t checkedNum = 0; static int32_t errorNum = 0; typedef struct { - pthread_t threadID; + TdThread threadID; int threadIndex; int totalThreads; void * taos; @@ -152,7 +152,7 @@ static void *shellCheckThreadFp(void *arg) { } static void shellRunCheckThreads(TAOS *con, SShellArguments *_args) { - pthread_attr_t thattr; + TdThreadAttr thattr; ShellThreadObj *threadObj = (ShellThreadObj *)calloc(_args->threadNum, sizeof(ShellThreadObj)); for (int t = 0; t < _args->threadNum; ++t) { ShellThreadObj *pThread = threadObj + t; @@ -161,17 +161,17 @@ static void shellRunCheckThreads(TAOS *con, SShellArguments *_args) { pThread->taos = con; pThread->db = _args->database; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&(pThread->threadID), &thattr, shellCheckThreadFp, (void *)pThread) != 0) { + if (taosThreadCreate(&(pThread->threadID), &thattr, shellCheckThreadFp, (void *)pThread) != 0) { fprintf(stderr, "ERROR: thread:%d failed to start\n", pThread->threadIndex); exit(0); } } for (int t = 0; t < _args->threadNum; ++t) { - pthread_join(threadObj[t].threadID, NULL); + taosThreadJoin(threadObj[t].threadID, NULL); } for (int t = 0; t < _args->threadNum; ++t) { diff --git a/tools/shell/src/backup/shellDarwin.c b/tools/shell/src/backup/shellDarwin.c index d7a976d52c..69c7a7bc4e 100644 --- a/tools/shell/src/backup/shellDarwin.c +++ b/tools/shell/src/backup/shellDarwin.c @@ -365,7 +365,7 @@ void *shellLoopQuery(void *arg) { setThreadName("shellLoopQuery"); - pthread_cleanup_push(cleanup_handler, NULL); + taosThreadCleanupPush(cleanup_handler, NULL); char *command = malloc(MAX_COMMAND_SIZE); if (command == NULL){ @@ -389,7 +389,7 @@ void *shellLoopQuery(void *arg) { tfree(command); exitShell(); - pthread_cleanup_pop(1); + taosThreadCleanupPop(1); return NULL; } diff --git a/tools/shell/src/backup/shellImport.c b/tools/shell/src/backup/shellImport.c index b42f77e87e..398bcf2280 100644 --- a/tools/shell/src/backup/shellImport.c +++ b/tools/shell/src/backup/shellImport.c @@ -28,7 +28,7 @@ static int32_t shellSQLFileNum = 0; static char shellTablesSQLFile[TSDB_FILENAME_LEN] = {0}; typedef struct { - pthread_t threadID; + TdThread threadID; int threadIndex; int totalThreads; void *taos; @@ -232,7 +232,7 @@ void* shellImportThreadFp(void *arg) static void shellRunImportThreads(SShellArguments* _args) { - pthread_attr_t thattr; + TdThreadAttr thattr; ShellThreadObj *threadObj = (ShellThreadObj *)calloc(_args->threadNum, sizeof(ShellThreadObj)); for (int t = 0; t < _args->threadNum; ++t) { ShellThreadObj *pThread = threadObj + t; @@ -244,17 +244,17 @@ static void shellRunImportThreads(SShellArguments* _args) exit(0); } - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); + taosThreadAttrInit(&thattr); + taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&(pThread->threadID), &thattr, shellImportThreadFp, (void*)pThread) != 0) { + if (taosThreadCreate(&(pThread->threadID), &thattr, shellImportThreadFp, (void*)pThread) != 0) { fprintf(stderr, "ERROR: thread:%d failed to start\n", pThread->threadIndex); exit(0); } } for (int t = 0; t < _args->threadNum; ++t) { - pthread_join(threadObj[t].threadID, NULL); + taosThreadJoin(threadObj[t].threadID, NULL); } for (int t = 0; t < _args->threadNum; ++t) { diff --git a/tools/shell/src/backup/tnettest.c b/tools/shell/src/backup/tnettest.c index 772d92d8c6..ee32bfb6be 100644 --- a/tools/shell/src/backup/tnettest.c +++ b/tools/shell/src/backup/tnettest.c @@ -519,7 +519,7 @@ static void taosNetTestServer(char *host, int32_t startPort, int32_t pkgLen) { int32_t num = endPort - startPort + 1; if (num < 0) num = 1; - pthread_t *pids = malloc(2 * num * sizeof(pthread_t)); + TdThread *pids = malloc(2 * num * sizeof(TdThread)); STestInfo *tinfos = malloc(num * sizeof(STestInfo)); STestInfo *uinfos = malloc(num * sizeof(STestInfo)); @@ -528,7 +528,7 @@ static void taosNetTestServer(char *host, int32_t startPort, int32_t pkgLen) { tcpInfo->port = port + i; tcpInfo->pktLen = pkgLen; - if (pthread_create(pids + i, NULL, taosNetBindTcpPort, tcpInfo) != 0) { + if (taosThreadCreate(pids + i, NULL, taosNetBindTcpPort, tcpInfo) != 0) { uInfo("failed to create TCP test thread, %s:%d", tcpInfo->hostFqdn, tcpInfo->port); exit(-1); } @@ -536,15 +536,15 @@ static void taosNetTestServer(char *host, int32_t startPort, int32_t pkgLen) { STestInfo *udpInfo = uinfos + i; udpInfo->port = port + i; tcpInfo->pktLen = pkgLen; - if (pthread_create(pids + num + i, NULL, taosNetBindUdpPort, udpInfo) != 0) { + if (taosThreadCreate(pids + num + i, NULL, taosNetBindUdpPort, udpInfo) != 0) { uInfo("failed to create UDP test thread, %s:%d", tcpInfo->hostFqdn, tcpInfo->port); exit(-1); } } for (int32_t i = 0; i < num; i++) { - pthread_join(pids[i], NULL); - pthread_join(pids[(num + i)], NULL); + taosThreadJoin(pids[i], NULL); + taosThreadJoin(pids[(num + i)], NULL); } } diff --git a/tools/shell/src/shellLinux.c b/tools/shell/src/shellLinux.c index 6da05f28df..de5db0b288 100644 --- a/tools/shell/src/shellLinux.c +++ b/tools/shell/src/shellLinux.c @@ -395,7 +395,7 @@ void *shellLoopQuery(void *arg) { setThreadName("shellLoopQuery"); - pthread_cleanup_push(cleanup_handler, NULL); + taosThreadCleanupPush(cleanup_handler, NULL); char *command = malloc(MAX_COMMAND_SIZE); if (command == NULL){ @@ -419,7 +419,7 @@ void *shellLoopQuery(void *arg) { tfree(command); exitShell(); - pthread_cleanup_pop(1); + taosThreadCleanupPop(1); return NULL; } diff --git a/tools/shell/src/shellMain.c b/tools/shell/src/shellMain.c index 2832855517..574e3fa8b8 100644 --- a/tools/shell/src/shellMain.c +++ b/tools/shell/src/shellMain.c @@ -17,7 +17,7 @@ #include "shell.h" #include "tglobal.h" -pthread_t pid; +TdThread pid; static tsem_t cancelSem; void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { @@ -140,8 +140,8 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } - pthread_t spid; - pthread_create(&spid, NULL, cancelHandler, NULL); + TdThread spid; + taosThreadCreate(&spid, NULL, cancelHandler, NULL); /* Interrupt handler. */ taosSetSignal(SIGTERM, shellQueryInterruptHandler); @@ -154,7 +154,7 @@ int main(int argc, char *argv[]) { /* Loop to query the input. */ while (1) { - pthread_create(&pid, NULL, shellLoopQuery, con); - pthread_join(pid, NULL); + taosThreadCreate(&pid, NULL, shellLoopQuery, con); + taosThreadJoin(pid, NULL); } }