forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua-debugchannel.c
285 lines (259 loc) · 6.16 KB
/
lua-debugchannel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#define LUA_LIB
// only for debug use
#include <lua.h>
#include <lauxlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "spinlock.h"
#define METANAME "debugchannel"
struct command {
struct command * next;
size_t sz;
};
struct channel {
struct spinlock lock;
int ref;
struct command * head;
struct command * tail;
};
static struct channel *
channel_new() {
struct channel * c = malloc(sizeof(*c));
memset(c, 0 , sizeof(*c));
c->ref = 1;
SPIN_INIT(c)
return c;
}
static struct channel *
channel_connect(struct channel *c) {
struct channel * ret = NULL;
SPIN_LOCK(c)
if (c->ref == 1) {
++c->ref;
ret = c;
}
SPIN_UNLOCK(c)
return ret;
}
static struct channel *
channel_release(struct channel *c) {
SPIN_LOCK(c)
--c->ref;
if (c->ref > 0) {
SPIN_UNLOCK(c)
return c;
}
// never unlock while reference is 0
struct command * p = c->head;
c->head = NULL;
c->tail = NULL;
while(p) {
struct command *next = p->next;
free(p);
p = next;
}
SPIN_UNLOCK(c)
SPIN_DESTROY(c)
free(c);
return NULL;
}
// call free after channel_read
static struct command *
channel_read(struct channel *c, double timeout) {
struct command * ret = NULL;
SPIN_LOCK(c)
if (c->head == NULL) {
SPIN_UNLOCK(c)
int ti = (int)(timeout * 100000);
usleep(ti);
return NULL;
}
ret = c->head;
c->head = ret->next;
if (c->head == NULL) {
c->tail = NULL;
}
SPIN_UNLOCK(c)
return ret;
}
static void
channel_write(struct channel *c, const char * s, size_t sz) {
struct command * cmd = malloc(sizeof(*cmd)+ sz);
cmd->sz = sz;
cmd->next = NULL;
memcpy(cmd+1, s, sz);
SPIN_LOCK(c)
if (c->tail == NULL) {
c->head = c->tail = cmd;
} else {
c->tail->next = cmd;
c->tail = cmd;
}
SPIN_UNLOCK(c)
}
struct channel_box {
struct channel *c;
};
static int
lread(lua_State *L) {
struct channel_box *cb = luaL_checkudata(L,1, METANAME);
double ti = luaL_optnumber(L, 2, 0);
struct command * c = channel_read(cb->c, ti);
if (c == NULL)
return 0;
lua_pushlstring(L, (const char *)(c+1), c->sz);
free(c);
return 1;
}
static int
lwrite(lua_State *L) {
struct channel_box *cb = luaL_checkudata(L,1, METANAME);
size_t sz;
const char * str = luaL_checklstring(L, 2, &sz);
channel_write(cb->c, str, sz);
return 0;
}
static int
lrelease(lua_State *L) {
struct channel_box *cb = lua_touserdata(L, 1);
if (cb) {
if (channel_release(cb->c) == NULL) {
cb->c = NULL;
}
}
return 0;
}
static struct channel *
new_channel(lua_State *L, struct channel *c) {
if (c == NULL) {
c = channel_new();
} else {
c = channel_connect(c);
}
if (c == NULL) {
luaL_error(L, "new channel failed");
// never go here
}
struct channel_box * cb = lua_newuserdatauv(L, sizeof(*cb), 0);
cb->c = c;
if (luaL_newmetatable(L, METANAME)) {
luaL_Reg l[]={
{ "read", lread },
{ "write", lwrite },
{ NULL, NULL },
};
luaL_newlib(L,l);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lrelease);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return c;
}
static int
lcreate(lua_State *L) {
struct channel *c = new_channel(L, NULL);
lua_pushlightuserdata(L, c);
return 2;
}
static int
lconnect(lua_State *L) {
struct channel *c = lua_touserdata(L, 1);
if (c == NULL)
return luaL_error(L, "Invalid channel pointer");
new_channel(L, c);
return 1;
}
static const int HOOKKEY = 0;
/*
** Auxiliary function used by several library functions: check for
** an optional thread as function's first argument and set 'arg' with
** 1 if this argument is present (so that functions can skip it to
** access their other arguments)
*/
static lua_State *getthread (lua_State *L, int *arg) {
if (lua_isthread(L, 1)) {
*arg = 1;
return lua_tothread(L, 1);
}
else {
*arg = 0;
return L; /* function will operate over current thread */
}
}
/*
** Call hook function registered at hook table for the current
** thread (if there is one)
*/
static void hookf (lua_State *L, lua_Debug *ar) {
static const char *const hooknames[] =
{"call", "return", "line", "count", "tail call"};
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
lua_pushthread(L);
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
if (ar->currentline >= 0)
lua_pushinteger(L, ar->currentline); /* push current line */
else lua_pushnil(L);
lua_call(L, 2, 1); /* call hook function */
int yield = lua_toboolean(L, -1);
lua_pop(L,1);
if (yield) {
lua_yield(L, 0);
}
}
}
/*
** Convert a string mask (for 'sethook') into a bit mask
*/
static int makemask (const char *smask, int count) {
int mask = 0;
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
if (count > 0) mask |= LUA_MASKCOUNT;
return mask;
}
static int db_sethook (lua_State *L) {
int arg, mask, count;
lua_Hook func;
lua_State *L1 = getthread(L, &arg);
if (lua_isnoneornil(L, arg+1)) { /* no hook? */
lua_settop(L, arg+1);
func = NULL; mask = 0; count = 0; /* turn off hooks */
}
else {
const char *smask = luaL_checkstring(L, arg+2);
luaL_checktype(L, arg+1, LUA_TFUNCTION);
count = (int)luaL_optinteger(L, arg + 3, 0);
func = hookf; mask = makemask(smask, count);
}
if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) {
lua_createtable(L, 0, 2); /* create a hook table */
lua_pushvalue(L, -1);
lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */
lua_pushstring(L, "k");
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
lua_pushvalue(L, -1);
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
}
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
lua_pushvalue(L, arg + 1); /* value (hook function) */
lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
lua_sethook(L1, func, mask, count);
return 0;
}
LUAMOD_API int
luaopen_skynet_debugchannel(lua_State *L) {
luaL_Reg l[] = {
{ "create", lcreate }, // for write
{ "connect", lconnect }, // for read
{ "release", lrelease },
{ "sethook", db_sethook },
{ NULL, NULL },
};
luaL_checkversion(L);
luaL_newlib(L,l);
return 1;
}