未验证 提交 0509aedd 编写于 作者: wafwerar's avatar wafwerar 提交者: GitHub

Merge pull request #10704 from taosdata/fix/ZhiqiangWang/TD-13771-redefine-timer-api

[TD-13771]<fix>: redefine timer api.
...@@ -20,6 +20,15 @@ ...@@ -20,6 +20,15 @@
extern "C" { extern "C" {
#endif #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 timer_create TIMER_CREATE_FUNC_TAOS_FORBID
#define timer_settime TIMER_SETTIME_FUNC_TAOS_FORBID
#define timer_delete TIMER_DELETE_FUNC_TAOS_FORBID
#define timeSetEvent TIMESETEVENT_SETTIME_FUNC_TAOS_FORBID
#define timeKillEvent TIMEKILLEVENT_SETTIME_FUNC_TAOS_FORBID
#endif
#define MSECONDS_PER_TICK 5 #define MSECONDS_PER_TICK 5
int32_t taosInitTimer(void (*callback)(int32_t), int32_t ms); int32_t taosInitTimer(void (*callback)(int32_t), int32_t ms);
......
...@@ -13,15 +13,11 @@ ...@@ -13,15 +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 ALLOW_FORBID_FUNC
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "os.h" #include "os.h"
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
/*
* windows implementation
*/
#include <Mmsystem.h> #include <Mmsystem.h>
#include <Windows.h> #include <Windows.h>
#include <stdint.h> #include <stdint.h>
...@@ -39,24 +35,9 @@ void WINAPI taosWinOnTimer(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR ...@@ -39,24 +35,9 @@ void WINAPI taosWinOnTimer(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR
} }
static MMRESULT timerId; static MMRESULT timerId;
int taosInitTimer(win_timer_f callback, int ms) {
DWORD_PTR param = *((int64_t *)&callback);
timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC);
if (timerId == 0) {
return -1;
}
return 0;
}
void taosUninitTimer() { timeKillEvent(timerId); }
#elif defined(_TD_DARWIN_64) #elif defined(_TD_DARWIN_64)
/*
* darwin implementation
*/
#include <sys/event.h> #include <sys/event.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
...@@ -88,53 +69,12 @@ static void* timer_routine(void* arg) { ...@@ -88,53 +69,12 @@ static void* timer_routine(void* arg) {
return NULL; return NULL;
} }
int taosInitTimer(void (*callback)(int), int ms) {
int r = 0;
timer_kq = -1;
timer_stop = 0;
timer_ms = ms;
timer_callback = callback;
timer_kq = kqueue();
if (timer_kq == -1) {
fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
// since no caller of this func checks the return value for the moment
abort();
}
r = pthread_create(&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
abort();
}
return 0;
}
void taosUninitTimer() {
int r = 0;
timer_stop = 1;
r = pthread_join(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
abort();
}
close(timer_kq);
timer_kq = -1;
}
void taos_block_sigalrm(void) { void taos_block_sigalrm(void) {
// we don't know if there's any specific API for SIGALRM to deliver to specific thread // we don't know if there's any specific API for SIGALRM to deliver to specific thread
// this implementation relies on kqueue rather than SIGALRM // this implementation relies on kqueue rather than SIGALRM
} }
#else #else
/*
* linux implementation
*/
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
...@@ -200,8 +140,39 @@ static void * taosProcessAlarmSignal(void *tharg) { ...@@ -200,8 +140,39 @@ static void * taosProcessAlarmSignal(void *tharg) {
return NULL; return NULL;
} }
#endif
int taosInitTimer(void (*callback)(int), int ms) { int taosInitTimer(void (*callback)(int), int ms) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
DWORD_PTR param = *((int64_t *)&callback);
timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC);
if (timerId == 0) {
return -1;
}
return 0;
#elif defined(_TD_DARWIN_64)
int r = 0;
timer_kq = -1;
timer_stop = 0;
timer_ms = ms;
timer_callback = callback;
timer_kq = kqueue();
if (timer_kq == -1) {
fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
// since no caller of this func checks the return value for the moment
abort();
}
r = pthread_create(&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
abort();
}
return 0;
#else
stopTimer = false; stopTimer = false;
pthread_attr_t tattr; pthread_attr_t tattr;
pthread_attr_init(&tattr); pthread_attr_init(&tattr);
...@@ -215,13 +186,29 @@ int taosInitTimer(void (*callback)(int), int ms) { ...@@ -215,13 +186,29 @@ int taosInitTimer(void (*callback)(int), int ms) {
} }
return 0; return 0;
#endif
} }
void taosUninitTimer() { void taosUninitTimer() {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
timeKillEvent(timerId);
#elif defined(_TD_DARWIN_64)
int r = 0;
timer_stop = 1;
r = pthread_join(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
abort();
}
close(timer_kq);
timer_kq = -1;
#else
stopTimer = true; stopTimer = true;
// printf("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread)); // printf("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread));
pthread_join(timerThread, NULL); pthread_join(timerThread, NULL);
#endif
} }
int64_t taosGetMonotonicMs() { int64_t taosGetMonotonicMs() {
...@@ -239,5 +226,3 @@ const char *taosMonotonicInit() { ...@@ -239,5 +226,3 @@ const char *taosMonotonicInit() {
return NULL; return NULL;
#endif #endif
} }
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册