aboutsummaryrefslogtreecommitdiff
path: root/src/timeout.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/timeout.c')
-rw-r--r--src/timeout.c73
1 files changed, 33 insertions, 40 deletions
diff --git a/src/timeout.c b/src/timeout.c
index dcc2105..74ba968 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -46,8 +46,7 @@ static luaL_reg func[] = {
46/*-------------------------------------------------------------------------*\ 46/*-------------------------------------------------------------------------*\
47* Initialize structure 47* Initialize structure
48\*-------------------------------------------------------------------------*/ 48\*-------------------------------------------------------------------------*/
49void tm_init(p_tm tm, int block, int total) 49void tm_init(p_tm tm, double block, double total) {
50{
51 tm->block = block; 50 tm->block = block;
52 tm->total = total; 51 tm->total = total;
53} 52}
@@ -60,18 +59,17 @@ void tm_init(p_tm tm, int block, int total)
60* Returns 59* Returns
61* the number of ms left or -1 if there is no time limit 60* the number of ms left or -1 if there is no time limit
62\*-------------------------------------------------------------------------*/ 61\*-------------------------------------------------------------------------*/
63int tm_get(p_tm tm) 62double tm_get(p_tm tm) {
64{ 63 if (tm->block < 0.0 && tm->total < 0.0) {
65 if (tm->block < 0 && tm->total < 0) {
66 return -1; 64 return -1;
67 } else if (tm->block < 0) { 65 } else if (tm->block < 0.0) {
68 int t = tm->total - tm_gettime() + tm->start; 66 double t = tm->total - tm_gettime() + tm->start;
69 return MAX(t, 0); 67 return MAX(t, 0.0);
70 } else if (tm->total < 0) { 68 } else if (tm->total < 0.0) {
71 return tm->block; 69 return tm->block;
72 } else { 70 } else {
73 int t = tm->total - tm_gettime() + tm->start; 71 double t = tm->total - tm_gettime() + tm->start;
74 return MIN(tm->block, MAX(t, 0)); 72 return MIN(tm->block, MAX(t, 0.0));
75 } 73 }
76} 74}
77 75
@@ -82,8 +80,7 @@ int tm_get(p_tm tm)
82* Returns 80* Returns
83* start field of structure 81* start field of structure
84\*-------------------------------------------------------------------------*/ 82\*-------------------------------------------------------------------------*/
85int tm_getstart(p_tm tm) 83double tm_getstart(p_tm tm) {
86{
87 return tm->start; 84 return tm->start;
88} 85}
89 86
@@ -95,19 +92,18 @@ int tm_getstart(p_tm tm)
95* Returns 92* Returns
96* the number of ms left or -1 if there is no time limit 93* the number of ms left or -1 if there is no time limit
97\*-------------------------------------------------------------------------*/ 94\*-------------------------------------------------------------------------*/
98int tm_getretry(p_tm tm) 95double tm_getretry(p_tm tm) {
99{ 96 if (tm->block < 0.0 && tm->total < 0.0) {
100 if (tm->block < 0 && tm->total < 0) {
101 return -1; 97 return -1;
102 } else if (tm->block < 0) { 98 } else if (tm->block < 0.0) {
103 int t = tm->total - tm_gettime() + tm->start; 99 double t = tm->total - tm_gettime() + tm->start;
104 return MAX(t, 0); 100 return MAX(t, 0.0);
105 } else if (tm->total < 0) { 101 } else if (tm->total < 0.0) {
106 int t = tm->block - tm_gettime() + tm->start; 102 double t = tm->block - tm_gettime() + tm->start;
107 return MAX(t, 0); 103 return MAX(t, 0.0);
108 } else { 104 } else {
109 int t = tm->total - tm_gettime() + tm->start; 105 double t = tm->total - tm_gettime() + tm->start;
110 return MIN(tm->block, MAX(t, 0)); 106 return MIN(tm->block, MAX(t, 0.0));
111 } 107 }
112} 108}
113 109
@@ -116,8 +112,7 @@ int tm_getretry(p_tm tm)
116* Input 112* Input
117* tm: timeout control structure 113* tm: timeout control structure
118\*-------------------------------------------------------------------------*/ 114\*-------------------------------------------------------------------------*/
119p_tm tm_markstart(p_tm tm) 115p_tm tm_markstart(p_tm tm) {
120{
121 tm->start = tm_gettime(); 116 tm->start = tm_gettime();
122 return tm; 117 return tm;
123} 118}
@@ -128,24 +123,23 @@ p_tm tm_markstart(p_tm tm)
128* time in ms. 123* time in ms.
129\*-------------------------------------------------------------------------*/ 124\*-------------------------------------------------------------------------*/
130#ifdef _WIN32 125#ifdef _WIN32
131int tm_gettime(void) 126double tm_gettime(void) {
132{ 127 FILETIME ft;
133 return GetTickCount(); 128 GetSystemTimeAsFileTime(&ft);
129 return ft.dwLowDateTime/1.0e7 + ft.dwHighDateTime*(4294967296.0/1.0e7);
134} 130}
135#else 131#else
136int tm_gettime(void) 132double tm_gettime(void) {
137{
138 struct timeval v; 133 struct timeval v;
139 gettimeofday(&v, (struct timezone *) NULL); 134 gettimeofday(&v, (struct timezone *) NULL);
140 return v.tv_sec * 1000 + v.tv_usec/1000; 135 return v.tv_sec + v.tv_usec/1.0e6;
141} 136}
142#endif 137#endif
143 138
144/*-------------------------------------------------------------------------*\ 139/*-------------------------------------------------------------------------*\
145* Initializes module 140* Initializes module
146\*-------------------------------------------------------------------------*/ 141\*-------------------------------------------------------------------------*/
147int tm_open(lua_State *L) 142int tm_open(lua_State *L) {
148{
149 luaL_openlib(L, NULL, func, 0); 143 luaL_openlib(L, NULL, func, 0);
150 return 0; 144 return 0;
151} 145}
@@ -156,16 +150,15 @@ int tm_open(lua_State *L)
156* time: time out value in seconds 150* time: time out value in seconds
157* mode: "b" for block timeout, "t" for total timeout. (default: b) 151* mode: "b" for block timeout, "t" for total timeout. (default: b)
158\*-------------------------------------------------------------------------*/ 152\*-------------------------------------------------------------------------*/
159int tm_meth_settimeout(lua_State *L, p_tm tm) 153int tm_meth_settimeout(lua_State *L, p_tm tm) {
160{ 154 double t = luaL_optnumber(L, 2, -1);
161 int ms = lua_isnil(L, 2) ? -1 : (int) (luaL_checknumber(L, 2)*1000.0);
162 const char *mode = luaL_optstring(L, 3, "b"); 155 const char *mode = luaL_optstring(L, 3, "b");
163 switch (*mode) { 156 switch (*mode) {
164 case 'b': 157 case 'b':
165 tm->block = ms; 158 tm->block = t;
166 break; 159 break;
167 case 'r': case 't': 160 case 'r': case 't':
168 tm->total = ms; 161 tm->total = t;
169 break; 162 break;
170 default: 163 default:
171 luaL_argcheck(L, 0, 3, "invalid timeout mode"); 164 luaL_argcheck(L, 0, 3, "invalid timeout mode");
@@ -183,7 +176,7 @@ int tm_meth_settimeout(lua_State *L, p_tm tm)
183\*-------------------------------------------------------------------------*/ 176\*-------------------------------------------------------------------------*/
184static int tm_lua_gettime(lua_State *L) 177static int tm_lua_gettime(lua_State *L)
185{ 178{
186 lua_pushnumber(L, tm_gettime()/1000.0); 179 lua_pushnumber(L, tm_gettime());
187 return 1; 180 return 1;
188} 181}
189 182