-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathtimer.cc
139 lines (122 loc) Β· 4.23 KB
/
timer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "swoole_api.h"
#include "swoole_timer.h"
using swoole::Timer;
using swoole::TimerCallback;
using swoole::TimerNode;
#ifdef __MACH__
Timer *sw_timer() {
return SwooleTG.timer;
}
#endif
bool swoole_timer_is_available() {
return SwooleTG.timer != nullptr;
}
TimerNode *swoole_timer_add(double timeout, bool persistent, const TimerCallback &callback, void *private_data) {
if (timeout < SW_TIMER_MIN_SEC) {
return swoole_timer_add(1L, persistent, callback, private_data);
}
return swoole_timer_add((long) (timeout * 1000), persistent, callback, private_data);
}
TimerNode *swoole_timer_add(long ms, bool persistent, const TimerCallback &callback, void *private_data) {
if (sw_unlikely(!swoole_timer_is_available())) {
SwooleTG.timer = new Timer();
if (sw_unlikely(!SwooleTG.timer->init())) {
delete SwooleTG.timer;
SwooleTG.timer = nullptr;
return nullptr;
}
}
return SwooleTG.timer->add(ms, persistent, private_data, callback);
}
bool swoole_timer_del(TimerNode *tnode) {
if (!swoole_timer_is_available()) {
swoole_warning("timer is not available");
return false;
}
return SwooleTG.timer->remove(tnode);
}
void swoole_timer_delay(TimerNode *tnode, long delay_ms) {
if (!swoole_timer_is_available()) {
swoole_warning("timer is not available");
return;
}
return SwooleTG.timer->delay(tnode, delay_ms);
}
long swoole_timer_after(long ms, const TimerCallback &callback, void *private_data) {
if (ms <= 0) {
swoole_warning("Timer must be greater than 0");
return SW_ERR;
}
TimerNode *tnode = swoole_timer_add(ms, false, callback, private_data);
if (tnode == nullptr) {
return SW_ERR;
} else {
return tnode->id;
}
}
long swoole_timer_tick(long ms, const TimerCallback &callback, void *private_data) {
if (ms <= 0) {
swoole_warning("Timer must be greater than 0");
return SW_ERR;
}
TimerNode *tnode = swoole_timer_add(ms, true, callback, private_data);
if (tnode == nullptr) {
return SW_ERR;
} else {
return tnode->id;
}
}
bool swoole_timer_exists(long timer_id) {
if (!swoole_timer_is_available()) {
swoole_warning("timer is not available");
return false;
}
TimerNode *tnode = SwooleTG.timer->get(timer_id);
return (tnode && !tnode->removed);
}
bool swoole_timer_clear(long timer_id) {
if (!swoole_timer_is_available()) {
swoole_warning("timer is not available");
return false;
}
return SwooleTG.timer->remove(SwooleTG.timer->get(timer_id));
}
TimerNode *swoole_timer_get(long timer_id) {
if (!swoole_timer_is_available()) {
swoole_warning("timer is not available");
return nullptr;
}
return SwooleTG.timer->get(timer_id);
}
void swoole_timer_free() {
if (!swoole_timer_is_available()) {
swoole_warning("timer is not available");
return;
}
delete SwooleTG.timer;
SwooleTG.timer = nullptr;
}
int swoole_timer_select() {
if (!swoole_timer_is_available()) {
return SW_ERR;
}
return SwooleTG.timer->select();
}
void swoole_timer_set_scheduler(const swoole::TimerScheduler &scheduler) {
SwooleTG.timer_scheduler = scheduler;
}