summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmil Renner Berthing <esmil@mailme.dk>2013-08-26 21:45:47 +0200
committerEmil Renner Berthing <esmil@mailme.dk>2013-08-26 22:31:54 +0200
commit1952c197233c9b6c7411588750169a1f2eeed64a (patch)
treed2d9a1452fab2f6cc7807f930c165586c95b7b02
parent72b7777c15ec2f806606cca8bff6211e4774daaa (diff)
downloadlem-1952c197233c9b6c7411588750169a1f2eeed64a.tar.gz
lem-1952c197233c9b6c7411588750169a1f2eeed64a.tar.xz
lem-1952c197233c9b6c7411588750169a1f2eeed64a.zip
pool: better API
The thread pool doesn't actually need the Lua thread, so let the callers handle that. Also if we don't care about the return allow the reaper function to be NULL and automatically free the lem_async struct when the worker is done.
-rw-r--r--bin/pool.c7
-rw-r--r--include/lem.h12
-rw-r--r--lem/io/core.c30
-rw-r--r--lem/io/file.c75
-rw-r--r--lem/io/stream.c6
-rw-r--r--lem/io/tcp.c11
-rw-r--r--lem/io/unix.c11
-rw-r--r--lem/lfs/core.c58
8 files changed, 117 insertions, 93 deletions
diff --git a/bin/pool.c b/bin/pool.c
index d2b2df0..5578b72 100644
--- a/bin/pool.c
+++ b/bin/pool.c
@@ -100,7 +100,10 @@ pool_cb(EV_P_ struct ev_async *w, int revents)
for (; a; a = next) {
pool_jobs--;
next = a->next;
- a->reap(a);
+ if (a->reap)
+ a->reap(a);
+ else
+ free(a);
}
if (pool_jobs == 0)
@@ -175,7 +178,7 @@ error:
}
void
-lem_async_put(struct lem_async *a)
+lem_async_run(struct lem_async *a)
{
int spawn = 0;
diff --git a/include/lem.h b/include/lem.h
index 0e34734..5b35459 100644
--- a/include/lem.h
+++ b/include/lem.h
@@ -54,7 +54,6 @@ extern struct ev_loop *lem_loop;
#endif
struct lem_async {
- lua_State *T;
void (*work)(struct lem_async *a);
void (*reap)(struct lem_async *a);
struct lem_async *next;
@@ -65,18 +64,17 @@ lua_State *lem_newthread(void);
void lem_forgetthread(lua_State *T);
void lem_queue(lua_State *T, int nargs);
void lem_exit(int status);
-void lem_async_put(struct lem_async *a);
+void lem_async_run(struct lem_async *a);
void lem_async_config(int delay, int min, int max);
static inline void
-lem_async_do(struct lem_async *a, lua_State *T,
- void (*work)(struct lem_async *),
- void (*reap)(struct lem_async *))
+lem_async_do(struct lem_async *a,
+ void (*work)(struct lem_async *a),
+ void (*reap)(struct lem_async *a))
{
- a->T = T;
a->work = work;
a->reap = reap;
- lem_async_put(a);
+ lem_async_run(a);
}
#endif
diff --git a/lem/io/core.c b/lem/io/core.c
index bd4f72a..9c8dfc2 100644
--- a/lem/io/core.c
+++ b/lem/io/core.c
@@ -114,6 +114,7 @@ error:
*/
struct open {
struct lem_async a;
+ lua_State *T;
const char *path;
int fd;
int flags;
@@ -173,7 +174,7 @@ static void
io_open_reap(struct lem_async *a)
{
struct open *o = (struct open *)a;
- lua_State *T = o->a.T;
+ lua_State *T = o->T;
int fd = o->fd;
int ret = o->flags;
@@ -247,10 +248,11 @@ io_open(lua_State *T)
return luaL_error(T, "invalid mode string");
o = lem_xmalloc(sizeof(struct open));
+ o->T = T;
o->path = path;
o->fd = perm;
o->flags = flags;
- lem_async_do(&o->a, T, io_open_work, io_open_reap);
+ lem_async_do(&o->a, io_open_work, io_open_reap);
lua_settop(T, 1);
lua_pushvalue(T, lua_upvalueindex(1));
@@ -263,6 +265,7 @@ io_open(lua_State *T)
*/
struct fromfd {
struct lem_async a;
+ lua_State *T;
int fd;
int ret;
};
@@ -322,7 +325,7 @@ static void
io_fromfd_reap(struct lem_async *a)
{
struct fromfd *ff = (struct fromfd *)a;
- lua_State *T = ff->a.T;
+ lua_State *T = ff->T;
int fd = ff->fd;
int ret = ff->ret;
@@ -351,8 +354,9 @@ io_fromfd(lua_State *T)
return luaL_argerror(T, 1, "invalid fd");
ff = lem_xmalloc(sizeof(struct fromfd));
+ ff->T = T;
ff->fd = fd;
- lem_async_do(&ff->a, T, io_fromfd_work, io_fromfd_reap);
+ lem_async_do(&ff->a, io_fromfd_work, io_fromfd_reap);
lua_settop(T, 0);
lua_pushvalue(T, lua_upvalueindex(1));
@@ -434,13 +438,14 @@ error:
*/
struct streamfile {
struct lem_async a;
+ lua_State *T;
const char *filename;
int pipe[2];
int file;
};
static void
-io_streamfile_work(struct lem_async *a)
+io_streamfile_worker(struct lem_async *a)
{
struct streamfile *s = (struct streamfile *)a;
int file = s->file;
@@ -514,15 +519,9 @@ static void
io_streamfile_reap(struct lem_async *a)
{
struct streamfile *s = (struct streamfile *)a;
- lua_State *T = s->a.T;
- int ret;
-
- if (T == NULL) {
- free(s);
- return;
- }
+ lua_State *T = s->T;
+ int ret = s->file;
- ret = s->file;
if (ret < 0) {
free(s);
lem_queue(T, io_strerror(T, -ret));
@@ -531,7 +530,7 @@ io_streamfile_reap(struct lem_async *a)
lem_debug("s->file = %d, s->pipe[0] = %d, s->pipe[1] = %d",
ret, s->pipe[0], s->pipe[1]);
- lem_async_do(&s->a, NULL, io_streamfile_work, io_streamfile_reap);
+ lem_async_do(&s->a, io_streamfile_worker, NULL);
stream_new(T, s->pipe[0], 2);
lem_queue(T, 1);
@@ -543,8 +542,9 @@ io_streamfile(lua_State *T)
const char *filename = lua_tostring(T, 1);
struct streamfile *s = lem_xmalloc(sizeof(struct streamfile));
+ s->T = T;
s->filename = filename;
- lem_async_do(&s->a, T, io_streamfile_open, io_streamfile_reap);
+ lem_async_do(&s->a, io_streamfile_open, io_streamfile_reap);
lua_settop(T, 1);
lua_pushvalue(T, lua_upvalueindex(1));
diff --git a/lem/io/file.c b/lem/io/file.c
index b7ad297..471b4a0 100644
--- a/lem/io/file.c
+++ b/lem/io/file.c
@@ -18,6 +18,7 @@
struct file {
struct lem_async a;
+ lua_State *T;
int fd;
int ret;
union {
@@ -61,7 +62,7 @@ file_new(lua_State *T, int fd, int mt)
lua_setmetatable(T, -2);
/* initialize userdata */
- f->a.T = NULL;
+ f->T = NULL;
f->fd = fd;
lem_inputbuf_init(&f->buf);
@@ -79,14 +80,6 @@ file_gc_work(struct lem_async *a)
close(gc->fd);
}
-static void
-file_gc_reap(struct lem_async *a)
-{
- struct file_gc *gc = (struct file_gc *)a;
-
- free(gc);
-}
-
static int
file_gc(lua_State *T)
{
@@ -98,7 +91,7 @@ file_gc(lua_State *T)
gc->fd = f->fd;
f->fd = -1;
- lem_async_do(&gc->a, NULL, file_gc_work, file_gc_reap);
+ lem_async_do(&gc->a, file_gc_work, NULL);
}
return 0;
@@ -133,9 +126,9 @@ static void
file_close_reap(struct lem_async *a)
{
struct file *f = (struct file *)a;
- lua_State *T = f->a.T;
+ lua_State *T = f->T;
- f->a.T = NULL;
+ f->T = NULL;
f->fd = -1;
if (f->ret) {
lem_queue(T, io_strerror(T, f->ret));
@@ -155,10 +148,11 @@ file_close(lua_State *T)
f = lua_touserdata(T, 1);
if (f->fd < 0)
return io_closed(T);
- if (f->a.T != NULL)
+ if (f->T != NULL)
return io_busy(T);
- lem_async_do(&f->a, T, file_close_work, file_close_reap);
+ f->T = T;
+ lem_async_do(&f->a, file_close_work, file_close_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
}
@@ -190,13 +184,13 @@ static void
file_readp_reap(struct lem_async *a)
{
struct file *f = (struct file *)a;
- lua_State *T = f->a.T;
+ lua_State *T = f->T;
int ret;
if (f->ret) {
enum lem_preason res = f->ret < 0 ? LEM_PCLOSED : LEM_PERROR;
- f->a.T = NULL;
+ f->T = NULL;
if (f->readp.p->destroy &&
(ret = f->readp.p->destroy(T, &f->buf, res)) > 0) {
@@ -215,12 +209,12 @@ file_readp_reap(struct lem_async *a)
ret = f->readp.p->process(T, &f->buf);
if (ret > 0) {
- f->a.T = NULL;
+ f->T = NULL;
lem_queue(T, ret);
return;
}
- lem_async_put(&f->a);
+ lem_async_run(&f->a);
}
static int
@@ -238,7 +232,7 @@ file_readp(lua_State *T)
f = lua_touserdata(T, 1);
if (f->fd < 0)
return io_closed(T);
- if (f->a.T != NULL)
+ if (f->T != NULL)
return io_busy(T);
p = lua_touserdata(T, 2);
@@ -249,8 +243,9 @@ file_readp(lua_State *T)
if (ret > 0)
return ret;
+ f->T = T;
f->readp.p = p;
- lem_async_do(&f->a, T, file_readp_work, file_readp_reap);
+ lem_async_do(&f->a, file_readp_work, file_readp_reap);
return lua_yield(T, lua_gettop(T));
}
@@ -273,11 +268,11 @@ static void
file_write_reap(struct lem_async *a)
{
struct file *f = (struct file *)a;
- lua_State *T = f->a.T;
+ lua_State *T = f->T;
int top;
if (f->ret) {
- f->a.T = NULL;
+ f->T = NULL;
lem_queue(T, io_strerror(T, f->ret));
return;
}
@@ -285,7 +280,7 @@ file_write_reap(struct lem_async *a)
top = lua_gettop(T);
do {
if (f->write.idx == top) {
- f->a.T = NULL;
+ f->T = NULL;
lua_pushboolean(T, 1);
lem_queue(T, 1);
return;
@@ -294,7 +289,7 @@ file_write_reap(struct lem_async *a)
f->write.str = lua_tolstring(T, ++f->write.idx, &f->write.len);
} while (f->write.len == 0);
- lem_async_put(&f->a);
+ lem_async_run(&f->a);
}
static int
@@ -319,17 +314,18 @@ file_write(lua_State *T)
f = lua_touserdata(T, 1);
if (f->fd < 0)
return io_closed(T);
- if (f->a.T != NULL)
+ if (f->T != NULL)
return io_busy(T);
if (len == 0) {
lua_pushboolean(T, 1);
return 1;
}
+ f->T = T;
f->write.str = str;
f->write.len = len;
f->write.idx = idx;
- lem_async_do(&f->a, T, file_write_work, file_write_reap);
+ lem_async_do(&f->a, file_write_work, file_write_reap);
return lua_yield(T, top);
}
@@ -355,9 +351,9 @@ static void
file_size_reap(struct lem_async *a)
{
struct file *f = (struct file *)a;
- lua_State *T = f->a.T;
+ lua_State *T = f->T;
- f->a.T = NULL;
+ f->T = NULL;
if (f->ret) {
lem_queue(T, io_strerror(T, f->ret));
@@ -377,10 +373,11 @@ file_size(lua_State *T)
f = lua_touserdata(T, 1);
if (f->fd < 0)
return io_closed(T);
- if (f->a.T != NULL)
+ if (f->T != NULL)
return io_busy(T);
- lem_async_do(&f->a, T, file_size_work, file_size_reap);
+ f->T = T;
+ lem_async_do(&f->a, file_size_work, file_size_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -407,9 +404,9 @@ static void
file_seek_reap(struct lem_async *a)
{
struct file *f = (struct file *)a;
- lua_State *T = f->a.T;
+ lua_State *T = f->T;
- f->a.T = NULL;
+ f->T = NULL;
if (f->ret) {
lem_queue(T, io_strerror(T, f->ret));
@@ -438,14 +435,15 @@ file_seek(lua_State *T)
"not an integer in proper range");
if (f->fd < 0)
return io_closed(T);
- if (f->a.T != NULL)
+ if (f->T != NULL)
return io_busy(T);
/* flush input buffer */
lem_inputbuf_init(&f->buf);
+ f->T = T;
f->seek.whence = mode[op];
- lem_async_do(&f->a, T, file_seek_work, file_seek_reap);
+ lem_async_do(&f->a, file_seek_work, file_seek_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -475,9 +473,9 @@ static void
file_lock_reap(struct lem_async *a)
{
struct file *f = (struct file *)a;
- lua_State *T = f->a.T;
+ lua_State *T = f->T;
- f->a.T = NULL;
+ f->T = NULL;
if (f->ret) {
lem_queue(T, io_strerror(T, f->ret));
@@ -511,11 +509,12 @@ file_lock(lua_State *T)
"not an integer in proper range");
if (f->fd < 0)
return io_closed(T);
- if (f->a.T != NULL)
+ if (f->T != NULL)
return io_busy(T);
+ f->T = T;
f->lock.type = mode[op];
- lem_async_do(&f->a, T, file_lock_work, file_lock_reap);
+ lem_async_do(&f->a, file_lock_work, file_lock_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
diff --git a/lem/io/stream.c b/lem/io/stream.c
index efa2781..e91cde8 100644
--- a/lem/io/stream.c
+++ b/lem/io/stream.c
@@ -418,6 +418,7 @@ stream_getpeer(lua_State *T)
struct sfhandle {
struct lem_async a;
+ lua_State *T;
struct stream *s;
off_t size;
off_t offset;
@@ -481,8 +482,8 @@ static void
stream_sendfile_reap(struct lem_async *a)
{
struct sfhandle *sf = (struct sfhandle *)a;
+ lua_State *T = sf->T;
struct stream *s = sf->s;
- lua_State *T = sf->a.T;
int ret;
if (sf->ret == 0) {
@@ -531,11 +532,12 @@ stream_sendfile(lua_State *T)
s->w.data = T;
sf = lem_xmalloc(sizeof(struct sfhandle));
+ sf->T = T;
sf->s = s;
sf->size = size;
sf->offset = offset;
sf->fd = f->fd;
- lem_async_do(&sf->a, T, stream_sendfile_work, stream_sendfile_reap);
+ lem_async_do(&sf->a, stream_sendfile_work, stream_sendfile_reap);
lua_settop(T, 2);
return lua_yield(T, 2);
diff --git a/lem/io/tcp.c b/lem/io/tcp.c
index 91ab187..69f62a7 100644
--- a/lem/io/tcp.c
+++ b/lem/io/tcp.c
@@ -22,6 +22,7 @@
struct tcp_getaddr {
struct lem_async a;
+ lua_State *T;
const char *node;
const char *service;
int sock;
@@ -113,7 +114,7 @@ static void
tcp_connect_reap(struct lem_async *a)
{
struct tcp_getaddr *g = (struct tcp_getaddr *)a;
- lua_State *T = g->a.T;
+ lua_State *T = g->T;
int sock = g->sock;
lem_debug("connection established");
@@ -153,10 +154,11 @@ tcp_connect(lua_State *T)
struct tcp_getaddr *g;
g = lem_xmalloc(sizeof(struct tcp_getaddr));
+ g->T = T;
g->node = node;
g->service = service;
g->sock = family;
- lem_async_do(&g->a, T, tcp_connect_work, tcp_connect_reap);
+ lem_async_do(&g->a, tcp_connect_work, tcp_connect_reap);
lua_settop(T, 2);
lua_pushvalue(T, lua_upvalueindex(1));
@@ -250,7 +252,7 @@ static void
tcp_listen_reap(struct lem_async *a)
{
struct tcp_getaddr *g = (struct tcp_getaddr *)a;
- lua_State *T = g->a.T;
+ lua_State *T = g->T;
int sock = g->sock;
if (g->node == NULL)
@@ -298,11 +300,12 @@ tcp_listen(lua_State *T, int family)
node = NULL;
g = lem_xmalloc(sizeof(struct tcp_getaddr));
+ g->T = T;
g->node = node;
g->service = service;
g->sock = family;
g->err = backlog;
- lem_async_do(&g->a, T, tcp_listen_work, tcp_listen_reap);
+ lem_async_do(&g->a, tcp_listen_work, tcp_listen_reap);
lua_settop(T, 2);
lua_pushvalue(T, lua_upvalueindex(1));
diff --git a/lem/io/unix.c b/lem/io/unix.c
index ccc9994..bf0dd61 100644
--- a/lem/io/unix.c
+++ b/lem/io/unix.c
@@ -18,6 +18,7 @@
struct unix_create {
struct lem_async a;
+ lua_State *T;
const char *path;
size_t len;
int sock;
@@ -76,7 +77,7 @@ static void
unix_connect_reap(struct lem_async *a)
{
struct unix_create *u = (struct unix_create *)a;
- lua_State *T = u->a.T;
+ lua_State *T = u->T;
int sock = u->sock;
if (sock >= 0) {
@@ -113,9 +114,10 @@ unix_connect(lua_State *T)
return luaL_argerror(T, 1, "path too long");
u = lem_xmalloc(sizeof(struct unix_create));
+ u->T = T;
u->path = path;
u->len = len;
- lem_async_do(&u->a, T, unix_connect_work, unix_connect_reap);
+ lem_async_do(&u->a, unix_connect_work, unix_connect_reap);
lua_settop(T, 1);
lua_pushvalue(T, lua_upvalueindex(1));
@@ -186,7 +188,7 @@ static void
unix_listen_reap(struct lem_async *a)
{
struct unix_create *u = (struct unix_create *)a;
- lua_State *T = u->a.T;
+ lua_State *T = u->T;
int sock = u->sock;
if (sock >= 0) {
@@ -232,11 +234,12 @@ unix_listen(lua_State *T)
return luaL_argerror(T, 1, "path too long");
u = lem_xmalloc(sizeof(struct unix_create));
+ u->T = T;
u->path = path;
u->len = len;
u->sock = perm;
u->err = backlog;
- lem_async_do(&u->a, T, unix_listen_work, unix_listen_reap);
+ lem_async_do(&u->a, unix_listen_work, unix_listen_reap);
lua_settop(T, 1);
lua_pushvalue(T, lua_upvalueindex(1));
diff --git a/lem/lfs/core.c b/lem/lfs/core.c
index 945d0cb..cbb451f 100644
--- a/lem/lfs/core.c
+++ b/lem/lfs/core.c
@@ -54,6 +54,7 @@ lfs_strerror(lua_State *T, int err)
struct lfs_pathop {
struct lem_async a;
+ lua_State *T;
union {
const char *path;
int ret;
@@ -64,7 +65,7 @@ static void
lfs_pathop_reap(struct lem_async *a)
{
struct lfs_pathop *po = (struct lfs_pathop *)a;
- lua_State *T = po->a.T;
+ lua_State *T = po->T;
int ret = po->ret;
free(po);
@@ -98,8 +99,9 @@ lfs_chdir(lua_State *T)
struct lfs_pathop *po;
po = lem_xmalloc(sizeof(struct lfs_pathop));
+ po->T = T;
po->path = path;
- lem_async_do(&po->a, T, lfs_chdir_work, lfs_pathop_reap);
+ lem_async_do(&po->a, lfs_chdir_work, lfs_pathop_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -127,8 +129,9 @@ lfs_mkdir(lua_State *T)
struct lfs_pathop *po;
po = lem_xmalloc(sizeof(struct lfs_pathop));
+ po->T = T;
po->path = path;
- lem_async_do(&po->a, T, lfs_mkdir_work, lfs_pathop_reap);
+ lem_async_do(&po->a, lfs_mkdir_work, lfs_pathop_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -155,8 +158,9 @@ lfs_rmdir(lua_State *T)
struct lfs_pathop *po;
po = lem_xmalloc(sizeof(struct lfs_pathop));
+ po->T = T;
po->path = path;
- lem_async_do(&po->a, T, lfs_rmdir_work, lfs_pathop_reap);
+ lem_async_do(&po->a, lfs_rmdir_work, lfs_pathop_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -183,8 +187,9 @@ lfs_remove(lua_State *T)
struct lfs_pathop *po;
po = lem_xmalloc(sizeof(struct lfs_pathop));
+ po->T = T;
po->path = path;
- lem_async_do(&po->a, T, lfs_remove_work, lfs_pathop_reap);
+ lem_async_do(&po->a, lfs_remove_work, lfs_pathop_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -195,6 +200,7 @@ lfs_remove(lua_State *T)
*/
struct lfs_twoop {
struct lem_async a;
+ lua_State *T;
union {
struct {
const char *old;
@@ -208,7 +214,7 @@ static void
lfs_twoop_reap(struct lem_async *a)
{
struct lfs_twoop *to = (struct lfs_twoop *)a;
- lua_State *T = to->a.T;
+ lua_State *T = to->T;
int ret = to->ret;
free(to);
@@ -252,9 +258,10 @@ lfs_link(lua_State *T)
struct lfs_twoop *to;
to = lem_xmalloc(sizeof(struct lfs_twoop));
+ to->T = T;
to->old = old;
to->new = new;
- lem_async_do(&to->a, T, symlink ? lfs_symlink_work : lfs_link_work,
+ lem_async_do(&to->a, symlink ? lfs_symlink_work : lfs_link_work,
lfs_twoop_reap);
lua_settop(T, 2);
@@ -280,9 +287,10 @@ lfs_rename(lua_State *T)
struct lfs_twoop *to;
to = lem_xmalloc(sizeof(struct lfs_twoop));
+ to->T = T;
to->old = old;
to->new = new;
- lem_async_do(&to->a, T, lfs_rename_work, lfs_twoop_reap);
+ lem_async_do(&to->a, lfs_rename_work, lfs_twoop_reap);
lua_settop(T, 2);
return lua_yield(T, 2);
@@ -294,6 +302,7 @@ lfs_rename(lua_State *T)
struct lfs_attr {
struct lem_async a;
struct stat st;
+ lua_State *T;
const char *path;
int op;
int ret;
@@ -403,7 +412,7 @@ static void
lfs_attr_reap(struct lem_async *a)
{
struct lfs_attr *at = (struct lfs_attr *)a;
- lua_State *T = at->a.T;
+ lua_State *T = at->T;
struct stat *st = &at->st;
if (at->ret) {
@@ -435,9 +444,10 @@ lfs_attr(lua_State *T)
struct lfs_attr *at;
at = lem_xmalloc(sizeof(struct lfs_attr));
+ at->T = T;
at->path = path;
at->op = op;
- lem_async_do(&at->a, T, lfs_stat_work, lfs_attr_reap);
+ lem_async_do(&at->a, lfs_stat_work, lfs_attr_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -450,8 +460,9 @@ lfs_symattr(lua_State *T)
struct lfs_attr *at;
at = lem_xmalloc(sizeof(struct lfs_attr));
+ at->T = T;
at->path = path;
- lem_async_do(&at->a, T, lfs_lstat_work, lfs_attr_reap);
+ lem_async_do(&at->a, lfs_lstat_work, lfs_attr_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -462,6 +473,7 @@ lfs_symattr(lua_State *T)
*/
struct lfs_touch {
struct lem_async a;
+ lua_State *T;
union {
struct {
struct utimbuf utb;
@@ -487,7 +499,7 @@ static void
lfs_touch_reap(struct lem_async *a)
{
struct lfs_touch *t = (struct lfs_touch *)a;
- lua_State *T = t->a.T;
+ lua_State *T = t->T;
int ret = t->ret;
free(t);
@@ -507,6 +519,7 @@ lfs_touch(lua_State *T)
struct lfs_touch *t;
t = lem_xmalloc(sizeof(struct lfs_touch));
+ t->T = T;
t->path = path;
if (lua_gettop(T) == 1) {
t->buf = NULL;
@@ -515,7 +528,7 @@ lfs_touch(lua_State *T)
t->utb.modtime = luaL_optnumber(T, 3, t->utb.actime);
t->buf = &t->utb;
}
- lem_async_do(&t->a, T, lfs_touch_work, lfs_touch_reap);
+ lem_async_do(&t->a, lfs_touch_work, lfs_touch_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -555,6 +568,7 @@ lfs_currentdir(lua_State *T)
*/
struct lfs_dir {
struct lem_async a;
+ lua_State *T;
DIR *handle;
struct dirent *entry;
union {
@@ -590,7 +604,7 @@ lfs_dir_close(lua_State *T)
d = lua_touserdata(T, 1);
if (d->handle == NULL)
return lfs_closed(T);
- if (d->a.T != NULL)
+ if (d->T != NULL)
return lfs_busy(T);
ret = closedir(d->handle);
@@ -626,9 +640,9 @@ static void
lfs_dir_next_reap(struct lem_async *a)
{
struct lfs_dir *d = (struct lfs_dir *)a;
- lua_State *T = d->a.T;
+ lua_State *T = d->T;
- d->a.T = NULL;
+ d->T = NULL;
if (d->ret) {
lem_queue(T, lfs_strerror(T, d->ret));
return;
@@ -650,10 +664,11 @@ lfs_dir_next(lua_State *T)
d = lua_touserdata(T, 1);
if (d->handle == NULL)
return lfs_closed(T);
- if (d->a.T != NULL)
+ if (d->T != NULL)
return lfs_busy(T);
- lem_async_do(&d->a, T, lfs_dir_next_work, lfs_dir_next_reap);
+ d->T = T;
+ lem_async_do(&d->a, lfs_dir_next_work, lfs_dir_next_reap);
lua_settop(T, 1);
return lua_yield(T, 1);
@@ -678,9 +693,9 @@ static void
lfs_dir_reap(struct lem_async *a)
{
struct lfs_dir *d = (struct lfs_dir *)a;
- lua_State *T = d->a.T;
+ lua_State *T = d->T;
- d->a.T = NULL;
+ d->T = NULL;
if (d->ret)
lem_queue(T, lfs_strerror(T, d->ret));
else
@@ -701,9 +716,10 @@ lfs_dir(lua_State *T)
lua_pushvalue(T, lua_upvalueindex(2));
lua_setmetatable(T, -2);
+ d->T = T;
d->handle = NULL;
d->path = path;
- lem_async_do(&d->a, T, lfs_dir_work, lfs_dir_reap);
+ lem_async_do(&d->a, lfs_dir_work, lfs_dir_reap);
return lua_yield(T, 3);
}