diff --git a/include/util/ttimer.h b/include/util/ttimer.h index 01d70c7d02..f2ee825c4e 100644 --- a/include/util/ttimer.h +++ b/include/util/ttimer.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_TIMER_H -#define _TD_UTIL_TIMER_H +#ifndef _TD_UTIL_TIMER_H_ +#define _TD_UTIL_TIMER_H_ #include "os.h" @@ -25,23 +25,23 @@ extern "C" { typedef void *tmr_h; typedef void (*TAOS_TMR_CALLBACK)(void *, void *); -extern int taosTmrThreads; +extern int32_t taosTmrThreads; #define MSECONDS_PER_TICK 5 -void *taosTmrInit(int maxTmr, int resoultion, int longest, const char *label); +void *taosTmrInit(int32_t maxTmr, int32_t resoultion, int32_t longest, const char *label); -tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int mseconds, void *param, void *handle); +tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int32_t mseconds, void *param, void *handle); bool taosTmrStop(tmr_h tmrId); bool taosTmrStopA(tmr_h *timerId); -bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void *param, void *handle, tmr_h *pTmrId); +bool taosTmrReset(TAOS_TMR_CALLBACK fp, int32_t mseconds, void *param, void *handle, tmr_h *pTmrId); void taosTmrCleanUp(void *handle); -int32_t taosInitTimer(void (*callback)(int), int32_t ms); +int32_t taosInitTimer(void (*callback)(int32_t), int32_t ms); void taosUninitTimer(); @@ -49,4 +49,4 @@ void taosUninitTimer(); } #endif -#endif /*_TD_UTIL_TIMER_H*/ +#endif /*_TD_UTIL_TIMER_H_*/ diff --git a/source/util/src/ttimer.c b/source/util/src/ttimer.c index 2c04603269..abb42ef28d 100644 --- a/source/util/src/ttimer.c +++ b/source/util/src/ttimer.c @@ -13,12 +13,11 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "ttimer.h" -#include "os.h" #include "taoserror.h" #include "tlog.h" #include "tsched.h" -#include "tutil.h" #define tmrFatal(...) \ { \ @@ -57,9 +56,9 @@ } \ } -#define TIMER_STATE_WAITING 0 -#define TIMER_STATE_EXPIRED 1 -#define TIMER_STATE_STOPPED 2 +#define TIMER_STATE_WAITING 0 +#define TIMER_STATE_EXPIRED 1 +#define TIMER_STATE_STOPPED 2 #define TIMER_STATE_CANCELED 3 typedef union _tmr_ctrl_t { @@ -118,9 +117,9 @@ static pthread_mutex_t tmrCtrlMutex; static tmr_ctrl_t* tmrCtrls; static tmr_ctrl_t* unusedTmrCtrl = NULL; static void* tmrQhandle; -static int numOfTmrCtrl = 0; +static int32_t numOfTmrCtrl = 0; -int taosTmrThreads = 1; +int32_t taosTmrThreads = 1; static uintptr_t nextTimerId = 0; static time_wheel_t wheels[] = { @@ -148,7 +147,7 @@ static void timerDecRef(tmr_obj_t* timer) { static void lockTimerList(timer_list_t* list) { int64_t tid = taosGetSelfPthreadId(); - int i = 0; + int32_t i = 0; while (atomic_val_compare_exchange_64(&(list->lockedBy), 0, tid) != 0) { if (++i % 1000 == 0) { sched_yield(); @@ -322,7 +321,7 @@ static void addToExpired(tmr_obj_t* head) { } } -static uintptr_t doStartTimer(tmr_obj_t* timer, TAOS_TMR_CALLBACK fp, int mseconds, void* param, tmr_ctrl_t* ctrl) { +static uintptr_t doStartTimer(tmr_obj_t* timer, TAOS_TMR_CALLBACK fp, int32_t mseconds, void* param, tmr_ctrl_t* ctrl) { uintptr_t id = getNextTimerId(); timer->id = id; timer->state = TIMER_STATE_WAITING; @@ -346,7 +345,7 @@ static uintptr_t doStartTimer(tmr_obj_t* timer, TAOS_TMR_CALLBACK fp, int msecon return id; } -tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle) { +tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int32_t mseconds, void* param, void* handle) { tmr_ctrl_t* ctrl = (tmr_ctrl_t*)handle; if (ctrl == NULL || ctrl->label[0] == 0) { return NULL; @@ -361,10 +360,10 @@ tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle return (tmr_h)doStartTimer(timer, fp, mseconds, param, ctrl); } -static void taosTimerLoopFunc(int signo) { +static void taosTimerLoopFunc(int32_t signo) { int64_t now = taosGetMonotonicMs(); - for (int i = 0; i < tListLen(wheels); i++) { + for (int32_t i = 0; i < tListLen(wheels); i++) { // `expried` is a temporary expire list. // expired timers are first add to this list, then move // to expired queue as a batch to improve performance. @@ -471,7 +470,7 @@ bool taosTmrStopA(tmr_h* timerId) { return ret; } -bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle, tmr_h* pTmrId) { +bool taosTmrReset(TAOS_TMR_CALLBACK fp, int32_t mseconds, void* param, void* handle, tmr_h* pTmrId) { tmr_ctrl_t* ctrl = (tmr_ctrl_t*)handle; if (ctrl == NULL || ctrl->label[0] == 0) { return false; @@ -500,7 +499,7 @@ bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle, // wait until there's no other reference to this timer, // so that we can reuse this timer safely. - for (int i = 1; atomic_load_8(&timer->refCount) > 1; ++i) { + for (int32_t i = 1; atomic_load_8(&timer->refCount) > 1; ++i) { if (i % 1000 == 0) { sched_yield(); } @@ -532,7 +531,7 @@ static void taosTmrModuleInit(void) { pthread_mutex_init(&tmrCtrlMutex, NULL); int64_t now = taosGetMonotonicMs(); - for (int i = 0; i < tListLen(wheels); i++) { + for (int32_t i = 0; i < tListLen(wheels); i++) { time_wheel_t* wheel = wheels + i; if (pthread_mutex_init(&wheel->mutex, NULL) != 0) { tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno)); @@ -561,7 +560,7 @@ static void taosTmrModuleInit(void) { tmrDebug("timer module is initialized, number of threads: %d", taosTmrThreads); } -void* taosTmrInit(int maxNumOfTmrs, int resolution, int longest, const char* label) { +void* taosTmrInit(int32_t maxNumOfTmrs, int32_t resolution, int32_t longest, const char* label) { const char* ret = taosMonotonicInit(); tmrDebug("ttimer monotonic clock source:%s", ret); @@ -607,7 +606,7 @@ void taosTmrCleanUp(void* handle) { taosCleanUpScheduler(tmrQhandle); - for (int i = 0; i < tListLen(wheels); i++) { + for (int32_t i = 0; i < tListLen(wheels); i++) { time_wheel_t* wheel = wheels + i; pthread_mutex_destroy(&wheel->mutex); free(wheel->slots);