aboutsummaryrefslogtreecommitdiff
path: root/src/timeout.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2003-11-27 00:30:54 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2003-11-27 00:30:54 +0000
commit3febb302ad28fd25de51cbc686739469b92d8921 (patch)
tree0a8d9a1070a7c1f2566c22562693fb27963037d9 /src/timeout.c
parent9bc4e0648ab4a177293a94425594bdc54c9a84fa (diff)
downloadluasocket-3febb302ad28fd25de51cbc686739469b92d8921.tar.gz
luasocket-3febb302ad28fd25de51cbc686739469b92d8921.tar.bz2
luasocket-3febb302ad28fd25de51cbc686739469b92d8921.zip
Killed a few bugs found by Tomas.
Diffstat (limited to 'src/timeout.c')
-rw-r--r--src/timeout.c74
1 files changed, 48 insertions, 26 deletions
diff --git a/src/timeout.c b/src/timeout.c
index 38d1135..5d6de99 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -16,8 +16,8 @@
16#ifdef WIN32 16#ifdef WIN32
17#include <windows.h> 17#include <windows.h>
18#else 18#else
19#include <sys/time.h>
19#include <sys/times.h> 20#include <sys/times.h>
20#include <time.h>
21#include <unistd.h> 21#include <unistd.h>
22#endif 22#endif
23 23
@@ -46,40 +46,62 @@ void tm_init(p_tm tm, int block, int total)
46} 46}
47 47
48/*-------------------------------------------------------------------------*\ 48/*-------------------------------------------------------------------------*\
49* Set and get timeout limits 49* Determines how much time we have left for the next system call,
50* if the previous call was successful
51* Input
52* tm: timeout control structure
53* Returns
54* the number of ms left or -1 if there is no time limit
55\*-------------------------------------------------------------------------*/
56int tm_getsuccess(p_tm tm)
57{
58 if (tm->block < 0 && tm->total < 0) {
59 return -1;
60 } else if (tm->block < 0) {
61 int t = tm->total - tm_gettime() + tm->start;
62 return MAX(t, 0);
63 } else if (tm->total < 0) {
64 return tm->block;
65 } else {
66 int t = tm->total - tm_gettime() + tm->start;
67 return MIN(tm->block, MAX(t, 0));
68 }
69}
70
71/*-------------------------------------------------------------------------*\
72* Returns time since start of operation
73* Input
74* tm: timeout control structure
75* Returns
76* start field of structure
50\*-------------------------------------------------------------------------*/ 77\*-------------------------------------------------------------------------*/
51void tm_setblock(p_tm tm, int block)
52{ tm->block = block; }
53void tm_settotal(p_tm tm, int total)
54{ tm->total = total; }
55int tm_getblock(p_tm tm)
56{ return tm->block; }
57int tm_gettotal(p_tm tm)
58{ return tm->total; }
59int tm_getstart(p_tm tm) 78int tm_getstart(p_tm tm)
60{ return tm->start; } 79{
80 return tm->start;
81}
61 82
62/*-------------------------------------------------------------------------*\ 83/*-------------------------------------------------------------------------*\
63* Determines how much time we have left for the current operation 84* Determines how much time we have left for the next system call,
85* if the previous call was a failure
64* Input 86* Input
65* tm: timeout control structure 87* tm: timeout control structure
66* Returns 88* Returns
67* the number of ms left or -1 if there is no time limit 89* the number of ms left or -1 if there is no time limit
68\*-------------------------------------------------------------------------*/ 90\*-------------------------------------------------------------------------*/
69int tm_get(p_tm tm) 91int tm_getfailure(p_tm tm)
70{ 92{
71 /* no timeout */ 93 if (tm->block < 0 && tm->total < 0) {
72 if (tm->block < 0 && tm->total < 0)
73 return -1; 94 return -1;
74 /* there is no block timeout, we use the return timeout */ 95 } else if (tm->block < 0) {
75 else if (tm->block < 0) 96 int t = tm->total - tm_gettime() + tm->start;
76 return MAX(tm->total - tm_gettime() + tm->start, 0); 97 return MAX(t, 0);
77 /* there is no return timeout, we use the block timeout */ 98 } else if (tm->total < 0) {
78 else if (tm->total < 0) 99 int t = tm->block - tm_gettime() + tm->start;
79 return tm->block; 100 return MAX(t, 0);
80 /* both timeouts are specified */ 101 } else {
81 else return MIN(tm->block, 102 int t = tm->total - tm_gettime() + tm->start;
82 MAX(tm->total - tm_gettime() + tm->start, 0)); 103 return MIN(tm->block, MAX(t, 0));
104 }
83} 105}
84 106
85/*-------------------------------------------------------------------------*\ 107/*-------------------------------------------------------------------------*\
@@ -131,10 +153,10 @@ int tm_meth_settimeout(lua_State *L, p_tm tm)
131 const char *mode = luaL_optstring(L, 3, "b"); 153 const char *mode = luaL_optstring(L, 3, "b");
132 switch (*mode) { 154 switch (*mode) {
133 case 'b': 155 case 'b':
134 tm_setblock(tm, ms); 156 tm->block = ms;
135 break; 157 break;
136 case 'r': case 't': 158 case 'r': case 't':
137 tm_settotal(tm, ms); 159 tm->total = ms;
138 break; 160 break;
139 default: 161 default:
140 luaL_argcheck(L, 0, 3, "invalid timeout mode"); 162 luaL_argcheck(L, 0, 3, "invalid timeout mode");