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
| #include <pthread.h> #include "lua.h" #include "lauxlib.h"
typedef struct Proc { lua_State *L; pthread_t thread; pthread_cond_t cond; const char *channel; struct Proc *previous, *next; } Proc;
static Proc *waitsend = NULL; static Proc *waitreceive = NULL; static pthread_mutex_t kernel_access = PTHREAD_MUTEX_INITIALIZER;
static Proc *getself(lua_State *L) { Proc *p;
lua_getfield(L, LUA_REGISTRYINDEX, "_SELF"); p = (Proc*)lua_touserdata(L, -1); lua_pop(L, 1); return p; }
static void movevalues(lua_State *send, lua_State *rec) { int n = lua_gettop(send); int i;
luaL_checkstack(rec, n, "too many results"); for (i = 2; i < n; i++) { lua_pushstring(rec, lua_tostring(send, i)); } }
static Proc *searchmatch(const char *channel, Proc **list) { Proc *node;
for (node = *list; node != NULL; node = node->next) { if (strcmp(channel, node->channel) == 0) { if (*list == node) { *list = (node->next == node) ? NULL : node->next; }
node->previous->next = node->next; node->next->previous = node->previous; return node; } }
return NULL; }
static void waitonlist(lua_State *L, const char *channel, Proc **list) { Proc *p = getself(L);
if (*list == NULL) { *list = p; p->previous = p->next = p; } else { p->previous = (*list)->previous; p->next = *list; p->previous->next = p->next->previous = p; }
p->channel = channel;
do { pthread_cond_wait(&p->cond, &kernel_access); } while (p->channel); }
static int ll_send(lua_State *L) { Proc *p;
const char *channel = luaL_checkstring(L, 1);
pthread_mutex_lock(&kernel_access);
p = searchmatch(channel, &waitreceive); if (p) { movevalues(L, p->L); p->channel = NULL; pthread_cond_signal(&p->cond); } else { waitonlist(L, channel, &waitsend); }
pthread_mutex_unlock(&kernel_access); return 0; }
static int ll_receive(lua_State *L) { Proc *p;
const char *channel = luaL_checkstring(L, 1); lua_settop(L, 1);
pthread_mutex_lock(&kernel_access);
p = searchmatch(channel, &waitsend); if (p) { movevalues(p->L, L); p->channel = NULL; pthread_cond_signal(&p->cond); } else { waitonlist(L, channel, &waitreceive); }
pthread_mutex_unlock(&kernel_access);
return lua_gettop(L) - 1; }
static void registerlib(lua_State *L, const char *name, lua_CFunction f) { lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); lua_pushcfunction(L, f); lua_setfield(L, -2, name); lua_pop(L, 2); }
static void openlibs(lua_State *L) { luaL_requiref(L, "_G", luaopen_base, 1); luaL_requiref(L, "package", luaopen_package, 1); lua_pop(L, 2); registerlib(L, "coroutine", luaopen_coroutine); registerlib(L, "table", luaopen_table); registerlib(L, "io", luaopen_io); registerlib(L, "os", luaopen_os); registerlib(L, "string", luaopen_string); registerlib(L, "math", luaopen_math); registerlib(L, "utf8", luaopen_utf8); registerlib(L, "debug", luaopen_debug); }
int luaopen_lproc(lua_State *L);
static void *ll_thread(void *arg) { lua_State *L = (lua_State *) arg; Proc *self;
openlibs(L); luaL_requiref(L, "lproc", luaopen_lproc, 1); lua_pop(L, 1);
self = (Proc *)lua_newuserdata(L, sizeof(Proc)); lua_setfield(L, LUA_REGISTRYINDEX, "_SELF"); self->L = L; self->thread = pthread_self(); self->channel = NULL; pthread_cond_init(&self->cond, NULL);
if (lua_pcall(L, 0, 0, 0) != 0) { fprintf(stderr, "thread error: %s", lua_tostring(L, -1)); }
pthread_cond_destroy(&getself(L)->cond); return NULL; }
static int ll_start(lua_State *L) { pthread_t thread;
const char *chunk = luaL_checkstring(L, 1); lua_State *L1 = luaL_newstate();
if (L1 == NULL) { luaL_error(L, "unable to create new state"); }
if (luaL_loadstring(L1, chunk) != 0) { luaL_error(L, "error in thread body: %s", lua_tostring(L1, -1)); }
if (pthread_create(&thread, NULL, ll_thread, L1) != 0) { luaL_error(L, "unable to create new thread"); }
pthread_detach(thread); return 0; }
static int ll_exit(lua_State *L) { pthread_exit(NULL); return 0; }
static const struct luaL_Reg ll_funcs[] = { {"start", ll_start}, {"send", ll_send}, {"receive", ll_receive}, {"exit", ll_exit}, {NULL, NULL} };
int luaopen_lproc(lua_State *L) { luaL_newlib(L, ll_funcs); return 1; }
|