diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-10-07 04:40:59 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-10-07 04:40:59 +0000 |
| commit | f4dadea763c1959a27dead24df3ee6c54c209842 (patch) | |
| tree | c13b294a8ca5438d59b60e3f5a25a4f7c1fc9a1b /src/timeout.c | |
| parent | 562d8cceb704a96a7b2f9acc4bc229ab9f5c6541 (diff) | |
| download | luasocket-f4dadea763c1959a27dead24df3ee6c54c209842.tar.gz luasocket-f4dadea763c1959a27dead24df3ee6c54c209842.tar.bz2 luasocket-f4dadea763c1959a27dead24df3ee6c54c209842.zip | |
Before compiling on Windows.
Diffstat (limited to 'src/timeout.c')
| -rw-r--r-- | src/timeout.c | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/src/timeout.c b/src/timeout.c index 4f1d345..863546e 100644 --- a/src/timeout.c +++ b/src/timeout.c | |||
| @@ -30,12 +30,12 @@ | |||
| 30 | /*=========================================================================*\ | 30 | /*=========================================================================*\ |
| 31 | * Internal function prototypes | 31 | * Internal function prototypes |
| 32 | \*=========================================================================*/ | 32 | \*=========================================================================*/ |
| 33 | static int tm_lua_gettime(lua_State *L); | 33 | static int timeout_lua_gettime(lua_State *L); |
| 34 | static int tm_lua_sleep(lua_State *L); | 34 | static int timeout_lua_sleep(lua_State *L); |
| 35 | 35 | ||
| 36 | static luaL_reg func[] = { | 36 | static luaL_reg func[] = { |
| 37 | { "gettime", tm_lua_gettime }, | 37 | { "gettime", timeout_lua_gettime }, |
| 38 | { "sleep", tm_lua_sleep }, | 38 | { "sleep", timeout_lua_sleep }, |
| 39 | { NULL, NULL } | 39 | { NULL, NULL } |
| 40 | }; | 40 | }; |
| 41 | 41 | ||
| @@ -45,7 +45,7 @@ static luaL_reg func[] = { | |||
| 45 | /*-------------------------------------------------------------------------*\ | 45 | /*-------------------------------------------------------------------------*\ |
| 46 | * Initialize structure | 46 | * Initialize structure |
| 47 | \*-------------------------------------------------------------------------*/ | 47 | \*-------------------------------------------------------------------------*/ |
| 48 | void tm_init(p_tm tm, double block, double total) { | 48 | void timeout_init(p_timeout tm, double block, double total) { |
| 49 | tm->block = block; | 49 | tm->block = block; |
| 50 | tm->total = total; | 50 | tm->total = total; |
| 51 | } | 51 | } |
| @@ -58,16 +58,16 @@ void tm_init(p_tm tm, double block, double total) { | |||
| 58 | * Returns | 58 | * Returns |
| 59 | * the number of ms left or -1 if there is no time limit | 59 | * the number of ms left or -1 if there is no time limit |
| 60 | \*-------------------------------------------------------------------------*/ | 60 | \*-------------------------------------------------------------------------*/ |
| 61 | double tm_get(p_tm tm) { | 61 | double timeout_get(p_timeout tm) { |
| 62 | if (tm->block < 0.0 && tm->total < 0.0) { | 62 | if (tm->block < 0.0 && tm->total < 0.0) { |
| 63 | return -1; | 63 | return -1; |
| 64 | } else if (tm->block < 0.0) { | 64 | } else if (tm->block < 0.0) { |
| 65 | double t = tm->total - tm_gettime() + tm->start; | 65 | double t = tm->total - timeout_gettime() + tm->start; |
| 66 | return MAX(t, 0.0); | 66 | return MAX(t, 0.0); |
| 67 | } else if (tm->total < 0.0) { | 67 | } else if (tm->total < 0.0) { |
| 68 | return tm->block; | 68 | return tm->block; |
| 69 | } else { | 69 | } else { |
| 70 | double t = tm->total - tm_gettime() + tm->start; | 70 | double t = tm->total - timeout_gettime() + tm->start; |
| 71 | return MIN(tm->block, MAX(t, 0.0)); | 71 | return MIN(tm->block, MAX(t, 0.0)); |
| 72 | } | 72 | } |
| 73 | } | 73 | } |
| @@ -79,7 +79,7 @@ double tm_get(p_tm tm) { | |||
| 79 | * Returns | 79 | * Returns |
| 80 | * start field of structure | 80 | * start field of structure |
| 81 | \*-------------------------------------------------------------------------*/ | 81 | \*-------------------------------------------------------------------------*/ |
| 82 | double tm_getstart(p_tm tm) { | 82 | double timeout_getstart(p_timeout tm) { |
| 83 | return tm->start; | 83 | return tm->start; |
| 84 | } | 84 | } |
| 85 | 85 | ||
| @@ -91,17 +91,17 @@ double tm_getstart(p_tm tm) { | |||
| 91 | * Returns | 91 | * Returns |
| 92 | * the number of ms left or -1 if there is no time limit | 92 | * the number of ms left or -1 if there is no time limit |
| 93 | \*-------------------------------------------------------------------------*/ | 93 | \*-------------------------------------------------------------------------*/ |
| 94 | double tm_getretry(p_tm tm) { | 94 | double timeout_getretry(p_timeout tm) { |
| 95 | if (tm->block < 0.0 && tm->total < 0.0) { | 95 | if (tm->block < 0.0 && tm->total < 0.0) { |
| 96 | return -1; | 96 | return -1; |
| 97 | } else if (tm->block < 0.0) { | 97 | } else if (tm->block < 0.0) { |
| 98 | double t = tm->total - tm_gettime() + tm->start; | 98 | double t = tm->total - timeout_gettime() + tm->start; |
| 99 | return MAX(t, 0.0); | 99 | return MAX(t, 0.0); |
| 100 | } else if (tm->total < 0.0) { | 100 | } else if (tm->total < 0.0) { |
| 101 | double t = tm->block - tm_gettime() + tm->start; | 101 | double t = tm->block - timeout_gettime() + tm->start; |
| 102 | return MAX(t, 0.0); | 102 | return MAX(t, 0.0); |
| 103 | } else { | 103 | } else { |
| 104 | double t = tm->total - tm_gettime() + tm->start; | 104 | double t = tm->total - timeout_gettime() + tm->start; |
| 105 | return MIN(tm->block, MAX(t, 0.0)); | 105 | return MIN(tm->block, MAX(t, 0.0)); |
| 106 | } | 106 | } |
| 107 | } | 107 | } |
| @@ -111,8 +111,8 @@ double tm_getretry(p_tm tm) { | |||
| 111 | * Input | 111 | * Input |
| 112 | * tm: timeout control structure | 112 | * tm: timeout control structure |
| 113 | \*-------------------------------------------------------------------------*/ | 113 | \*-------------------------------------------------------------------------*/ |
| 114 | p_tm tm_markstart(p_tm tm) { | 114 | p_timeout timeout_markstart(p_timeout tm) { |
| 115 | tm->start = tm_gettime(); | 115 | tm->start = timeout_gettime(); |
| 116 | return tm; | 116 | return tm; |
| 117 | } | 117 | } |
| 118 | 118 | ||
| @@ -122,7 +122,7 @@ p_tm tm_markstart(p_tm tm) { | |||
| 122 | * time in s. | 122 | * time in s. |
| 123 | \*-------------------------------------------------------------------------*/ | 123 | \*-------------------------------------------------------------------------*/ |
| 124 | #ifdef _WIN32 | 124 | #ifdef _WIN32 |
| 125 | double tm_gettime(void) { | 125 | double timeout_gettime(void) { |
| 126 | FILETIME ft; | 126 | FILETIME ft; |
| 127 | double t; | 127 | double t; |
| 128 | GetSystemTimeAsFileTime(&ft); | 128 | GetSystemTimeAsFileTime(&ft); |
| @@ -132,7 +132,7 @@ double tm_gettime(void) { | |||
| 132 | return (t - 11644473600.0); | 132 | return (t - 11644473600.0); |
| 133 | } | 133 | } |
| 134 | #else | 134 | #else |
| 135 | double tm_gettime(void) { | 135 | double timeout_gettime(void) { |
| 136 | struct timeval v; | 136 | struct timeval v; |
| 137 | gettimeofday(&v, (struct timezone *) NULL); | 137 | gettimeofday(&v, (struct timezone *) NULL); |
| 138 | /* Unix Epoch time (time since January 1, 1970 (UTC)) */ | 138 | /* Unix Epoch time (time since January 1, 1970 (UTC)) */ |
| @@ -143,7 +143,7 @@ double tm_gettime(void) { | |||
| 143 | /*-------------------------------------------------------------------------*\ | 143 | /*-------------------------------------------------------------------------*\ |
| 144 | * Initializes module | 144 | * Initializes module |
| 145 | \*-------------------------------------------------------------------------*/ | 145 | \*-------------------------------------------------------------------------*/ |
| 146 | int tm_open(lua_State *L) { | 146 | int timeout_open(lua_State *L) { |
| 147 | luaL_openlib(L, NULL, func, 0); | 147 | luaL_openlib(L, NULL, func, 0); |
| 148 | return 0; | 148 | return 0; |
| 149 | } | 149 | } |
| @@ -154,7 +154,7 @@ int tm_open(lua_State *L) { | |||
| 154 | * time: time out value in seconds | 154 | * time: time out value in seconds |
| 155 | * mode: "b" for block timeout, "t" for total timeout. (default: b) | 155 | * mode: "b" for block timeout, "t" for total timeout. (default: b) |
| 156 | \*-------------------------------------------------------------------------*/ | 156 | \*-------------------------------------------------------------------------*/ |
| 157 | int tm_meth_settimeout(lua_State *L, p_tm tm) { | 157 | int timeout_meth_settimeout(lua_State *L, p_timeout tm) { |
| 158 | double t = luaL_optnumber(L, 2, -1); | 158 | double t = luaL_optnumber(L, 2, -1); |
| 159 | const char *mode = luaL_optstring(L, 3, "b"); | 159 | const char *mode = luaL_optstring(L, 3, "b"); |
| 160 | switch (*mode) { | 160 | switch (*mode) { |
| @@ -178,16 +178,16 @@ int tm_meth_settimeout(lua_State *L, p_tm tm) { | |||
| 178 | /*-------------------------------------------------------------------------*\ | 178 | /*-------------------------------------------------------------------------*\ |
| 179 | * Returns the time the system has been up, in secconds. | 179 | * Returns the time the system has been up, in secconds. |
| 180 | \*-------------------------------------------------------------------------*/ | 180 | \*-------------------------------------------------------------------------*/ |
| 181 | static int tm_lua_gettime(lua_State *L) | 181 | static int timeout_lua_gettime(lua_State *L) |
| 182 | { | 182 | { |
| 183 | lua_pushnumber(L, tm_gettime()); | 183 | lua_pushnumber(L, timeout_gettime()); |
| 184 | return 1; | 184 | return 1; |
| 185 | } | 185 | } |
| 186 | 186 | ||
| 187 | /*-------------------------------------------------------------------------*\ | 187 | /*-------------------------------------------------------------------------*\ |
| 188 | * Sleep for n seconds. | 188 | * Sleep for n seconds. |
| 189 | \*-------------------------------------------------------------------------*/ | 189 | \*-------------------------------------------------------------------------*/ |
| 190 | int tm_lua_sleep(lua_State *L) | 190 | int timeout_lua_sleep(lua_State *L) |
| 191 | { | 191 | { |
| 192 | double n = luaL_checknumber(L, 1); | 192 | double n = luaL_checknumber(L, 1); |
| 193 | #ifdef _WIN32 | 193 | #ifdef _WIN32 |
