diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-05-25 01:54:13 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-05-25 01:54:13 +0000 |
| commit | 0f6c8d50a99997ac7829864b1c93362b50f1bbf3 (patch) | |
| tree | d0cefe3a05484e65b7b7e79d8cae4a1d2e6d19fb /src/timeout.c | |
| parent | c1ef3e7103cc652d2004ef1ddc9409b946207f33 (diff) | |
| download | luasocket-0f6c8d50a99997ac7829864b1c93362b50f1bbf3.tar.gz luasocket-0f6c8d50a99997ac7829864b1c93362b50f1bbf3.tar.bz2 luasocket-0f6c8d50a99997ac7829864b1c93362b50f1bbf3.zip | |
Porting to LUA 5.0 final
Diffstat (limited to 'src/timeout.c')
| -rw-r--r-- | src/timeout.c | 116 |
1 files changed, 64 insertions, 52 deletions
diff --git a/src/timeout.c b/src/timeout.c index 5549c89..17878aa 100644 --- a/src/timeout.c +++ b/src/timeout.c | |||
| @@ -1,18 +1,19 @@ | |||
| 1 | /*=========================================================================*\ | 1 | /*=========================================================================*\ |
| 2 | * Timeout management functions | 2 | * Timeout management functions |
| 3 | * Global Lua functions: | 3 | * Global Lua functions: |
| 4 | * _sleep: (debug mode only) | 4 | * _sleep |
| 5 | * _time: (debug mode only) | 5 | * _time |
| 6 | * | 6 | * |
| 7 | * RCS ID: $Id$ | 7 | * RCS ID: $Id$ |
| 8 | \*=========================================================================*/ | 8 | \*=========================================================================*/ |
| 9 | #include <stdio.h> | ||
| 10 | |||
| 9 | #include <lua.h> | 11 | #include <lua.h> |
| 10 | #include <lauxlib.h> | 12 | #include <lauxlib.h> |
| 11 | 13 | ||
| 12 | #include "lspriv.h" | 14 | #include "luasocket.h" |
| 13 | #include "lstm.h" | 15 | #include "aux.h" |
| 14 | 16 | #include "tm.h" | |
| 15 | #include <stdio.h> | ||
| 16 | 17 | ||
| 17 | #ifdef WIN32 | 18 | #ifdef WIN32 |
| 18 | #include <windows.h> | 19 | #include <windows.h> |
| @@ -28,78 +29,69 @@ | |||
| 28 | static int tm_lua_time(lua_State *L); | 29 | static int tm_lua_time(lua_State *L); |
| 29 | static int tm_lua_sleep(lua_State *L); | 30 | static int tm_lua_sleep(lua_State *L); |
| 30 | 31 | ||
| 32 | static luaL_reg func[] = { | ||
| 33 | { "time", tm_lua_time }, | ||
| 34 | { "sleep", tm_lua_sleep }, | ||
| 35 | { NULL, NULL } | ||
| 36 | }; | ||
| 37 | |||
| 31 | /*=========================================================================*\ | 38 | /*=========================================================================*\ |
| 32 | * Exported functions. | 39 | * Exported functions. |
| 33 | \*=========================================================================*/ | 40 | \*=========================================================================*/ |
| 34 | /*-------------------------------------------------------------------------*\ | 41 | /*-------------------------------------------------------------------------*\ |
| 35 | * Sets timeout limits | 42 | * Initialize structure |
| 36 | * Input | ||
| 37 | * tm: timeout control structure | ||
| 38 | * mode: block or return timeout | ||
| 39 | * value: timeout value in miliseconds | ||
| 40 | \*-------------------------------------------------------------------------*/ | 43 | \*-------------------------------------------------------------------------*/ |
| 41 | void tm_set(p_tm tm, int tm_block, int tm_return) | 44 | void tm_init(p_tm tm, int block, int total) |
| 42 | { | 45 | { |
| 43 | tm->tm_block = tm_block; | 46 | tm->block = block; |
| 44 | tm->tm_return = tm_return; | 47 | tm->total = total; |
| 45 | } | 48 | } |
| 46 | 49 | ||
| 47 | /*-------------------------------------------------------------------------*\ | 50 | /*-------------------------------------------------------------------------*\ |
| 48 | * Returns timeout limits | 51 | * Set and get timeout limits |
| 49 | * Input | ||
| 50 | * tm: timeout control structure | ||
| 51 | * mode: block or return timeout | ||
| 52 | * value: timeout value in miliseconds | ||
| 53 | \*-------------------------------------------------------------------------*/ | 52 | \*-------------------------------------------------------------------------*/ |
| 54 | void tm_get(p_tm tm, int *tm_block, int *tm_return) | 53 | void tm_setblock(p_tm tm, int block) |
| 55 | { | 54 | { tm->block = block; } |
| 56 | if (tm_block) *tm_block = tm->tm_block; | 55 | void tm_settotal(p_tm tm, int total) |
| 57 | if (tm_return) *tm_return = tm->tm_return; | 56 | { tm->total = total; } |
| 58 | } | 57 | int tm_getblock(p_tm tm) |
| 58 | { return tm->block; } | ||
| 59 | int tm_gettotal(p_tm tm) | ||
| 60 | { return tm->total; } | ||
| 61 | int tm_getstart(p_tm tm) | ||
| 62 | { return tm->start; } | ||
| 59 | 63 | ||
| 60 | /*-------------------------------------------------------------------------*\ | 64 | /*-------------------------------------------------------------------------*\ |
| 61 | * Determines how much time we have left for the current io operation | 65 | * Determines how much time we have left for the current operation |
| 62 | * an IO write operation. | ||
| 63 | * Input | 66 | * Input |
| 64 | * tm: timeout control structure | 67 | * tm: timeout control structure |
| 65 | * Returns | 68 | * Returns |
| 66 | * the number of ms left or -1 if there is no time limit | 69 | * the number of ms left or -1 if there is no time limit |
| 67 | \*-------------------------------------------------------------------------*/ | 70 | \*-------------------------------------------------------------------------*/ |
| 68 | int tm_getremaining(p_tm tm) | 71 | int tm_get(p_tm tm) |
| 69 | { | 72 | { |
| 70 | /* no timeout */ | 73 | /* no timeout */ |
| 71 | if (tm->tm_block < 0 && tm->tm_return < 0) | 74 | if (tm->block < 0 && tm->total < 0) |
| 72 | return -1; | 75 | return -1; |
| 73 | /* there is no block timeout, we use the return timeout */ | 76 | /* there is no block timeout, we use the return timeout */ |
| 74 | else if (tm->tm_block < 0) | 77 | else if (tm->block < 0) |
| 75 | return MAX(tm->tm_return - tm_gettime() + tm->tm_start, 0); | 78 | return MAX(tm->total - tm_gettime() + tm->start, 0); |
| 76 | /* there is no return timeout, we use the block timeout */ | 79 | /* there is no return timeout, we use the block timeout */ |
| 77 | else if (tm->tm_return < 0) | 80 | else if (tm->total < 0) |
| 78 | return tm->tm_block; | 81 | return tm->block; |
| 79 | /* both timeouts are specified */ | 82 | /* both timeouts are specified */ |
| 80 | else return MIN(tm->tm_block, | 83 | else return MIN(tm->block, |
| 81 | MAX(tm->tm_return - tm_gettime() + tm->tm_start, 0)); | 84 | MAX(tm->total - tm_gettime() + tm->start, 0)); |
| 82 | } | 85 | } |
| 83 | 86 | ||
| 84 | /*-------------------------------------------------------------------------*\ | 87 | /*-------------------------------------------------------------------------*\ |
| 85 | * Marks the operation start time in sock structure | 88 | * Marks the operation start time in structure |
| 86 | * Input | 89 | * Input |
| 87 | * tm: timeout control structure | 90 | * tm: timeout control structure |
| 88 | \*-------------------------------------------------------------------------*/ | 91 | \*-------------------------------------------------------------------------*/ |
| 89 | void tm_markstart(p_tm tm) | 92 | void tm_markstart(p_tm tm) |
| 90 | { | 93 | { |
| 91 | tm->tm_start = tm_gettime(); | 94 | tm->start = tm_gettime(); |
| 92 | tm->tm_end = tm->tm_start; | ||
| 93 | } | ||
| 94 | |||
| 95 | /*-------------------------------------------------------------------------*\ | ||
| 96 | * Returns the length of the operation in ms | ||
| 97 | * Input | ||
| 98 | * tm: timeout control structure | ||
| 99 | \*-------------------------------------------------------------------------*/ | ||
| 100 | int tm_getelapsed(p_tm tm) | ||
| 101 | { | ||
| 102 | return tm->tm_end - tm->tm_start; | ||
| 103 | } | 95 | } |
| 104 | 96 | ||
| 105 | /*-------------------------------------------------------------------------*\ | 97 | /*-------------------------------------------------------------------------*\ |
| @@ -125,11 +117,31 @@ int tm_gettime(void) | |||
| 125 | \*-------------------------------------------------------------------------*/ | 117 | \*-------------------------------------------------------------------------*/ |
| 126 | void tm_open(lua_State *L) | 118 | void tm_open(lua_State *L) |
| 127 | { | 119 | { |
| 128 | (void) L; | 120 | luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); |
| 129 | lua_pushcfunction(L, tm_lua_time); | 121 | } |
| 130 | priv_newglobal(L, "_time"); | 122 | |
| 131 | lua_pushcfunction(L, tm_lua_sleep); | 123 | /*-------------------------------------------------------------------------*\ |
| 132 | priv_newglobal(L, "_sleep"); | 124 | * Sets timeout values for IO operations |
| 125 | * Lua Input: base, time [, mode] | ||
| 126 | * time: time out value in seconds | ||
| 127 | * mode: "b" for block timeout, "t" for total timeout. (default: b) | ||
| 128 | \*-------------------------------------------------------------------------*/ | ||
| 129 | int tm_meth_timeout(lua_State *L, p_tm tm) | ||
| 130 | { | ||
| 131 | int ms = lua_isnil(L, 2) ? -1 : (int) (luaL_checknumber(L, 2)*1000.0); | ||
| 132 | const char *mode = luaL_optstring(L, 3, "b"); | ||
| 133 | switch (*mode) { | ||
| 134 | case 'b': | ||
| 135 | tm_setblock(tm, ms); | ||
| 136 | break; | ||
| 137 | case 'r': case 't': | ||
| 138 | tm_settotal(tm, ms); | ||
| 139 | break; | ||
| 140 | default: | ||
| 141 | luaL_argcheck(L, 0, 3, "invalid timeout mode"); | ||
| 142 | break; | ||
| 143 | } | ||
| 144 | return 0; | ||
| 133 | } | 145 | } |
| 134 | 146 | ||
| 135 | /*=========================================================================*\ | 147 | /*=========================================================================*\ |
