Compare commits

..

No commits in common. "master" and "V0.4" have entirely different histories.
master ... V0.4

5 changed files with 404 additions and 468 deletions

12
.gitignore vendored
View File

@ -1,12 +0,0 @@
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
build

View File

@ -1,10 +1,10 @@
# MT_TimerMT译为Multiple或Multi # MT_TimerMT译为Multiple或Multi
#### 一、介绍 #### 介绍
一个Linux下的超级简洁的定时器*利用epoll机制和timerfd新特性实现的多重、多用、多个定时任务实现*。只需要使用TIMER_CREATE()接口创建一个定时器实体,即**可向其添加成千上万个定时任务,定时任务可达到纳秒级别的精度,且可在同一时间点添加不同的定时任务!**。 一个Linux下的超级简洁的定时器*利用epoll机制和timerfd新特性实现的多重、多用、多个定时实现*。只需要使用TIMER_CREATE()接口创建一个定时器实体,即**可向其添加成千上万个定时任务,定时任务可达到纳秒级别的精度,且可在同一时间点添加不同的定时任务!**。
#### 二、软件接口 #### 软件接口
整个定时器包含如下几类接口。 整个定时器包含如下几类接口。
@ -22,12 +22,12 @@ TIMER_DEINIT(name);
3. **添加和删除定时任务** 3. **添加和删除定时任务**
``` ```
TIMER_ADD(name, itimespec, repeat, cb, data, rb); TIMER_ADD(name, itimespec, repeat, cb, data);
TIMER_DEL(name, timerfd); TIMER_DEL(name, timerfd);
``` ```
TIMER_ADD()用于向定时器实例name中添加一个定时任务,其参数描述如下: TIMER_ADD()用于向定时器实例name中添加一个定时,其参数描述如下:
- ittimespec是定时任务的定时时间和循环时间,其结构体类型如下: - ittimespec是定时器的定时时间和循环事件,其结构体类型如下:
``` ```
struct timespec { struct timespec {
time_t tv_sec; // seconds time_t tv_sec; // seconds
@ -39,15 +39,15 @@ struct itimerspec {
}; };
``` ```
其中it_value即是超时时间(相对时间)若想定义周期定时任务则设置it_interval成员若不想定义周期定时任务则需设置it_interval成员都为0。因此第一次超时和后面周期定时任务是可以使用不同时间的。 其中it_value即是超时时间若想定义周期定时任务则设置it_interval成员若不想定义周期定时则需设置it_interval成员都为0。因此第一次超时和后面周期定时任务是可以使用不同时间的。
- repeat是周期定时任务的重复次数,若设置为**-1代表永远重复0代表一次都不执行**因此repeat应至少为1或者使用-1 - repeat是周期定时的重复次数,若设置为**-1代表永远重复0代表一次都不执行**因此repeat应至少为1或者使用-1
- cb为定时任务超时回调函数其类型为void (*timer_callback_t)(void *data) - cb为定时任务超时回调函数其类型为void (*timer_callback_t)(void *data)
- data为定时任务回调函数的参数为void *类型,用户可指定为自己定义的结构体; - data为定时任务回调函数的参数为void *类型,用户可指定为自己定义的结构体;
TIMER_ADD()添加定时任务成功返回新定时任务的文件描述符,失败返回 < 0TIMER_DEL() TIMER_ADD()添加定时任务成功返回新定时任务的文件描述符,失败返回 < 0TIMER_DEL()
4. **查询和清空定时任务** 4. **查询和清空定时**
``` ```
TIMER_COUNT(name); TIMER_COUNT(name);
TIMER_CLEAR(name); TIMER_CLEAR(name);
@ -55,9 +55,9 @@ TIMER_CLEAR(name);
TIMER_COUNT(name)用于查询定时器实例name中现存的定时任务个数TIMER_CLEAR(name)用于清空定时器实例name中的所有定时任务。 TIMER_COUNT(name)用于查询定时器实例name中现存的定时任务个数TIMER_CLEAR(name)用于清空定时器实例name中的所有定时任务。
#### 三、使用实例 #### 使用实例
下面是一个非常简单的使用示例:共创建了两个定时任务每个第一次超时都是3S后面每隔1S超时一次但第一个定时任务频次为8第二个定时任务频次为3当所有定时任务都超时后,输入回车即可退出: 下面是一个非常简单的使用示例:共创建了两个定时每个第一次超时都是3S后面每隔1S超时一次但第一个定时器频次为8第二个定时器频次为3当所有定时器都超时后,输入回车即可退出:
``` ```
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -105,11 +105,7 @@ int main(void)
} }
``` ```
#### 四、赞赏作者 #### 参与贡献
![image](https://images.gitee.com/uploads/images/2019/0510/144101_bc93efba_3026819.jpeg)
#### 五、参与贡献
1. Fork 本仓库 1. Fork 本仓库
2. 新建 Feat_xxx 分支 2. 新建 Feat_xxx 分支
@ -117,7 +113,7 @@ int main(void)
4. 新建 Pull Request 4. 新建 Pull Request
#### 六、码云特技 #### 码云特技
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) 2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com)

View File

@ -1,69 +1,69 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2019 @ konishi5202@163.com * Copyright (c) 2019 @ konishi5202@163.com
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in all * The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software. * copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "mt_timer.h" #include "mt_timer.h"
void timeout_handle(void *arg) void timeout_handle(void *arg)
{ {
printf("[%ld]:timeout1\n", time(NULL)); printf("[%ld]:timeout1\n", time(NULL));
} }
void timeout_handler(void *arg) void timeout_handler(void *arg)
{ {
printf("[%ld]:timeout2\n", time(NULL)); printf("[%ld]:timeout2\n", time(NULL));
} }
TIMER_CREATE(test); TIMER_CREATE(test);
int main(void) int main(void)
{ {
int timer; int timer;
struct itimerspec itimespec; struct itimerspec itimespec;
TIMER_INIT(test, 10); TIMER_INIT(test, 10);
itimespec.it_value.tv_sec = 3; itimespec.it_value.tv_sec = 3;
itimespec.it_value.tv_nsec = 0; itimespec.it_value.tv_nsec = 0;
itimespec.it_interval.tv_sec = 1; itimespec.it_interval.tv_sec = 1;
itimespec.it_interval.tv_nsec = 0; itimespec.it_interval.tv_nsec = 0;
timer = TIMER_ADD(test, &itimespec, 8, timeout_handle, NULL, NULL); timer = TIMER_ADD(test, &itimespec, 8, timeout_handle, NULL);
TIMER_ADD(test, &itimespec, 3, timeout_handler, NULL, NULL); TIMER_ADD(test, &itimespec, 3, timeout_handler, NULL);
printf("[%ld]:timer_add : %d\n", time(NULL), TIMER_COUNT(test)); printf("[%ld]:timer_add : %d\n", time(NULL), TIMER_COUNT(test));
sleep(4);//getchar(); sleep(4);//getchar();
TIMER_DEL(test, timer); TIMER_DEL(test, timer);
printf("[%ld]:timer_del : %d\n", time(NULL), TIMER_COUNT(test)); printf("[%ld]:timer_del : %d\n", time(NULL), TIMER_COUNT(test));
TIMER_CLEAR(test); TIMER_CLEAR(test);
printf("[%ld]:timer_clear : %d\n", time(NULL), TIMER_COUNT(test)); printf("[%ld]:timer_clear : %d\n", time(NULL), TIMER_COUNT(test));
getchar(); getchar();
TIMER_DEINIT(test); TIMER_DEINIT(test);
return 0; return 0;
} }

View File

@ -1,211 +1,172 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2019 @ konishi5202@163.com * Copyright (c) 2019 @ konishi5202@163.com
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in all * The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software. * copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h>
#include <sys/eventfd.h> #include "mt_timer.h"
#include "mt_timer.h" int timer_init(MT_TIMER_OBJECT *object, void *(*thread_handler)(void *arg), int max_num)
{
int timer_init(MT_TIMER_OBJECT *object, void *(*thread_handler)(void *arg), int max_num) object->timer_max = max_num;
{ object->timer_active_flag = true;
struct epoll_event event; object->timer_epoll_fd = epoll_create(max_num);
assert(NULL != object); if(object->timer_epoll_fd < 0)
if(true == object->timer_active_flag) {
return 0; MT_TIMER_PRINT_INL("MT-Timer error: epoll_create failed %s.\n", strerror(errno));
return -1;
object->timer_event_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); }
if(-1 == object->timer_event_fd)
{ if(pthread_create(&object->timer_thread_id, NULL, thread_handler, NULL) != 0)
MT_TIMER_PRINT_INL("MT-Timer error: eventfd() failed, %s!\n", strerror(errno)); {
return -1; MT_TIMER_PRINT_INL("MT-Timer error: pthread_create failed %s.\n", strerror(errno));
} return -1;
object->timer_max = max_num; }
object->timer_active_flag = true;
object->timer_epoll_fd = epoll_create(max_num); return 0;
if(object->timer_epoll_fd < 0) }
{
MT_TIMER_PRINT_INL("MT-Timer error: epoll_create failed %s.\n", strerror(errno)); void timer_deinit(MT_TIMER_OBJECT *object)
close(object->timer_event_fd); {
object->timer_event_fd = -1; struct itimerspec itimespec;
return -1;
} object->timer_active_flag = false;
event.data.ptr = NULL; itimespec.it_value.tv_sec = 0;
event.events = EPOLLIN | EPOLLET; itimespec.it_value.tv_nsec = 50;
if(epoll_ctl(object->timer_epoll_fd, EPOLL_CTL_ADD, object->timer_event_fd, &event) < 0) itimespec.it_interval.tv_sec = 0;
{ itimespec.it_interval.tv_nsec = 0;
fprintf(stderr, "epoll: epoll_ctl ADD failed, %s\n", strerror(errno));
close(object->timer_epoll_fd); timer_add(object, &itimespec, 0, NULL, NULL);
close(object->timer_event_fd); pthread_join(object->timer_thread_id, NULL);
object->timer_epoll_fd = -1; timer_clear(object);
object->timer_event_fd = -1; }
return -1;
} int timer_add(MT_TIMER_OBJECT *object, struct itimerspec *itimespec,
int repeat, timer_callback_t cb, void *data)
if(pthread_create(&object->timer_thread_id, NULL, thread_handler, NULL) != 0) {
{ struct epoll_event event;
MT_TIMER_PRINT_INL("MT-Timer error: pthread_create failed %s.\n", strerror(errno)); MT_TIMER_NODE *handler = NULL;
close(object->timer_epoll_fd);
close(object->timer_event_fd); handler = (MT_TIMER_NODE *)malloc(sizeof(MT_TIMER_NODE));
object->timer_epoll_fd = -1; if(NULL == handler)
object->timer_event_fd = -1; {
return -1; MT_TIMER_PRINT_INL("MT-Timer error: malloc failed %s.\n", strerror(errno));
} return -1;
}
return 0;
} handler->timer_cb = cb;
handler->timer_cnt = repeat;
void timer_deinit(MT_TIMER_OBJECT *object) handler->timer_data = data;
{ handler->timer_fd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC|TFD_NONBLOCK);
uint64_t quit = 0x51; if(handler->timer_fd < 0)
assert(NULL != object); {
if(false == object->timer_active_flag) MT_TIMER_PRINT_INL("MT-Timer error: timerfd_create failed %s.\n", strerror(errno));
return ; free(handler);
object->timer_active_flag = false; return -2;
usleep(100); }
write(object->timer_event_fd, &quit, sizeof(uint64_t)); /* wakeup thread */ if(timerfd_settime(handler->timer_fd, 0, itimespec, NULL) == -1)
pthread_join(object->timer_thread_id, NULL); {
timer_clear(object); MT_TIMER_PRINT_INL("MT-Timer error: timerfd_settime failed %s.\n", strerror(errno));
close(object->timer_epoll_fd); close(handler->timer_fd);
close(object->timer_event_fd); free(handler);
object->timer_epoll_fd = -1; return -3;
object->timer_event_fd = -1; }
}
event.events = EPOLLIN | EPOLLET;
int timer_add(MT_TIMER_OBJECT *object, struct itimerspec *itimespec, event.data.ptr = handler;
int repeat, timer_callback_t cb, void *data, timer_release_t rb) if(epoll_ctl(object->timer_epoll_fd, EPOLL_CTL_ADD, handler->timer_fd, &event) < 0)
{ {
uint64_t quit = 0x51; MT_TIMER_PRINT_INL("MT-Timer error: epoll_ctl ADD failed %s.\n", strerror(errno));
struct epoll_event event; close(handler->timer_fd);
MT_TIMER_NODE *handler = NULL; free(handler);
assert(NULL != object); return -4;
handler = (MT_TIMER_NODE *)malloc(sizeof(MT_TIMER_NODE)); }
if(NULL == handler)
{ pthread_rwlock_wrlock(&object->timer_rwlock);
MT_TIMER_PRINT_INL("MT-Timer error: malloc failed %s.\n", strerror(errno)); HASH_ADD_INT(object->timer_head, timer_fd, handler);
return -1; pthread_rwlock_unlock(&object->timer_rwlock);
}
return handler->timer_fd;
handler->timer_cb = cb; }
handler->release_cb = rb;
handler->timer_cnt = repeat; int timer_del(MT_TIMER_OBJECT *object, int timerfd)
handler->timer_data = data; {
handler->timer_fd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC|TFD_NONBLOCK); struct epoll_event event;
if(handler->timer_fd < 0) MT_TIMER_NODE *handler = NULL;
{
MT_TIMER_PRINT_INL("MT-Timer error: timerfd_create failed %s.\n", strerror(errno)); pthread_rwlock_rdlock(&object->timer_rwlock);
free(handler); HASH_FIND_INT(object->timer_head, &timerfd, handler);
return -2; pthread_rwlock_unlock(&object->timer_rwlock);
} if(NULL == handler)
if(timerfd_settime(handler->timer_fd, 0, itimespec, NULL) == -1) return 0;
{
MT_TIMER_PRINT_INL("MT-Timer error: timerfd_settime failed %s.\n", strerror(errno)); event.data.ptr = (void *)handler;
close(handler->timer_fd); event.events = EPOLLIN | EPOLLET;
free(handler); if(epoll_ctl(object->timer_epoll_fd, EPOLL_CTL_DEL, handler->timer_fd, &event) < 0)
return -3; {
} MT_TIMER_PRINT_INL("MT-Timer error: epoll_ctl DEL failed %s.\n", strerror(errno));
return -1;
event.events = EPOLLIN | EPOLLET; }
event.data.ptr = handler;
if(epoll_ctl(object->timer_epoll_fd, EPOLL_CTL_ADD, handler->timer_fd, &event) < 0) pthread_rwlock_wrlock(&object->timer_rwlock);
{ HASH_DEL(object->timer_head, handler);
MT_TIMER_PRINT_INL("MT-Timer error: epoll_ctl ADD failed %s.\n", strerror(errno)); pthread_rwlock_unlock(&object->timer_rwlock);
close(handler->timer_fd); close(handler->timer_fd);
free(handler); free(handler);
return -4;
} return 0;
write(object->timer_event_fd, &quit, sizeof(uint64_t)); /* wakeup thread */ }
pthread_rwlock_wrlock(&object->timer_rwlock);
HASH_ADD_INT(object->timer_head, timer_fd, handler); int timer_count(MT_TIMER_OBJECT *object)
pthread_rwlock_unlock(&object->timer_rwlock); {
return HASH_COUNT(object->timer_head);
return handler->timer_fd; }
}
int timer_clear(MT_TIMER_OBJECT *object)
int timer_del(MT_TIMER_OBJECT *object, int timerfd) {
{ struct epoll_event event;
struct epoll_event event; MT_TIMER_NODE *handler = NULL;
MT_TIMER_NODE *handler = NULL;
assert(NULL != object); event.events = EPOLLIN | EPOLLET;
pthread_rwlock_rdlock(&object->timer_rwlock); for(handler = object->timer_head; handler != NULL; handler = handler->hh.next)
HASH_FIND_INT(object->timer_head, &timerfd, handler); {
pthread_rwlock_unlock(&object->timer_rwlock); event.data.ptr = (void *)handler;
if(NULL == handler) if(epoll_ctl(object->timer_epoll_fd, EPOLL_CTL_DEL, handler->timer_fd, &event) < 0)
return 0; {
MT_TIMER_PRINT_INL("MT-Timer error: epoll_ctl CLEAR failed %s.\n", strerror(errno));
event.data.ptr = (void *)handler; return -1;
event.events = EPOLLIN | EPOLLET; }
if(epoll_ctl(object->timer_epoll_fd, EPOLL_CTL_DEL, handler->timer_fd, &event) < 0) pthread_rwlock_wrlock(&object->timer_rwlock);
{ HASH_DEL(object->timer_head, handler);
MT_TIMER_PRINT_INL("MT-Timer error: epoll_ctl DEL failed %s.\n", strerror(errno)); pthread_rwlock_unlock(&object->timer_rwlock);
return -1; close(handler->timer_fd);
} free(handler);
}
pthread_rwlock_wrlock(&object->timer_rwlock); return 0;
HASH_DEL(object->timer_head, handler); }
pthread_rwlock_unlock(&object->timer_rwlock);
close(handler->timer_fd);
if(handler->release_cb)
handler->release_cb(handler->timer_data);
free(handler);
return 0;
}
int timer_count(MT_TIMER_OBJECT *object)
{
assert(NULL != object);
return HASH_COUNT(object->timer_head);
}
int timer_clear(MT_TIMER_OBJECT *object)
{
struct epoll_event event;
MT_TIMER_NODE *handler = NULL;
assert(NULL != object);
event.events = EPOLLIN | EPOLLET;
for(handler = object->timer_head; handler != NULL; handler = handler->hh.next)
{
event.data.ptr = (void *)handler;
if(epoll_ctl(object->timer_epoll_fd, EPOLL_CTL_DEL, handler->timer_fd, &event) < 0)
{
MT_TIMER_PRINT_INL("MT-Timer error: epoll_ctl CLEAR failed %s.\n", strerror(errno));
return -1;
}
pthread_rwlock_wrlock(&object->timer_rwlock);
HASH_DEL(object->timer_head, handler);
pthread_rwlock_unlock(&object->timer_rwlock);
close(handler->timer_fd);
if(handler->release_cb)
handler->release_cb(handler->timer_data);
free(handler);
}
return 0;
}

View File

@ -1,181 +1,172 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2019 @ konishi5202@163.com * Copyright (c) 2019 @ konishi5202@163.com
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in all * The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software. * copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
#ifndef _MT_TIMER_H__ #ifndef _MT_TIMER_H__
#define _MT_TIMER_H__ #define _MT_TIMER_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <pthread.h> #include <pthread.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/timerfd.h> #include <sys/timerfd.h>
#include "uthash.h" #include "uthash.h"
#define DEBUG_ENABLE #define DEBUG_ENABLE
/************************** Debug Config Start **************************/ /************************** Debug Config Start **************************/
#ifdef DEBUG_ENABLE #ifdef DEBUG_ENABLE
#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#ifdef EFSM_PRINT #ifdef EFSM_PRINT
#define MT_TIMER_PRINT_INL(...) EFSM_PRINT(__VA_ARGS__) #define MT_TIMER_PRINT_INL(...) EFSM_PRINT(__VA_ARGS__)
#else #else
#define MT_TIMER_PRINT_INL(...) printf(__VA_ARGS__) #define MT_TIMER_PRINT_INL(...) printf(__VA_ARGS__)
#endif #endif
#else #else
#ifdef EFSM_PRINT #ifdef EFSM_PRINT
#define MT_TIMER_PRINT_INL(format, args...) EFSM_PRINT(format, ##args) #define MT_TIMER_PRINT_INL(format, args...) EFSM_PRINT(format, ##args)
#else #else
#define MT_TIMER_PRINT_INL(format, args...) printf(format, ##args) #define MT_TIMER_PRINT_INL(format, args...) printf(format, ##args)
#endif #endif
#endif #endif
#else #else
#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#define MT_TIMER_PRINT_INL(...) #define MT_TIMER_PRINT_INL(...)
#else #else
#define MT_TIMER_PRINT_INL(format, args...) #define MT_TIMER_PRINT_INL(format, args...)
#endif #endif
#endif #endif
/************************** Debug Config End **************************/ /************************** Debug Config End **************************/
#ifndef bool #ifndef bool
typedef enum{false, true} bool; typedef enum{false, true} bool;
#define bool bool #define bool bool
#endif #endif
typedef void (*timer_release_t)(void *data); typedef void (*timer_callback_t)(void *data);
typedef void (*timer_callback_t)(void *data);
typedef struct {
typedef struct { int timer_fd;
int timer_fd; int timer_cnt;
int timer_cnt; void *timer_data;
void *timer_data; UT_hash_handle hh;
UT_hash_handle hh; timer_callback_t timer_cb;
timer_callback_t timer_cb; }MT_TIMER_NODE; /* MT mean multiple */
timer_release_t release_cb;
}MT_TIMER_NODE; /* MT mean multiple */ typedef struct {
int timer_max;
typedef struct { int timer_epoll_fd;
int timer_max; bool timer_active_flag;
int timer_epoll_fd; MT_TIMER_NODE *timer_head;
int timer_event_fd; pthread_t timer_thread_id;
bool timer_active_flag; pthread_rwlock_t timer_rwlock;
MT_TIMER_NODE *timer_head; }MT_TIMER_OBJECT;
pthread_t timer_thread_id;
pthread_rwlock_t timer_rwlock; #define TIMER_THREAD_CREATE(name) \
}MT_TIMER_OBJECT; static void *mt_timer_thread_##name(void *arg) \
{ \
#define TIMER_THREAD_CREATE(name) \ int nfds, i; \
static void *mt_timer_thread_##name(void *arg) \ char buf[128]; \
{ \ struct epoll_event event; \
int nfds, i; \ MT_TIMER_NODE *timer_node = NULL; \
uint64_t u64; \ struct epoll_event events[mt_timer_##name.timer_max]; \
char buf[128]; \ /*pthread_detach(pthread_self());*/ \
struct epoll_event event; \ event.events = EPOLLIN | EPOLLET; \
MT_TIMER_NODE *timer_node = NULL; \ MT_TIMER_PRINT_INL("MT-Timer Info: %s thread is running.\n", #name); \
struct epoll_event events[mt_timer_##name.timer_max]; \ while(mt_timer_##name.timer_active_flag) \
/*pthread_detach(pthread_self());*/ \ { \
event.events = EPOLLIN | EPOLLET; \ nfds = epoll_wait(mt_timer_##name.timer_epoll_fd, events, mt_timer_##name.timer_max, -1); \
MT_TIMER_PRINT_INL("MT-Timer Info: %s thread is running.\n", #name); \ if(nfds <= 0) \
while(mt_timer_##name.timer_active_flag) \ continue; \
{ \ for(i = 0; i < nfds; i++) \
nfds = epoll_wait(mt_timer_##name.timer_epoll_fd, events, mt_timer_##name.timer_max, -1); \ { \
if(nfds <= 0) \ timer_node = (MT_TIMER_NODE *)events[i].data.ptr; \
continue; \ if(NULL == timer_node) \
for(i = 0; i < nfds; i++) \ continue; \
{ \ while(read(timer_node->timer_fd, buf, sizeof(buf)) > 0); \
timer_node = (MT_TIMER_NODE *)events[i].data.ptr; \ if(NULL == timer_node->timer_cb) \
if(NULL == timer_node) \ continue; \
{ \ if(-1 == timer_node->timer_cnt) \
read(mt_timer_##name.timer_event_fd, &u64, sizeof(uint64_t)); \ timer_node->timer_cb(timer_node->timer_data); \
continue; \ else \
} \ { \
while(read(timer_node->timer_fd, buf, sizeof(buf)) > 0); \ if(timer_node->timer_cnt) \
if(NULL == timer_node->timer_cb) \ { \
continue; \ timer_node->timer_cb(timer_node->timer_data); \
if(-1 == timer_node->timer_cnt) \ timer_node->timer_cnt--; \
timer_node->timer_cb(timer_node->timer_data); \ } \
else \ else \
{ \ { \
if(timer_node->timer_cnt) \ event.data.ptr = (void *)timer_node; \
{ \ epoll_ctl(mt_timer_##name.timer_epoll_fd, EPOLL_CTL_DEL, timer_node->timer_fd, &event); \
timer_node->timer_cb(timer_node->timer_data); \ pthread_rwlock_wrlock(&mt_timer_##name.timer_rwlock); \
timer_node->timer_cnt--; \ HASH_DEL(mt_timer_##name.timer_head, timer_node); \
} \ pthread_rwlock_unlock(&mt_timer_##name.timer_rwlock); \
else \ close(timer_node->timer_fd); \
{ \ free(timer_node); \
event.data.ptr = (void *)timer_node; \ } \
epoll_ctl(mt_timer_##name.timer_epoll_fd, EPOLL_CTL_DEL, timer_node->timer_fd, &event); \ } \
pthread_rwlock_wrlock(&mt_timer_##name.timer_rwlock); \ } \
HASH_DEL(mt_timer_##name.timer_head, timer_node); \ } \
pthread_rwlock_unlock(&mt_timer_##name.timer_rwlock); \ MT_TIMER_PRINT_INL("MT-Timer Info: %s thread is exit.\n", #name); \
close(timer_node->timer_fd); \ pthread_exit(NULL); \
if(timer_node->release_cb) \ }
timer_node->release_cb(timer_node->timer_data); \
free(timer_node); \ extern int timer_init(MT_TIMER_OBJECT *object, void *(*thread_handler)(void *arg), int max_num);
} \ extern void timer_deinit(MT_TIMER_OBJECT *object);
} \ extern int timer_add(MT_TIMER_OBJECT *object, struct itimerspec *itimespec,
} \ int repeat, timer_callback_t cb, void *data);
} \ extern int timer_del(MT_TIMER_OBJECT *object, int timerfd);
MT_TIMER_PRINT_INL("MT-Timer Info: %s thread is exit.\n", #name); \ extern int timer_count(MT_TIMER_OBJECT *object);
pthread_exit(NULL); \ extern int timer_clear(MT_TIMER_OBJECT *object);
}
/************************** API Interface **************************/
extern int timer_init(MT_TIMER_OBJECT *object, void *(*thread_handler)(void *arg), int max_num); #define TIMER_CREATE(name) \
extern void timer_deinit(MT_TIMER_OBJECT *object); MT_TIMER_OBJECT mt_timer_##name = {0, -1, false, NULL, 0, PTHREAD_RWLOCK_INITIALIZER}; \
extern int timer_add(MT_TIMER_OBJECT *object, struct itimerspec *itimespec, TIMER_THREAD_CREATE(name)
int repeat, timer_callback_t cb, void *data, timer_release_t rb); #define TIMER_DECLEAR(name) \
extern int timer_del(MT_TIMER_OBJECT *object, int timerfd); extern MT_TIMER_OBJECT mt_timer_##name
extern int timer_count(MT_TIMER_OBJECT *object); #define TIMER_INIT(name, max) \
extern int timer_clear(MT_TIMER_OBJECT *object); timer_init(&mt_timer_##name, mt_timer_thread_##name, max)
#define TIMER_DEINIT(name) \
/************************** API Interface **************************/ timer_deinit(&mt_timer_##name)
#define TIMER_CREATE(name) \ #define TIMER_ADD(name, itimespec, repeat, cb, data) \
MT_TIMER_OBJECT mt_timer_##name = {0, -1, -1, false, NULL, 0, PTHREAD_RWLOCK_INITIALIZER}; \ timer_add(&mt_timer_##name, itimespec, repeat, cb, data)
TIMER_THREAD_CREATE(name) #define TIMER_DEL(name, timerfd) \
#define TIMER_DECLEAR(name) \ timer_del(&mt_timer_##name, timerfd)
extern MT_TIMER_OBJECT mt_timer_##name #define TIMER_COUNT(name) timer_count(&mt_timer_##name)
#define TIMER_INIT(name, max) \ #define TIMER_CLEAR(name) timer_clear(&mt_timer_##name)
timer_init(&mt_timer_##name, mt_timer_thread_##name, max)
#define TIMER_DEINIT(name) \ #ifdef __cplusplus
timer_deinit(&mt_timer_##name) }
#define TIMER_ADD(name, itimespec, repeat, cb, data, rb) \ #endif
timer_add(&mt_timer_##name, itimespec, repeat, cb, data, rb)
#define TIMER_DEL(name, timerfd) \ #endif // _MT_TIMER_H__
timer_del(&mt_timer_##name, timerfd)
#define TIMER_COUNT(name) timer_count(&mt_timer_##name)
#define TIMER_CLEAR(name) timer_clear(&mt_timer_##name)
#ifdef __cplusplus
}
#endif
#endif // _MT_TIMER_H__