-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlworker.c
180 lines (144 loc) · 4.03 KB
/
lworker.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
#include <pthread.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <string.h>
#include <stdio.h>
typedef struct worker {
lua_State *L;
pthread_t thread;
pthread_cond_t cond;
const char *channel;
struct worker *prev, *next;
} worker;
int luaopen_lworker(lua_State *L);
static worker * WSEND = NULL;
static worker * WRECV = NULL;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static worker* _self(lua_State *L) {
worker * p;
lua_getfield(L, LUA_REGISTRYINDEX, "_SELF");
p = (worker *)lua_touserdata(L, -1);
return p;
}
/* move vlaues from a sender process to a receiver*/
static void _move(lua_State *send, lua_State *recv) {
int n = lua_gettop(send);
int i;
for (i = 2; i <= n; i++) { /*1st channel*/
lua_pushstring(recv, lua_tostring(send, i));
}
}
static worker* _match(const char *channel, worker **queue) {
worker *node = *queue;
if (node == NULL) return NULL;
do {
if (strcmp(channel, node->channel) == 0) {
if (*queue == node)
*queue = (node->next == node) ? NULL : node->next;
node->prev->next = node->next;
node->next->prev = node->prev;
return node;
}
node = node->next;
} while (node != *queue);
return NULL;
}
static void _wait(lua_State *L, const char *channel, worker **queue) {
worker *p = _self(L);
if (*queue == NULL) {
*queue = p;
p->prev = p->next = p;
} else {
p->prev = (*queue)->prev;
p->next = *queue;
p->prev->next = p->next->prev = p;
}
p->channel = channel;
do{
pthread_cond_wait(&p->cond, &mutex);
} while(p->channel);
}
static void* _thread(void *arg){
lua_State *L = (lua_State *)arg;
luaL_openlibs(L);
luaL_requiref(L, "lwork", luaopen_lworker, 1);
lua_pop(L, 1);
if(lua_pcall(L, 0, 0, 0) != 0)
fprintf(stderr, "thread error:%s", lua_tostring(L, -1));
printf("END PCALL\n");
pthread_cond_destroy(&_self(L)->cond);
lua_close(L);
return NULL;
}
static int lsend(lua_State *L) {
worker *p;
const char *channel = luaL_checkstring(L, 1);
printf("SEND : %s\n", channel);
pthread_mutex_lock(&mutex);
p = _match(channel, &WRECV);
if (p) {
_move(L, p->L);
p->channel = NULL;
pthread_cond_signal(&p->cond);
} else {
_wait(L, channel, &WSEND);
}
pthread_mutex_unlock(&mutex);
return 0;
}
static int lrecv(lua_State *L) {
worker *p;
const char *channel = luaL_checkstring(L, 1);
printf("RECV : %s\n", channel);
lua_settop(L, 1);
pthread_mutex_lock(&mutex);
p = _match(channel, &WSEND);
if (p) {
_move(p->L, L);
p->channel = NULL;
pthread_cond_signal(&p->cond);
} else {
_wait(L, channel, &WRECV);
}
pthread_mutex_unlock(&mutex);
return lua_gettop(L) - 1;
}
static int lstart(lua_State *L) {
pthread_t thread;
const char *chunk = luaL_checkstring(L, 1);
printf("%s\n", chunk);
lua_State *L1 = luaL_newstate();
if (L1 == NULL)
luaL_error(L, "unable to create new lua state");
printf("load main chunk:\n");
if (luaL_loadstring(L1, chunk) != 0)
luaL_error(L, "error starting thread: %s", lua_tostring(L1, -1));
if (pthread_create(&thread, NULL, _thread, L1) != 0)
luaL_error(L, "unable to create new thread");
//pthread_detach(thread);
pthread_join(thread, NULL);
return 0;
}
static int lexit(lua_State *L) {
pthread_exit(NULL);
return 0;
}
static const struct luaL_Reg l[] = {
{"start", lstart},
{"send", lsend},
{"recv", lrecv},
{"exit", lexit},
{NULL, NULL}
};
int luaopen_lworker(lua_State *L) {
printf("begin load worker lib\n");
worker *self = (worker *)lua_newuserdata(L, sizeof(worker));
lua_setfield(L, LUA_REGISTRYINDEX, "_SELF");
self->L = L;
self->thread = pthread_self();
self->channel = NULL;
pthread_cond_init(&self->cond, NULL);
luaL_newlib(L, l);
return 1;
}