diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2002-07-03 19:06:54 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2002-07-03 19:06:54 +0000 |
commit | 88026bef8a5ca586e354965a79134646fb566c72 (patch) | |
tree | 7594d9748c9dfd5db4addc0756b0cf86a0d9206a /src/timeout.c | |
parent | 9b8bce6465d2c7de84782f9e12f529a020a16444 (diff) | |
download | luasocket-88026bef8a5ca586e354965a79134646fb566c72.tar.gz luasocket-88026bef8a5ca586e354965a79134646fb566c72.tar.bz2 luasocket-88026bef8a5ca586e354965a79134646fb566c72.zip |
Initial revision
Diffstat (limited to 'src/timeout.c')
-rw-r--r-- | src/timeout.c | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/src/timeout.c b/src/timeout.c new file mode 100644 index 0000000..266a86e --- /dev/null +++ b/src/timeout.c | |||
@@ -0,0 +1,160 @@ | |||
1 | /*=========================================================================*\ | ||
2 | * Timeout management functions | ||
3 | \*=========================================================================*/ | ||
4 | #include <lua.h> | ||
5 | #include <lauxlib.h> | ||
6 | |||
7 | #include "lspriv.h" | ||
8 | #include "lstm.h" | ||
9 | |||
10 | #include <stdio.h> | ||
11 | |||
12 | #ifdef WIN32 | ||
13 | #include <windows.h> | ||
14 | #else | ||
15 | #include <sys/times.h> | ||
16 | #include <time.h> | ||
17 | #include <unistd.h> | ||
18 | #endif | ||
19 | |||
20 | /*=========================================================================*\ | ||
21 | * Internal function prototypes | ||
22 | \*=========================================================================*/ | ||
23 | #ifdef _DEBUG | ||
24 | static int tm_lua_time(lua_State *L); | ||
25 | static int tm_lua_sleep(lua_State *L); | ||
26 | #endif | ||
27 | |||
28 | /*=========================================================================*\ | ||
29 | * Exported functions. | ||
30 | \*=========================================================================*/ | ||
31 | /*-------------------------------------------------------------------------*\ | ||
32 | * Sets timeout limits | ||
33 | * Input | ||
34 | * tm: timeout control structure | ||
35 | * mode: block or return timeout | ||
36 | * value: timeout value in miliseconds | ||
37 | \*-------------------------------------------------------------------------*/ | ||
38 | void tm_set(p_tm tm, int tm_block, int tm_return) | ||
39 | { | ||
40 | tm->tm_block = tm_block; | ||
41 | tm->tm_return = tm_return; | ||
42 | } | ||
43 | |||
44 | /*-------------------------------------------------------------------------*\ | ||
45 | * Returns timeout limits | ||
46 | * Input | ||
47 | * tm: timeout control structure | ||
48 | * mode: block or return timeout | ||
49 | * value: timeout value in miliseconds | ||
50 | \*-------------------------------------------------------------------------*/ | ||
51 | void tm_get(p_tm tm, int *tm_block, int *tm_return) | ||
52 | { | ||
53 | if (tm_block) *tm_block = tm->tm_block; | ||
54 | if (tm_return) *tm_return = tm->tm_return; | ||
55 | } | ||
56 | |||
57 | /*-------------------------------------------------------------------------*\ | ||
58 | * Determines how much time we have left for the current io operation | ||
59 | * an IO write operation. | ||
60 | * Input | ||
61 | * tm: timeout control structure | ||
62 | * Returns | ||
63 | * the number of ms left or -1 if there is no time limit | ||
64 | \*-------------------------------------------------------------------------*/ | ||
65 | int tm_getremaining(p_tm tm) | ||
66 | { | ||
67 | /* no timeout */ | ||
68 | if (tm->tm_block < 0 && tm->tm_return < 0) | ||
69 | return -1; | ||
70 | /* there is no block timeout, we use the return timeout */ | ||
71 | else if (tm->tm_block < 0) | ||
72 | return MAX(tm->tm_return - tm_gettime() + tm->tm_start, 0); | ||
73 | /* there is no return timeout, we use the block timeout */ | ||
74 | else if (tm->tm_return < 0) | ||
75 | return tm->tm_block; | ||
76 | /* both timeouts are specified */ | ||
77 | else return MIN(tm->tm_block, | ||
78 | MAX(tm->tm_return - tm_gettime() + tm->tm_start, 0)); | ||
79 | } | ||
80 | |||
81 | /*-------------------------------------------------------------------------*\ | ||
82 | * Marks the operation start time in sock structure | ||
83 | * Input | ||
84 | * tm: timeout control structure | ||
85 | \*-------------------------------------------------------------------------*/ | ||
86 | void tm_markstart(p_tm tm) | ||
87 | { | ||
88 | tm->tm_start = tm_gettime(); | ||
89 | tm->tm_end = tm->tm_start; | ||
90 | } | ||
91 | |||
92 | /*-------------------------------------------------------------------------*\ | ||
93 | * Returns the length of the operation in ms | ||
94 | * Input | ||
95 | * tm: timeout control structure | ||
96 | \*-------------------------------------------------------------------------*/ | ||
97 | int tm_getelapsed(p_tm tm) | ||
98 | { | ||
99 | return tm->tm_end - tm->tm_start; | ||
100 | } | ||
101 | |||
102 | /*-------------------------------------------------------------------------*\ | ||
103 | * Gets time in ms, relative to system startup. | ||
104 | * Returns | ||
105 | * time in ms. | ||
106 | \*-------------------------------------------------------------------------*/ | ||
107 | #ifdef WIN32 | ||
108 | int tm_gettime(void) | ||
109 | { | ||
110 | return GetTickCount(); | ||
111 | } | ||
112 | #else | ||
113 | int tm_gettime(void) | ||
114 | { | ||
115 | struct tms t; | ||
116 | return (times(&t)*1000)/CLK_TCK; | ||
117 | } | ||
118 | #endif | ||
119 | |||
120 | /*-------------------------------------------------------------------------*\ | ||
121 | * Initializes module | ||
122 | \*-------------------------------------------------------------------------*/ | ||
123 | void tm_open(lua_State *L) | ||
124 | { | ||
125 | (void) L; | ||
126 | #ifdef _DEBUG | ||
127 | lua_pushcfunction(L, tm_lua_time); | ||
128 | lua_setglobal(L, "_time"); | ||
129 | lua_pushcfunction(L, tm_lua_sleep); | ||
130 | lua_setglobal(L, "_sleep"); | ||
131 | #endif | ||
132 | } | ||
133 | |||
134 | /*=========================================================================*\ | ||
135 | * Test support functions | ||
136 | \*=========================================================================*/ | ||
137 | /*-------------------------------------------------------------------------*\ | ||
138 | * Returns the time the system has been up, in secconds. | ||
139 | \*-------------------------------------------------------------------------*/ | ||
140 | #ifdef _DEBUG | ||
141 | static int tm_lua_time(lua_State *L) | ||
142 | { | ||
143 | lua_pushnumber(L, tm_gettime()/1000.0); | ||
144 | return 1; | ||
145 | } | ||
146 | |||
147 | /*-------------------------------------------------------------------------*\ | ||
148 | * Sleep for n seconds. | ||
149 | \*-------------------------------------------------------------------------*/ | ||
150 | int tm_lua_sleep(lua_State *L) | ||
151 | { | ||
152 | double n = luaL_check_number(L, 1); | ||
153 | #ifdef WIN32 | ||
154 | Sleep(n*1000); | ||
155 | #else | ||
156 | sleep(n); | ||
157 | #endif | ||
158 | return 0; | ||
159 | } | ||
160 | #endif | ||