提交 13981723 编写于 作者: S Shengliang Guan

timer

上级 e14308c5
...@@ -13,8 +13,8 @@ ...@@ -13,8 +13,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _TD_UTIL_TIMER_H #ifndef _TD_UTIL_TIMER_H_
#define _TD_UTIL_TIMER_H #define _TD_UTIL_TIMER_H_
#include "os.h" #include "os.h"
...@@ -25,23 +25,23 @@ extern "C" { ...@@ -25,23 +25,23 @@ extern "C" {
typedef void *tmr_h; typedef void *tmr_h;
typedef void (*TAOS_TMR_CALLBACK)(void *, void *); typedef void (*TAOS_TMR_CALLBACK)(void *, void *);
extern int taosTmrThreads; extern int32_t taosTmrThreads;
#define MSECONDS_PER_TICK 5 #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 taosTmrStop(tmr_h tmrId);
bool taosTmrStopA(tmr_h *timerId); 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); 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(); void taosUninitTimer();
...@@ -49,4 +49,4 @@ void taosUninitTimer(); ...@@ -49,4 +49,4 @@ void taosUninitTimer();
} }
#endif #endif
#endif /*_TD_UTIL_TIMER_H*/ #endif /*_TD_UTIL_TIMER_H_*/
...@@ -13,12 +13,11 @@ ...@@ -13,12 +13,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define _DEFAULT_SOURCE
#include "ttimer.h" #include "ttimer.h"
#include "os.h"
#include "taoserror.h" #include "taoserror.h"
#include "tlog.h" #include "tlog.h"
#include "tsched.h" #include "tsched.h"
#include "tutil.h"
#define tmrFatal(...) \ #define tmrFatal(...) \
{ \ { \
...@@ -57,9 +56,9 @@ ...@@ -57,9 +56,9 @@
} \ } \
} }
#define TIMER_STATE_WAITING 0 #define TIMER_STATE_WAITING 0
#define TIMER_STATE_EXPIRED 1 #define TIMER_STATE_EXPIRED 1
#define TIMER_STATE_STOPPED 2 #define TIMER_STATE_STOPPED 2
#define TIMER_STATE_CANCELED 3 #define TIMER_STATE_CANCELED 3
typedef union _tmr_ctrl_t { typedef union _tmr_ctrl_t {
...@@ -118,9 +117,9 @@ static pthread_mutex_t tmrCtrlMutex; ...@@ -118,9 +117,9 @@ static pthread_mutex_t tmrCtrlMutex;
static tmr_ctrl_t* tmrCtrls; static tmr_ctrl_t* tmrCtrls;
static tmr_ctrl_t* unusedTmrCtrl = NULL; static tmr_ctrl_t* unusedTmrCtrl = NULL;
static void* tmrQhandle; 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 uintptr_t nextTimerId = 0;
static time_wheel_t wheels[] = { static time_wheel_t wheels[] = {
...@@ -148,7 +147,7 @@ static void timerDecRef(tmr_obj_t* timer) { ...@@ -148,7 +147,7 @@ static void timerDecRef(tmr_obj_t* timer) {
static void lockTimerList(timer_list_t* list) { static void lockTimerList(timer_list_t* list) {
int64_t tid = taosGetSelfPthreadId(); int64_t tid = taosGetSelfPthreadId();
int i = 0; int32_t i = 0;
while (atomic_val_compare_exchange_64(&(list->lockedBy), 0, tid) != 0) { while (atomic_val_compare_exchange_64(&(list->lockedBy), 0, tid) != 0) {
if (++i % 1000 == 0) { if (++i % 1000 == 0) {
sched_yield(); sched_yield();
...@@ -322,7 +321,7 @@ static void addToExpired(tmr_obj_t* head) { ...@@ -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(); uintptr_t id = getNextTimerId();
timer->id = id; timer->id = id;
timer->state = TIMER_STATE_WAITING; timer->state = TIMER_STATE_WAITING;
...@@ -346,7 +345,7 @@ static uintptr_t doStartTimer(tmr_obj_t* timer, TAOS_TMR_CALLBACK fp, int msecon ...@@ -346,7 +345,7 @@ static uintptr_t doStartTimer(tmr_obj_t* timer, TAOS_TMR_CALLBACK fp, int msecon
return id; 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; tmr_ctrl_t* ctrl = (tmr_ctrl_t*)handle;
if (ctrl == NULL || ctrl->label[0] == 0) { if (ctrl == NULL || ctrl->label[0] == 0) {
return NULL; return NULL;
...@@ -361,10 +360,10 @@ tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle ...@@ -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); return (tmr_h)doStartTimer(timer, fp, mseconds, param, ctrl);
} }
static void taosTimerLoopFunc(int signo) { static void taosTimerLoopFunc(int32_t signo) {
int64_t now = taosGetMonotonicMs(); 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. // `expried` is a temporary expire list.
// expired timers are first add to this list, then move // expired timers are first add to this list, then move
// to expired queue as a batch to improve performance. // to expired queue as a batch to improve performance.
...@@ -471,7 +470,7 @@ bool taosTmrStopA(tmr_h* timerId) { ...@@ -471,7 +470,7 @@ bool taosTmrStopA(tmr_h* timerId) {
return ret; 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; tmr_ctrl_t* ctrl = (tmr_ctrl_t*)handle;
if (ctrl == NULL || ctrl->label[0] == 0) { if (ctrl == NULL || ctrl->label[0] == 0) {
return false; return false;
...@@ -500,7 +499,7 @@ bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle, ...@@ -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, // wait until there's no other reference to this timer,
// so that we can reuse this timer safely. // 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) { if (i % 1000 == 0) {
sched_yield(); sched_yield();
} }
...@@ -532,7 +531,7 @@ static void taosTmrModuleInit(void) { ...@@ -532,7 +531,7 @@ static void taosTmrModuleInit(void) {
pthread_mutex_init(&tmrCtrlMutex, NULL); pthread_mutex_init(&tmrCtrlMutex, NULL);
int64_t now = taosGetMonotonicMs(); 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; time_wheel_t* wheel = wheels + i;
if (pthread_mutex_init(&wheel->mutex, NULL) != 0) { if (pthread_mutex_init(&wheel->mutex, NULL) != 0) {
tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno)); tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno));
...@@ -561,7 +560,7 @@ static void taosTmrModuleInit(void) { ...@@ -561,7 +560,7 @@ static void taosTmrModuleInit(void) {
tmrDebug("timer module is initialized, number of threads: %d", taosTmrThreads); 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(); const char* ret = taosMonotonicInit();
tmrDebug("ttimer monotonic clock source:%s", ret); tmrDebug("ttimer monotonic clock source:%s", ret);
...@@ -607,7 +606,7 @@ void taosTmrCleanUp(void* handle) { ...@@ -607,7 +606,7 @@ void taosTmrCleanUp(void* handle) {
taosCleanUpScheduler(tmrQhandle); 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; time_wheel_t* wheel = wheels + i;
pthread_mutex_destroy(&wheel->mutex); pthread_mutex_destroy(&wheel->mutex);
free(wheel->slots); free(wheel->slots);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册